Python six.class_types() Examples
The following are 24
code examples of six.class_types().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
six
, or try the search function
.
Example #1
Source File: _utils.py From debtcollector with Apache License 2.0 | 6 votes |
def get_class_name(obj, fully_qualified=True): """Get class name for object. If object is a type, fully qualified name of the type is returned. Else, fully qualified name of the type of the object is returned. For builtin types, just name is returned. """ if not isinstance(obj, six.class_types): obj = type(obj) try: built_in = obj.__module__ in _BUILTIN_MODULES except AttributeError: 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__
Example #2
Source File: test_six.py From c4ddev with MIT License | 5 votes |
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)
Example #3
Source File: utils.py From easy_cache with MIT License | 5 votes |
def get_function_path(function, bound_to=None): """Get received function path (as string), to import function later with `import_string`. """ if isinstance(function, six.string_types): return function # static and class methods if hasattr(function, '__func__'): real_function = function.__func__ elif callable(function): real_function = function else: return function func_path = [] module = getattr(real_function, '__module__', '__main__') if module: func_path.append(module) if not bound_to: try: bound_to = six.get_method_self(function) except AttributeError: pass if bound_to: if isinstance(bound_to, six.class_types): func_path.append(bound_to.__name__) else: func_path.append(bound_to.__class__.__name__) func_path.append(real_function.__name__) else: # qualname is available in Python 3 only func_path.append(getattr(real_function, '__qualname__', real_function.__name__)) return '.'.join(func_path)
Example #4
Source File: reflection.py From oslo.utils with Apache License 2.0 | 5 votes |
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)
Example #5
Source File: reflection.py From oslo.utils with Apache License 2.0 | 5 votes |
def get_all_class_names(obj, up_to=object, fully_qualified=True, truncate_builtins=True): """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, fully_qualified=fully_qualified, truncate_builtins=truncate_builtins)
Example #6
Source File: reflection.py From oslo.utils with Apache License 2.0 | 5 votes |
def get_class_name(obj, fully_qualified=True, truncate_builtins=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) if truncate_builtins: 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__
Example #7
Source File: util.py From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International | 5 votes |
def usage(obj, selfname='self'): str(obj) # In case it's lazy, this will load it. if not isinstance(obj, class_types): obj = obj.__class__ print('%s supports the following operations:' % obj.__name__) for (name, method) in sorted(pydoc.allmethods(obj).items()): if name.startswith('_'): continue if getattr(method, '__deprecated__', False): continue if sys.version_info[0] >= 3: getargspec = inspect.getfullargspec else: getargspec = inspect.getargspec args, varargs, varkw, defaults = getargspec(method)[:4] if ( args and args[0] == 'self' and (defaults is None or len(args) > len(defaults)) ): args = args[1:] name = '%s.%s' % (selfname, name) argspec = inspect.formatargspec(args, varargs, varkw, defaults) print( textwrap.fill( '%s%s' % (name, argspec), initial_indent=' - ', subsequent_indent=' ' * (len(name) + 5), ) ) ########################################################################## # IDLE ##########################################################################
Example #8
Source File: inspection.py From qcore with Apache License 2.0 | 5 votes |
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)
Example #9
Source File: test_six.py From data with GNU General Public License v3.0 | 5 votes |
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)
Example #10
Source File: test_six.py From data with GNU General Public License v3.0 | 5 votes |
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)
Example #11
Source File: test_six.py From data with GNU General Public License v3.0 | 5 votes |
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)
Example #12
Source File: test_six.py From data with GNU General Public License v3.0 | 5 votes |
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)
Example #13
Source File: test_six.py From c4ddev with MIT License | 5 votes |
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)
Example #14
Source File: query.py From python-sdk with GNU Lesser General Public License v3.0 | 5 votes |
def __init__(self, query_class): """ :param query_class: 要查询的 class 名称或者对象 :type query_class: string_types or leancloud.ObjectMeta """ if isinstance(query_class, six.string_types): if query_class in ("File", "_File"): query_class = File else: query_class = Object.extend(query_class) if not isinstance(query_class, (type, six.class_types)) or not issubclass( query_class, (File, Object) ): raise ValueError("Query takes string or LeanCloud Object") self._query_class = query_class self._where = {} self._include = [] self._include_acl = None self._limit = -1 self._skip = 0 self._extra = {} self._order = [] self._select = []
Example #15
Source File: gen_dns_client_test.py From apitools with Apache License 2.0 | 5 votes |
def testAttributes(self): inner_classes = set([]) for key, value in dns_v1_client.DnsV1.__dict__.items(): if isinstance(value, six.class_types): inner_classes.add(key) self.assertEquals(set([ 'ChangesService', 'ProjectsService', 'ManagedZonesService', 'ResourceRecordSetsService']), inner_classes)
Example #16
Source File: iam_client_test.py From apitools with Apache License 2.0 | 5 votes |
def testAttributes(self): inner_classes = set([]) for key, value in iam_v1_client.IamV1.__dict__.items(): if isinstance(value, six.class_types): inner_classes.add(key) self.assertEquals(set([ 'IamPoliciesService', 'ProjectsService', 'ProjectsServiceAccountsKeysService', 'ProjectsServiceAccountsService', 'RolesService']), inner_classes)
Example #17
Source File: monkey.py From PhonePi_SampleServer with MIT License | 5 votes |
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)
Example #18
Source File: decorators.py From python-sasctl with Apache License 2.0 | 5 votes |
def experimental(func): """Decorate a function or class to designated it as experimental. Will raise an `ExperimentalWarning` when used and automatically adds a Sphinx '.. warning::' directive to the docstring. Parameters ---------- func Returns ------- func """ @functools.wraps(func) def _wrapper(*args, **kwargs): warning = '%s is experimental and may be modified or removed without warning.' % func.__name__ warnings.warn(warning, category=ExperimentalWarning, stacklevel=2) return func(*args, **kwargs) type_ = 'class' if isinstance(func, six.class_types) else 'method' directive = '.. warning:: This %s is experimental and may be modified or removed without warning.' % type_ try: # Insert directive into original docstring _wrapper.__doc__ = _insert_docstring_text(func, directive) except AttributeError: # __doc__ is not writable in Py27 pass return _wrapper
Example #19
Source File: test_six.py From six with MIT License | 5 votes |
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)
Example #20
Source File: test_six.py From six with MIT License | 5 votes |
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)
Example #21
Source File: conf.py From cti-python-stix2 with BSD 3-Clause "New" or "Revised" License | 5 votes |
def can_document_member(cls, member, membername, isattr, parent): return isinstance(member, class_types) and \ issubclass(member, _STIXBase) and \ hasattr(member, '_properties')
Example #22
Source File: wrap_matcher.py From learn_python3_spider with MIT License | 5 votes |
def is_matchable_type(expected_type): if isinstance(expected_type, type): return True if isinstance(expected_type, six.class_types): return True if isinstance(expected_type, tuple) and \ expected_type and \ all(map(is_matchable_type, expected_type)): return True return False
Example #23
Source File: monkey.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 5 votes |
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)
Example #24
Source File: _utils.py From debtcollector with Apache License 2.0 | 4 votes |
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__) # When running under sphinx it appears this can be none? if so just # don't include it... mod, rest = (parts[0], parts[1:]) if not mod: return '.'.join(rest) else: return '.'.join(parts)