-
-
Notifications
You must be signed in to change notification settings - Fork 205
Start to split off formatting routines.. #1376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b96d5ec
Start to split off formatting routines..
rocky 495a84b
DRY code a little
rocky ee8e6d3
to_svg doesn't seem needed for _ArcBox
rocky 33fc83a
Mark what's needed and tidy
rocky 346b19f
Relocate one more SVG to formatter space
rocky faf0528
Test Point and Ellipse
rocky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,48 @@ | ||
| # key is str: to_xxx name, value is formatter function to call | ||
| format2fn = {} | ||
| import inspect | ||
| from typing import Callable | ||
|
|
||
| # key is str: (to_xxx name, value) is formatter function to call | ||
| format2fn = {} | ||
|
|
||
|
|
||
| def lookup_method(self, format: str): | ||
| def lookup_method(self, format: str) -> Callable: | ||
| """ | ||
| Find a conversion method for `format` in self's class method resolution order. | ||
| """ | ||
| for cls in inspect.getmro(type(self)): | ||
| format_fn = format2fn.get(("svg", cls), None) | ||
| format_fn = format2fn.get((format, cls), None) | ||
| if format_fn is not None: | ||
| # print(f"format function: {format_fn.__name__} for {type(self).__name__}") | ||
| return format_fn | ||
| raise RuntimeError(f"Can't find formatter {format} for {type(self)}") | ||
| raise RuntimeError( | ||
| f"Can't find formatter {format_fn.__name__} for {type(self).__name__}" | ||
| ) | ||
|
|
||
|
|
||
| def add_conversion_fn(cls) -> None: | ||
| """Add to `format2fn` a mapping from a conversion type and builtin-class | ||
| to a conversion method. | ||
|
|
||
| The conversion type is determined form the module name. | ||
| For example, in module mathics.formatter.svg the conversion | ||
| type is "svg". | ||
|
|
||
| The conversion method is assumed to be a method in the caller's | ||
| module, and is derived from lowercasing `cls`. | ||
|
|
||
| For example function arrowbox in module mathics.formatter.svg would be | ||
| the SVG conversion routine for class ArrowBox. | ||
|
|
||
| We use frame introspection to get all of this done. | ||
| """ | ||
| fr = inspect.currentframe().f_back | ||
| module_dict = fr.f_globals | ||
|
|
||
| # The last part of the module name is expected to be the conversion routine. | ||
| conversion_type = module_dict["__name__"].split(".")[-1] | ||
|
|
||
| # Derive the conversion function from the passed-in class argument. | ||
| module_fn_name = cls.__name__.lower() | ||
|
|
||
| # Finally register the mapping: (Builtin-class, conversion name) -> conversion_function. | ||
| format2fn[(conversion_type, cls)] = module_dict[module_fn_name] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mmatera In this comment I start to DRY to code. As with a number of other places where we DRY code, it does it by assuming some sort of convention. Conventions and abbreviations abound in Physics for example, and if you don't understand the convention, then things are more complex.
Therefore for experimental kinds of things I find it useful to start out with the long form and then figure out how to shorten it.
In proofs, some mathematicians and physicists are able to go immediately to the additional abbreviations and skip steps in proofs. I can't and find it helpful to start out with the long form and then compress from there.
There still are two routines that need to get moved out of the Builtin code. And I would like to start to work up a test for just the conversion routines.
One other thing I love about this is that I now see the possibility of being able to write a utility function which I can use to write SVG. With this I can use this to experiment with SVG's in a way that is in less heavy form than going through Mathics. And we can do that for Asymptote and the next thing and the next thing...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is a temporary way to disentangle the code, I think it is OK. Also if at the end, we only expose to the core a function that convert an expression into an svg.