def __init__(self, getattr=getattr, skip_types=( types.ModuleType, types.IntType, types.StringType, types.NoneType, ), del_types=( types.FunctionType, types.MethodType, types.BuiltinFunctionType, types.BuiltinMethodType, ), deepcopy=copy.deepcopy, ClassType=types.ClassType): # Localize class attributes in the instance dict classobj = self.__class__ freeze(classobj) dict = self.__dict__ dict.update(classobj.__dict__) # Fixup attributes for name, obj in dict.items(): if name[:1] == '_ ': # Delete all special attributes del dict[name] objtype = type(obj) if objtype is ClassType: # Handle classes if name != 'DefaultSection' and \ issubclass(obj,ConfigSection): # Instantiate ConfigSections dict[name] = obj() # leave all others as classes elif objtype in del_types: # Delete these types del dict[name] elif objtype in skip_types: # Skip these types pass else: # Deepcopy everything else try: dict[name] = deepcopy(obj) except copy.error, why: raise Error,\ 'namespace entry "%s" could not be copied: %s' % \ (name,why)
def __init__(self, getattr=getattr, skip_types=(types.ModuleType, types.IntType, types.StringType, types.NoneType, ), del_types=(types.FunctionType, types.UnboundMethodType, types.MethodType, types.BuiltinFunctionType, types.BuiltinMethodType,), deepcopy=copy.deepcopy,ClassType=types.ClassType): # Localize class attributes in the instance dict classobj = self.__class__ freeze(classobj) dict = self.__dict__ dict.update(classobj.__dict__) # Fixup attributes for name,obj in dict.items(): if name[:1] == '_ ': # Delete all special attributes del dict[name] objtype = type(obj) if objtype is ClassType: # Handle classes if name != 'DefaultSection' and \ issubclass(obj,ConfigSection): # Instantiate ConfigSections dict[name] = obj() # leave all others as classes elif objtype in del_types: # Delete these types del dict[name] elif objtype in skip_types: # Skip these types pass else: # Deepcopy everything else try: dict[name] = deepcopy(obj) except copy.error,why: raise Error,\ 'namespace entry "%s" could not be copied: %s' % \ (name,why)
""" # runtime O(1) if type(keyindex) is IntType: # om[0] try: key = self.list[keyindex] except IndexError: return self._default else: return self.dict[key] else: # om['bar'] try: return self.dict[keyindex] except KeyError: return self._default freeze(OrderedMappingWithDefault) ### if __name__ == '__main__': o = OrderedMapping() o['a'] = 'b' o['b'] = 'c' o['c'] = 'd' p = OrderedMapping() p['A'] = 'B' p['B'] = 'C' r = OrderedMapping(('1',1),('2',2),('3',3))
### class NamespaceWithDefault(DefaultMixin,Namespace): """ Namespace class with an infinite number of attributes. The namespace returns _default for all undefined attributes (except the ones that start with an underscore, see __getattr__). The _default is defined as '' for this class. The acquisition feature is *not* available for instances of this class. """ freeze(NamespaceWithDefault) ### class _NotFound: pass _NotFound = _NotFound() class FormatNamespaceMixin: """ This mixin makes a Namespace class conform to the Python format string mechanism. The following format specifiers are supported: %(attribute)s - gives the string representation
self.__class__.__name__, self.proxy_object_repr, id(self)) # # XXX Use specialized unbound C methods for these... trading a Python call # against a dict lookup. # def __getattr__(self,what): return self.proxy_getattr(what) def __setattr__(self,what,to): self.proxy_setattr(what,to) freeze(InstanceProxy) class CachingInstanceProxy(InstanceProxy): """ Proxy that wraps Python instances transparently and caches accessed attributes and methods. Note that cached attributes are not looked up in the wrapped instance after the first lookup -- if their value changes, this won't be noticed by objects that access the object through this wrapper. """ def __getattr__(self,what): self.__dict__[what] = o = self.proxy_getattr(what)
### class NamespaceWithDefault(DefaultMixin,Namespace): """ Namespace class with an infinite number of attributes. The namespace returns _default for all undefined attributes (except the ones that start with an underscore, see __getattr__). The _default is defined as '' for this class. The acquisition feature is *not* available for instances of this class. """ freeze(NamespaceWithDefault) ### class AcquisitionMixin: """ Mixin that acquires unknown attributes from a .baseobj (if set) """ baseobj = None # Base object ("containing" this object) # Implicit acquisition __getattr__ = acquire # Use the (new) builtin acquire() to implement # the acquisition mechanism
# runtime O(1) if type(keyindex) is IntType: # om[0] try: key = self.list[keyindex] except IndexError: return self._default else: return self.dict[key] else: # om['bar'] try: return self.dict[keyindex] except KeyError: return self._default freeze(OrderedMappingWithDefault) ### if __name__ == '__main__': o = OrderedMapping() o['a'] = 'b' o['b'] = 'c' o['c'] = 'd' p = OrderedMapping() p['A'] = 'B' p['B'] = 'C' r = OrderedMapping(('1', 1), ('2', 2), ('3', 3))
class NamespaceWithDefault(DefaultMixin, Namespace): """ Namespace class with an infinite number of attributes. The namespace returns _default for all undefined attributes (except the ones that start with an underscore, see __getattr__). The _default is defined as '' for this class. The acquisition feature is *not* available for instances of this class. """ freeze(NamespaceWithDefault) ### class AcquisitionMixin: """ Mixin that acquires unknown attributes from a .baseobj (if set) """ baseobj = None # Base object ("containing" this object) # Implicit acquisition __getattr__ = acquire # Use the (new) builtin acquire() to implement # the acquisition mechanism
self.__class__.__name__, self.proxy_object_repr, id(self)) # # XXX Use specialized unbound C methods for these... trading a Python call # against a dict lookup. # def __getattr__(self, what): return self.proxy_getattr(what) def __setattr__(self, what, to): self.proxy_setattr(what, to) freeze(InstanceProxy) class CachingInstanceProxy(InstanceProxy): """ Proxy that wraps Python instances transparently and caches accessed attributes and methods. Note that cached attributes are not looked up in the wrapped instance after the first lookup -- if their value changes, this won't be noticed by objects that access the object through this wrapper. """ def __getattr__(self, what): self.__dict__[what] = o = self.proxy_getattr(what)