git.fiddlerwoaroof.com
unsorted/pythonsnippets_0045.py
60b28ac5
 import functools
 import collections
 def instantiate(cls): return cls()
 
 class Namespace(object):
     def registry(name, bases, dict_):
         cls = type(name, bases, dict_)
         cls.__dict = {}
         return cls
     __metaclass__ = registry
     def __init__(self, instance=None):
         self.__dict__ = self.__dict
         self.__inst = instance
         self._init()
     def _init(self): pass
     def __get__(self, instance, owner):
         result = type(self)(instance)
         return result
     def __check_inst(self):
9c112cd3
         if self.__inst is None:
60b28ac5
             raise AttributeError, 'this class is unbound, can\'t call methods'
     def __getattribute__(self, name):
9c112cd3
         if not name.startswith('_'):
60b28ac5
             self.__check_inst()
         result = object.__getattribute__(self, name)
9c112cd3
         if callable(result) and not name.startswith('_'):
60b28ac5
             result = functools.partial(result, self.__inst)
         return result
 
 
 
 class DB(object):
     @instantiate
     class record(Namespace):
         @classmethod
         def _init(self):
             self.__value = 1
         def get(self, db):
             return self.__value
         def set(self, db, value):
             self.__value = value
         def checkparam(self, db):
             print db.param.get()
             return db.param.get() == self.get()
     @instantiate
     class param(Namespace):
         @classmethod
         def _init(self):
             self.__value = 1
         def get(self, db):
             db.othermethod(self.__value)
             return self.__value
         def set(self, db, value):
             db.othermethod(self.__value)
             self.__value = value
             db.othermethod(self.__value)
     def othermethod(self, value): print self, value
     def recordget(self): return self.record.get()
9c112cd3
     def recordset(self, value): return self.record.set(value)