def issubclass(subclass, class_or_tuple): if isinstance(class_or_tuple, tuple): for class_ in class_or_tuple: if issubclass(subclass, class_): return True return False return __builtin__.issubclass(subclass, class_or_tuple)
def _handle_given_decorator(self, function, method): # # ''' Another decorator was given to this instance. This decorator \ should additionally be used on given function. ''' self.__func__ = function self.method_type = method if self.__func__ is not None: if builtins.isinstance(self.__func__, FunctionDecorator): ''' If we are wrapping a nested instance of this class we \ propagate inspected informations to lower decorator. This \ case is given if one instances of this class wraps \ another one. The last wrapping instance additionally uses \ a common or manageable wrapper. ''' self.wrapped_decorator = self.__func__ self.method_type = self.__func__.method_type self.__func__ = self.__func__.__func__ elif(builtins.isinstance(method, builtins.type) and builtins.issubclass(method, FunctionDecorator)): ''' If we are wrapping a nested class of this type we \ propagate inspected informations to lower decorator. This \ case is given if one instances of this class wraps \ another one and the lower one is given via argument. ''' self.wrapped_decorator = self.method_type(method=self.__func__) self.method_type = self.wrapped_decorator.method_type self.__func__ = self.wrapped_decorator.__func__ elif self.method_type in self.COMMON_DECORATORS: self.__func__ = self.method_type(self.__func__) return self
def _is_right_type(cls, given_type, expected_type): # # ''' Check whether a given type is expected type or given type is a \ subclass of expected type. Fixes bug that in python a boolean is a subtype of an integer. Examples: >>> CheckObject._is_right_type(bool, int) False >>> CheckObject._is_right_type(list, tuple) False >>> CheckObject._is_right_type(list, list) True >>> from collections import Iterable >>> CheckObject._is_right_type(list, Iterable) True ''' return ( given_type is expected_type or expected_type is Null or expected_type is builtins.type(None) or builtins.issubclass(given_type, expected_type) and not ( given_type is builtins.bool and expected_type is builtins.int))
def __init__(self, hcls, name='Hook', echo=False, baseclass=Hooked, *args): self.logger = log.name(name) self.echo=echo self.log('Created') self.args=args if issubclass(hcls,baseclass): self.log(str(hcls)) self.hcls=self.filter(hcls.get_all())
def __init__(self, hcls, name='Hook', echo=False, baseclass=Hooked, *args): self.logger = log.name(name) self.echo = echo self.log('Created') self.args = args if issubclass(hcls, baseclass): self.log(str(hcls)) self.hcls = self.filter(hcls.get_all())
def issubclass(obj, cls): # pylint: disable=redefined-builtin,g-bad-name """A sane implementation of issubclass. See http://bugs.python.org/issue10569 Python bare issubclass must be protected by an isinstance test first since it can only work on types and raises when provided something which is not a type. Args: obj: Any object or class. cls: The class to check against. Returns: True if obj is a subclass of cls and False otherwise. """ return isinstance(obj, type) and __builtin__.issubclass(obj, cls)
def add(self,element): l=False if type(element).__name__=='list': l=True for e in element: self.add(e) if (isinstance(element,self.elementClass) or issubclass(element.__class__,self.elementClass)) and element not in self.elements: self.elements.append(element) element.destroy=lambda: self.remove(element) element.onAdd() self.onAdd(element) return self.elements elif l: return self.elements else: self.error='wrong type of <element> [%s]' % element return False
def issubclass(class1, class2): """A version of 'issubclass' that works with extension classes as well as regular Python classes. """ # Both class objects are regular Python classes, so use the # built-in 'issubclass()'. if type(class1) is types.ClassType and type(class2) is types.ClassType: return __builtin__.issubclass(class1, class2) # Both so-called class objects have a '__bases__' attribute: ie., # they aren't regular Python classes, but they sure look like them. # Assume they are extension classes and reimplement what the builtin # 'issubclass()' does behind the scenes. elif hasattr(class1, '__bases__') and hasattr(class2, '__bases__'): # XXX it appears that "ec.__class__ is type(ec)" for an # extension class 'ec': could we/should we use this as an # additional check for extension classes? # Breadth-first traversal of class1's superclass tree. Order # doesn't matter because we're just looking for a "yes/no" # answer from the tree; if we were trying to resolve a name, # order would be important! stack = [class1] while stack: if stack[0] is class2: return 1 stack.extend(list(stack[0].__bases__)) del stack[0] else: return 0 # Not a regular class, not an extension class: blow up for consistency # with builtin 'issubclass()" else: raise TypeError, "arguments must be class or ExtensionClass objects"
def issubclass(thing, the_other_thing): """is *thing* a subclass of *the other thing*?""" return __builtin__.issubclass(thing, the_other_thing)
def issubclass(x, cls): #@ReservedAssignment "True if x is a class and subclass of cls, False otherwise. Overrides built-in issubclass() which raised exception if 'x' was not a class (inconvenient in many cases); this function accepts non-classes too." return isinstance(x, type) and __builtin__.issubclass(x, cls)
def __init__(self, method, function=None): # # ''' Collects informations about wrapped method. Examples: >>> def a(): pass >>> FunctionDecorator(a) # doctest: +ELLIPSIS Object of "FunctionDecorator" with wrapped function "a" and ... >>> FunctionDecorator(5) # doctest: +ELLIPSIS Traceback (most recent call last): ... TypeError: First ... but "int" (5) given. >>> FunctionDecorator(FunctionDecorator(a)) # doctest: +ELLIPSIS Object of "FunctionDecorator" with wrapped function "a" and ... >>> FunctionDecorator( ... FunctionDecorator, FunctionDecorator(a) ... ) # doctest: +ELLIPSIS Object of "FunctionDecorator" with wrapped function "a" and ... ''' # # # region properties '''Properties of function calls.''' self.class_object = self.object = self.return_value = \ self.wrapped_decorator = None '''Saves method types like static or class bounded.''' self.method_type = None ''' Determines if class or object references for a wrapped method are \ already determined. ''' self.arguments_determined = False '''Saves the wrapped function.''' self.__func__ = None ''' NOTE: We can't use "self.__class__" because this points usually \ to a subclass of this class. ''' if(method in self.COMMON_DECORATORS + self.MANAGEABLE_DECORATORS or builtins.isinstance(method, builtins.type) and builtins.issubclass(method, FunctionDecorator)): self._handle_given_decorator(function, method) elif builtins.isinstance(method, FunctionDecorator): ''' If we are wrapping a nested instance of this class we \ propagate inspected informations to lower decorator. This \ case is given if one instances of this class wraps another one. ''' self.wrapped_decorator = method self.__func__ = method.__func__ self.method_type = method.method_type elif(builtins.isinstance(method, (Function, Method))): self.__func__ = method else: raise builtins.TypeError( 'First argument for initializing "{class_name}" must be ' '"{common_methods}", "{function_type}" or "{method_type}" but ' '"{type}" ({value}) given.'.format( class_name=self.__class__.__name__, common_methods='", "'.join(builtins.map( lambda decorator: decorator.__name__, self.COMMON_DECORATORS + self.MANAGEABLE_DECORATORS + [FunctionDecorator])), function_type=Function.__name__, method_type=Method.__name__, type=builtins.type(method).__name__, value=builtins.str(method)))