I am using the following (Jethon 2.7) code to decorate the function in some sections:
< Import> import code from importer import importer can be inspected import decorator def idle_disorder (method, * args, ** kwargs): # nothing: d return method (* args, ** kwargs) class UselessMetaClass (type): def __new __ (cls, for name, dict.items () in the method: if the name is not. Starstswith ('_') and inspect.isroutine (method): dict [name] = decorator (Useless_discountor, method) Return type. __new __ (cls, clsname, bases, dict) square useless (object): __metaclass__ = UselessMetaClass The goal is to decorate all public works (ie With names such as an underscore) with useless_decorator
Of course, this behavior is only required in those classes which are derived from useless.
Unfortunately, I run into MetaCLASS conflict errors I'm quite difficult to debug them. Have been taken and I think they are arising from reasons beyond my control (due to the library of a third party I am using: saikuli).
But, maybe I do not need to use a metaclass! Does anyone know a way to emulate the above code without using MetaLess?
IE, is there any other way of implementing a decorator for all functions in a category?
(PS I know that I can manually decorate each function, but this is not the solution I'm looking for)
Changing your metallax to a class decorator should be straight forward. A class decorator receives the class equally as a logic and gives the (revised) class:
def unneeded_clash_dicator (CLS): name, method, cls .__ dict __. Item (): If name does not begin then ('_') and inspect.isroutine (method): setter (cls, name, decorator (waste_decorator, method)) return class
The main difference here is that you can not do Direcly cls .__ dict __
Here, for the new style classes which will be a dictproxy that does not support the assignment, Code> setattr . Then you just make your class:
@ useless-square_dicorator class idle (object): def method_to_decorate (self, * args, * kwargs): ...
Although it will not affect subclasses of useless, they will also be decorated using a class decorator. If it is not acceptable, then a metaclass may be a better option ...
No comments:
Post a Comment