Skip to content

Conversation

@sid-mungale
Copy link
Collaborator

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

Copy link
Collaborator

@lukethehuman lukethehuman left a 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.

Copy link
Collaborator

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.

Copy link
Collaborator

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)

Copy link
Collaborator Author

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:
Copy link
Collaborator

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.

Copy link
Collaborator

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.")
```

Copy link
Collaborator Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow custom classes to utilise the GeometryMaker.parse_json method

3 participants