我们从Python开源项目中,提取了以下43个代码示例,用于说明如何使用six.class_types()。
def get_callable_name(function): """Generate a name from callable. Tries to do the best to guess fully qualified callable name. """ method_self = get_method_self(function) if method_self is not None: # This is a bound method. if isinstance(method_self, six.class_types): # This is a bound class method. im_class = method_self else: im_class = type(method_self) try: parts = (im_class.__module__, function.__qualname__) except AttributeError: parts = (im_class.__module__, im_class.__name__, function.__name__) elif inspect.ismethod(function) or inspect.isfunction(function): # This could be a function, a static method, a unbound method... try: parts = (function.__module__, function.__qualname__) except AttributeError: if hasattr(function, 'im_class'): # This is a unbound method, which exists only in python 2.x im_class = function.im_class parts = (im_class.__module__, im_class.__name__, function.__name__) else: parts = (function.__module__, function.__name__) else: im_class = type(function) if im_class is _TYPE_TYPE: im_class = function try: parts = (im_class.__module__, im_class.__qualname__) except AttributeError: parts = (im_class.__module__, im_class.__name__) return '.'.join(parts)
def get_routes(package): routes = [] for _, modname, ispkg in pkgutil.walk_packages( path=package.__path__, prefix=package.__name__ + '.', onerror=lambda x: None): if not ispkg: module = import_module(modname) for k, cls in vars(module).items(): if k.startswith("_") or not isinstance(cls, six.class_types): continue if issubclass(cls, BaseAPI): if getattr(cls, "route", False): routes.append(cls) return routes # monkeypatch to force application json on raised exceptions
def runTests(self): if self.catchbreak: installHandler() if self.testRunner is None: self.testRunner = runner.TextTestRunner if isinstance(self.testRunner, six.class_types): try: try: testRunner = self.testRunner(verbosity=self.verbosity, failfast=self.failfast, buffer=self.buffer, tb_locals=self.tb_locals) except TypeError: # didn't accept the tb_locals argument testRunner = self.testRunner(verbosity=self.verbosity, failfast=self.failfast, buffer=self.buffer) except TypeError: # didn't accept the verbosity, buffer or failfast arguments testRunner = self.testRunner() else: # it is assumed to be a TestRunner instance testRunner = self.testRunner self.result = testRunner.run(self.test) if self.exit: sys.exit(not self.result.wasSuccessful())
def get_class_name(obj, fully_qualified=True): """Get class name for object. If object is a type, returns name of the type. If object is a bound method or a class method, returns its ``self`` object's class name. If object is an instance of class, returns instance's class name. Else, name of the type of the object is returned. If fully_qualified is True, returns fully qualified name of the type. For builtin types, just name is returned. TypeError is raised if can't get class name from object. """ if inspect.isfunction(obj): raise TypeError("Can't get class name.") if inspect.ismethod(obj): obj = get_method_self(obj) if not isinstance(obj, six.class_types): obj = type(obj) try: built_in = obj.__module__ in _BUILTIN_MODULES except AttributeError: # nosec pass else: if built_in: return obj.__name__ if fully_qualified and hasattr(obj, '__module__'): return '%s.%s' % (obj.__module__, obj.__name__) else: return obj.__name__
def get_all_class_names(obj, up_to=object): """Get class names of object parent classes. Iterate over all class names object is instance or subclass of, in order of method resolution (mro). If up_to parameter is provided, only name of classes that are sublcasses to that class are returned. """ if not isinstance(obj, six.class_types): obj = type(obj) for cls in obj.mro(): if issubclass(cls, up_to): yield get_class_name(cls)
def get_unpatched(item): lookup = ( get_unpatched_class if isinstance(item, six.class_types) else get_unpatched_function if isinstance(item, types.FunctionType) else lambda item: None ) return lookup(item)
def is_classmethod(fn): """Returns whether f is a classmethod.""" # This is True for bound methods if not inspect.ismethod(fn): return False if not hasattr(fn, '__self__'): return False im_self = fn.__self__ # This is None for instance methods on classes, but True # for instance methods on instances. if im_self is None: return False # This is True for class methods of new- and old-style classes, respectively return isinstance(im_self, six.class_types)
def to_iter(e): """???????""" if isinstance(e, (six.string_types, six.string_types, six.class_types, six.text_type, six.binary_type, six.class_types, six.integer_types, float)): return e, elif isinstance(e, list): return e else: return e
def get_callable(subcommand): # type: (config.RcliEntryPoint) -> Union[FunctionType, MethodType] """Return a callable object from the subcommand. Args: subcommand: A object loaded from an entry point. May be a module, class, or function. Returns: The callable entry point for the subcommand. If the subcommand is a function, it will be returned unchanged. If the subcommand is a module or a class, an instance of the command class will be returned. Raises: AssertionError: Raised when a module entry point does not have a callable class named Command. """ _LOGGER.debug( 'Creating callable from subcommand "%s".', subcommand.__name__) if isinstance(subcommand, ModuleType): _LOGGER.debug('Subcommand is a module.') assert hasattr(subcommand, 'Command'), ( 'Module subcommand must have callable "Command" class definition.') callable_ = subcommand.Command # type: ignore else: callable_ = subcommand if any(isinstance(callable_, t) for t in six.class_types): return callable_() return callable_
def topickle(self, filename): # remove unpicklable attributes warnfunc = self._warnfunc self.set_warnfunc(None) values = self.config.values del self.config.values domains = self.domains del self.domains picklefile = open(filename, 'wb') # remove potentially pickling-problematic values from config for key, val in list(vars(self.config).items()): if key.startswith('_') or \ isinstance(val, types.ModuleType) or \ isinstance(val, types.FunctionType) or \ isinstance(val, class_types): del self.config[key] try: pickle.dump(self, picklefile, pickle.HIGHEST_PROTOCOL) finally: picklefile.close() # reset attributes self.domains = domains self.config.values = values self.set_warnfunc(warnfunc) # --------- ENVIRONMENT INITIALIZATION -------------------------------------
def can_document_member(cls, member, membername, isattr, parent): return isinstance(member, class_types)
def can_document_member(cls, member, membername, isattr, parent): return isinstance(member, class_types) and \ issubclass(member, BaseException)
def can_document_member(cls, member, membername, isattr, parent): isdatadesc = isdescriptor(member) and not \ isinstance(member, cls.method_types) and not \ type(member).__name__ in ("type", "method_descriptor", "instancemethod") return isdatadesc or (not isinstance(parent, ModuleDocumenter) and not inspect.isroutine(member) and not isinstance(member, class_types))
def test_class_types(): class X: pass class Y(object): pass assert isinstance(X, six.class_types) assert isinstance(Y, six.class_types) assert not isinstance(X(), six.class_types)
def test_from_imports(): from six.moves.queue import Queue assert isinstance(Queue, six.class_types) from six.moves.configparser import ConfigParser assert isinstance(ConfigParser, six.class_types)
def before(action): """Decorator to execute the given action function *before* the responder. Args: action (callable): A function of the form ``func(req, resp, resource, params)``, where `resource` is a reference to the resource class instance associated with the request, and `params` is a dict of URI Template field names, if any, that will be passed into the resource responder as kwargs. Note: Hooks may inject extra params as needed. For example:: def do_something(req, resp, resource, params): try: params['id'] = int(params['id']) except ValueError: raise falcon.HTTPBadRequest('Invalid ID', 'ID was not valid.') params['answer'] = 42 """ def _before(responder_or_resource): if isinstance(responder_or_resource, six.class_types): resource = responder_or_resource for method in HTTP_METHODS: responder_name = 'on_' + method.lower() try: responder = getattr(resource, responder_name) except AttributeError: # resource does not implement this method pass else: # Usually expect a method, but any callable will do if callable(responder): # This pattern is necessary to capture the current # value of responder in the do_before_all closure; # otherwise, they will capture the same responder # variable that is shared between iterations of the # for loop, above. def let(responder=responder): do_before_all = _wrap_with_before(action, responder) setattr(resource, responder_name, do_before_all) let() return resource else: responder = responder_or_resource do_before_one = _wrap_with_before(action, responder) return do_before_one return _before
def after(action): """Decorator to execute the given action function *after* the responder. Args: action (callable): A function of the form ``func(req, resp, resource)``, where `resource` is a reference to the resource class instance associated with the request """ def _after(responder_or_resource): if isinstance(responder_or_resource, six.class_types): resource = responder_or_resource for method in HTTP_METHODS: responder_name = 'on_' + method.lower() try: responder = getattr(resource, responder_name) except AttributeError: # resource does not implement this method pass else: # Usually expect a method, but any callable will do if callable(responder): def let(responder=responder): do_after_all = _wrap_with_after(action, responder) setattr(resource, responder_name, do_after_all) let() return resource else: responder = responder_or_resource do_after_one = _wrap_with_after(action, responder) return do_after_one return _after # ----------------------------------------------------------------------------- # Helpers # -----------------------------------------------------------------------------
def _getobj(self): # Regular object-making first obj = super(SpecInstance, self)._getobj() # Then decorate it with our parent's extra attributes, allowing nested # test classes to appear as an aggregate of parents' "scopes". # NOTE: need parent.parent due to instance->class hierarchy # NOTE: of course, skipping if we've gone out the top into a module etc if ( not hasattr(self, 'parent') or not hasattr(self.parent, 'parent') or not isinstance(self.parent.parent, SpecInstance) ): return obj parent_obj = self.parent.parent.obj # Obtain parent attributes, etc not found on our obj (serves as both a # useful identifier of "stuff added to an outer class" and a way of # ensuring that we can override such attrs), and set them on obj delta = set(dir(parent_obj)).difference(set(dir(obj))) for name in delta: value = getattr(parent_obj, name) # Classes get skipped; they'd always just be other 'inner' classes # that we don't want to copy elsewhere. if isinstance(value, six.class_types): continue # Functions (methods) may get skipped, or not, depending: if isinstance(value, types.MethodType): # If they look like tests, they get skipped; don't want to copy # tests around! if istestfunction(name): continue # Non-test == they're probably lifecycle methods # (setup/teardown) or helpers (_do_thing). Rebind them to the # target instance, otherwise the 'self' in the setup/helper is # not the same 'self' as that in the actual test method it runs # around or within! # TODO: arguably, all setup or helper methods should become # autouse class fixtures (see e.g. pytest docs under 'xunit # setup on steroids') func = six.get_method_function(value) setattr(obj, name, six.create_bound_method(func, obj)) # Anything else should be some data-type attribute, which is copied # verbatim / by-value. else: setattr(obj, name, value) return obj