-
Notifications
You must be signed in to change notification settings - Fork 0
66 allow custom classes to utilise the geometrymakerparse json method #92
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
base: dev
Are you sure you want to change the base?
66 allow custom classes to utilise the geometrymakerparse json method #92
Conversation
lukethehuman
left a comment
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.
Very happy to see this support being added, though I think there may be a better way to do this that doesn't require passing in the names of custom classes explicitly. See comment for suggestion.
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.
I think we should also have a test for this functionality, the same or slightly simpler version of this example code should work well as a test.
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.
Also a minor gripe, but the filename is slightly hard to read, would suggest an underscore like 3_ (would want to do this for all your example files to be consistent ofc)
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.
Changed file names and added a test
| key_route_delimiter (str): delimiter for parameter paths | ||
| ''' | ||
| def __init__(self) -> None: | ||
| def __init__(self, custom_classes=[]) -> None: |
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.
This works and might well be the best solution, but it could also be possible for us to avoid needing a keyword argument by taking advantage of the fact that the python contains a list of all the subclasses which inherit from a given base class in the current scope via: ComponentBase.__subclasses__()
Searching that list for the correct classname would discover both native hypnos subclasses and user-defined subclasses. Thus the user would only need to define their subclass and make sure it's in scope (e.g. import it) and hypnos could find it.
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.
The following function could support this:
def get_subclass_from_classname(classname) -> ComponentBase:
"""Return a ComponentBase subclass by name."""
subclasses = ComponentBase.__subclasses__()
for subclass in subclasses:
if subclass.__name__ == classname:
return subclass
raise ValueError("No such class.")
```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.
I was a bit paranoid about scopes as i think I'd originally tried something similar to this and it didn't discover every subclass, as well as feeling painful to call every time a class needs to be instantiated. Hence I opted to make a dictionary in #91 and append custom components manually. I could look into what could be causing the bug if this is a preferable way though?
Initialise GeometryMaker with a custom class and it should be able to make geometry using it
New
3custom_component.py, 3custom_component.json: Demonstrate how to create a custom component
Updated
geometry_maker.GeometryMaker