示例#1
0
文件: Maps.py 项目: redhog/Grimoire
 def __getitem__(self, name):
     p = self.__keys__.pos(Obj.classOfInstance(name))
     if p < len(self.__keys__):
         for key in self.__keys__[p:]:
             if Obj.isInstance(name, key):
                 return super(SubTypeMap, self).__getitem__(key)
     raise AttributeError(name)
示例#2
0
文件: Maps.py 项目: redhog/Grimoire
 def __getitem__(self, name):
     p = self.__keys__.pos(name)
     if p < len(self.__keys__):
         for key in self.__keys__[p:]:
             if Obj.isSubclass(name, key):
                 return super(SubTypeMap, self).__getitem__(key)
     raise KeyError(name, self)
示例#3
0
文件: EMRO.py 项目: redhog/Grimoire
    def __new__(cls, name, bases, dict):
        emroMethods = {}
        if '__emroMethods__' in dict:
            emroMethods = dict['__emroMethods__']
        for key, value in dict.iteritems():
            if Obj.isInstance(value, EMROMethod):
                if key not in emroMethods:
                    emroMethods[key] = ([], [])
                emroMethods[key][0].extend(value.preFns)
                emroMethods[key][1].extend(value.postFns)
        dict['__emroMethods__'] = emroMethods

        def toRecursiveFn(fn):
            def EMROMethod(*arg, **kw):
                return fn(EMROMethod, *arg, **kw)
            return EMROMethod
        for key in dict['__emroMethods__'].iterkeys():
            dict['__emroMethods__'][key] = ([toRecursiveFn(method)
                                             for method in dict['__emroMethods__'][key][0]],
                                            [toRecursiveFn(method)
                                             for method in dict['__emroMethods__'][key][1]])

        obj = types.TypeType.__new__(cls, name, bases, dict)
        items = Iter.Reverse(bases) + [obj]
        emroMethods = {}
        for item in items:
            if hasattr(item, '__emroMethods__'):
                for key in item.__emroMethods__.iterkeys():
                    emroMethods[key] = ([], [])
        
        def merge(list1, list2):
            for item in list2:
                if item not in list1:
                    list1.append(item)
        for item in items:
            for key in emroMethods:
                if hasattr(item, '__emroMethods__') and key in item.__emroMethods__:
                    merge(emroMethods[key][0], item.__emroMethods__[key][0])
                    merge(emroMethods[key][1], item.__emroMethods__[key][1])
                elif hasattr(item, key):
                    merge(emroMethods[key][1], [getattr(item, key)])
        obj.__emroMethods__ = emroMethods

        for key in emroMethods:
            if emroMethods[key][0]:
                setattr(obj, key, emroMethods[key][0][0])
            else:
                setattr(obj, key, emroMethods[key][1][-1])

        return obj
示例#4
0
文件: Maps.py 项目: redhog/Grimoire
 def __getitem__(self, name):
     try:
         return super(DescMap, self).__getitem__(name)
     except AttributeError:
         pass
     except KeyError:
         pass
     if type(name) == types.InstanceType:
         try:
             return super(DescMap, self).__getitem__(Obj.classOfInstance(name))
         except AttributeError:
             pass
         except KeyError:
             pass
     try:
         return super(DescMap, self).__getitem__(type(name))
     except AttributeError:
         pass
     except KeyError:
         pass
     raise KeyError(name, self)
示例#5
0
文件: Maps.py 项目: redhog/Grimoire
 def __init__(self, key):
     self.key = key
     self.hash = Obj.hashAnyNonHashable(self.key)