3 Adds attributes to an existing class.
9 never_attrs = ('__module__', '__metaclass__')
11 def __new__(cls, name, bases, attrs):
13 raise AttributeError('%s: "%s" cannot add methods to more than one class.' % (cls.__name__, name))
17 for attr, value in attrs.iteritems():
18 if attr in cls.never_attrs:
20 if not cls.dunder_attrs and attr.startswith('__'):
22 if not cls.replace_attrs and hasattr(base, attr):
24 setattr(base, attr, value)
29 class MonkeyPatch(type):
31 Similar to Category, except it will replace attributes.
36 dunder_attrs = Category.dunder_attrs
37 never_attrs = Category.never_attrs
42 def unpatched(cls, klass, name):
44 return cls.unpatches[klass][name]
46 return getattr(klass, name)
48 def __new__(cls, name, bases, attrs):
50 raise AttributeError('%s: "%s" cannot patch more than one class.' % (cls.__name__, name))
54 for attr, value in attrs.iteritems():
55 if attr in cls.never_attrs:
57 if not cls.dunder_attrs and attr.startswith('__'):
59 if hasattr(base, attr):
60 if not cls.replace_attrs:
63 if base not in cls.unpatches:
64 cls.unpatches[base] = {}
65 cls.unpatches[base][attr] = getattr(base, attr)
67 setattr(base, attr, value)