66 allow custom classes to utilise the geometrymakerparse json method#92
66 allow custom classes to utilise the geometrymakerparse json method#92sid-mungale wants to merge 11 commits intodevfrom
Conversation
lukethehuman
left a comment
There was a problem hiding this comment.
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.
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.
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.
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.
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.
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.
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