Factory DRY patterns¶
CRIMINALLY INVASIVE HACKS in
Adds clumsy but automatic getter resolving.
The __getattr__ override makes this class try to call a get_*() method for variables that are not in self.__dict__.
For example, when self.foo is evaluated and ‘foo’ not in self.__dict__ then it will call the self.get_foo()
If self.get_foo() returns None, it will try to get the result again from self.__dict__. Which means that we are going to witness this horrroorr:
class YourEvil(Factory): def get_foo(self): self.calls += 1 self.foo = 13 assert YourEvil.foo == 13 # crime scene 1 assert YourEvil.foo == 13 # crime scene 2 assert YourEvil.calls == 1 # crime scene 3
For the moment it is pretty clumsy because i tried to contain the criminality rate as low as possible meanwhile i like the work it does for me !
Return a subclass with the given attributes.
If a model is found, it will prefix the class name with the model.
__getattr__that ensures a first argument to getters.
Makes the getter work both from class and instance
Thanks to this, your get_*() methods will /maybe/ work in both cases:
YourClass.foo # calls get_foo(YourClass) YourClass().foo # calls get_foo(self)
Don’t code drunk.
Return the cls.
did it go to far at this point ?