def shop(animal, status): """Class annotation. Create a getter method""" def getter(self): return self.name + " " + animal + " is " + status annotate.injectAttribute("shop", 3, "get_" + animal, getter) return status
def decorator(method): def get_accompaniment(self, *args, **kwargs): """Create a method for the accompaniment""" return self.name + " wants " + accompaniment # Inject the new method in the class annotate.injectAttribute("accompany", 3, accompaniment, get_accompaniment) # Inject the original method with a new name annotate.injectAttribute("accompany", 3, "original_" + method.__name__, method) def wrapper(self, *args, **kwargs): """Wrapp a method call and add an accompaniment to its result""" result = method(self, *args, **kwargs) return result + " and " + accompaniment # Call the class to register the decorator annotate.injectClassCallback("accompany", 3, "_decorator", accompaniment, method, wrapper) return wrapper
def _inject_definition(relation, descriptor_type, factory, role, force): if not force and factory == BasePartner: raise DefinitionError( "BasePartner class cannot be used in has_one or has_many " "definitions. Instead you should create a subclass for each type " "of relation you are defining.") annotate.injectClassCallback("handler", 4, "_define_handler", descriptor_type, factory, role) annotate.injectClassCallback("relation", 4, "_append_relation", relation) @property def getter(self): return self.query(relation.name) def getter_with_role(self, role): return self.query_with_role(relation.name, role) annotate.injectAttribute("relation_getter", 4, relation.name, getter) annotate.injectAttribute("relation_getter", 4, relation.name + "_with_role", getter_with_role)