Replies: 2 comments
-
Is there a way to make it so only @executor is needed without needing a metaclass? |
Beta Was this translation helpful? Give feedback.
0 replies
-
Yes and no. Something like this might work :
But it does not have any advantage to make it like that. The metaclass should be part of the core code of Comfy and it's up to dev to use it or not anyway. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Heya,
I've just started to play with Comfy and got a look on the way to make external nodes. I noticed an area where we could improve the way we register the executor function. As you know, in our current implementation, the executor function is manually assigned to the
FUNCTION
attribute of the class to be recognized by the framework.While this approach works, it can be a bit error-prone and cumbersome, especially when we need to change the executor function.
To improve this, I've implemented a new approach using Python decorators and metaclasses. Here's how it works:
executor
. When this decorator is applied to a function, it sets an attributeis_registered
of the function toTrue
. This simply marks the function for registration without changing its behavior.Node
. When a class with this metaclass is defined, the metaclass checks all the attributes of the class. If it finds a function that has theis_registered
attribute set toTrue
, it sets theFUNCTION
attribute to the name of that function.With this new approach, you can simply apply the
@executor
decorator to the function you want to register as the executor. The framework will automatically use the decorated function, without the need to manually assign the function name toFUNCTION
. This makes the code more robust and easier to maintain.Here is an example of how to use this new feature:
This concept of attribute registration using decorators and metaclasses can be extended to other attributes of the Node class as well. For example, if we have other attributes that require dynamic assignment based on certain conditions or functions, we can create specific decorators for them. By marking these functions with the decorator, we can automate the process of attribute assignment during class definition, thereby enhancing the flexibility and maintainability of the code. This approach allows us to easily adapt to changes and additions, making our framework more robust and scalable.
Please take a look at this new approach and let me know your thoughts. I believe this improvement will make our framework even more efficient and user-friendly.
Following our discussion, I am open to drafting a pull request if it's agreed that these modifications would benefit the project
Regards,
Nevysha
Beta Was this translation helpful? Give feedback.
All reactions