All pastes #89374 Raw Edit

Cody Brocious

public python v1 · immutable
#89374 ·published 2006-07-16 05:06 UTC
rendered paste body
class Module(object):    __CALL_INITS__ = True        def __new__(self, *args, **kwargs):        self = object.__new__(self)        self.__objid__ = int(str(self).split(' ')[-1][:-1], 0)        self.__capsule__ = {}        if self.__CALL_INITS__:            Module.__init_all__(self, self.__class__, args=args, kwargs=kwargs)       return self    def __init_all__(self, __class__, args, kwargs):        for base in __class__.__bases__:            if base == Module:                continue            Module.__init_all__(self, base, args=args, kwargs=kwargs)            if base.__init__ != object.__init__:                code = base.__init__.im_func.func_code                # Get the number of keyword arguments by getting the length of the defaults                kwarg_count = (base.__init__.im_func.func_defaults != None and \                               len(base.__init__.im_func.func_defaults)) or 0                # Subtract the number of kwargs from the arg count                # Subtract 1 for `self' and ensure the proper number of args                arg_count = code.co_argcount - kwarg_count - 1                if arg_count > len(args):                    continue                temp_args = args[:arg_count]                # Iterate over keyword args passed into this function                # and determine which are supported                temp_kwargs = {}                for name in kwargs:                    if name in code.co_varnames and name not in code.co_names:                        temp_kwargs[name] = kwargs[name]                # Call the function                base.__init__(self, *temp_args, **temp_kwargs)    def surround(self, obj):        try:            self.__capsule__[obj.__objid__] = obj            return True        except KeyError:            return False    def release(self, obj=None, objid=None):        if objid != None:            del self.__capsule__[objid]        elif obj != None:            for id in self.__capsule__:                if self.__capsule__[id] == obj:                    del self.__capsule__[id]                    break        else:            return False