-
Notifications
You must be signed in to change notification settings - Fork 10
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
Effectory? #367
Comments
My 2cnts, which perhaps aren't worth much because I haven't looked into the instantiation of Effects much at all. I feel that factory functions are not really Pythonic. To me it seems that what But maybe I don't understand; what difference is there between Nevertheless, your main point is that the inside of these functions is a mess, and that you know a better way of doing that, which is great! Personally, I don't like the |
Taking another look (at a more reasonable time of day),
class EventFactory():
"""Generalized factory class to produce events from XML tags."""
def __init__(self):
self._creators = {}
def register_type(self, tag: str, creator: Callable) -> None:
"""Add tag and event class to registry."""
self._creators[tag] = creator
def create_event(self, tag: str, **kwargs):
"""Create event class from registered tag."""
creator = self._creators.get(tag)
if not creator:
raise ValueError(tag)
return creator(**kwargs)
factory = EventFactory()
factory.register_type("event", Event)
factory.register_type("superevent", SuperEvent)
factory.register_type("multievent", MultiEvent)
factory.register_type("subevent", SubEvent) |
Registration of subclasses is a common usecase for meta classes. Perhaps registering classes is the main reason meta classes even exist in Python. Everything is a object in Python; classes are instances of a meta-class (which in reality is just a class, nothing really meta about it). You can specify the meta class that a specific class is an instance off. So the As a syntactic sugar for the above, you can simply overload the Metaclasses are real fun, and also an easy to create an unintelligible mess, so buyer beware! |
About that last point; |
I've read about metaclasses in python and the |
Just a silly little post-midnight idea I just had, writing it down so I don't forget:
The way we create effects in
effects.effect_utils.make_effect()
, and by extent in the (rather convoluted) constructor ofoptics.optical_element.OpticalElement
is a bit ... meh.Maybe this could be streamlined via a factory-pattern for effects, hence the name of this issue, the effect-factory or
Effactory
. This would also help in the recent struggle related to the whole currsys/cmds thing, that needs to be passed to an effect. In summary, we might want to only create effects (i.e. instances of subclasses ofeffects.Effect
) via this method. I'm not suggesting to make it impossible to directly instantiate an effect subclass, but the effectory should be the default way, that we use throughout the production code (in ScopeSim and elsewhere (IRDB etc.)).The text was updated successfully, but these errors were encountered: