本文整理匯總了Python中inspect.ismethoddescriptor方法的典型用法代碼示例。如果您正苦於以下問題:Python inspect.ismethoddescriptor方法的具體用法?Python inspect.ismethoddescriptor怎麽用?Python inspect.ismethoddescriptor使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類inspect
的用法示例。
在下文中一共展示了inspect.ismethoddescriptor方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_func_args
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import ismethoddescriptor [as 別名]
def get_func_args(func, stripself=False):
"""Return the argument name list of a callable"""
if inspect.isfunction(func):
func_args, _, _, _ = _getargspec_py23(func)
elif inspect.isclass(func):
return get_func_args(func.__init__, True)
elif inspect.ismethod(func):
return get_func_args(func.__func__, True)
elif inspect.ismethoddescriptor(func):
return []
elif isinstance(func, partial):
return [x for x in get_func_args(func.func)[len(func.args):]
if not (func.keywords and x in func.keywords)]
elif hasattr(func, '__call__'):
if inspect.isroutine(func):
return []
elif getattr(func, '__name__', None) == '__call__':
return []
else:
return get_func_args(func.__call__, True)
else:
raise TypeError('%s is not callable' % type(func))
if stripself:
func_args.pop(0)
return func_args
示例2: isfunc
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import ismethoddescriptor [as 別名]
def isfunc(mod, f):
assert hasattr(mod, f)
attr = getattr(mod, f)
#Ignore functions like _add
if (len(f) >= 2):
if f[0] == "_" and f[1] != "_":
return False
#Ignore functions from this list
ignore = ['__all__', '__array__', '__array_priority__', '__array_wrap__', '__bool__', '__builtins__', '__cached__', '__class__', '__deepcopy__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__file__', '__format__', '__getattribute__', '__getitem__', '__hash__', '__index__', '__init__', '__init_subclass__', '__iter__', '__len__', '__loader__', '__module__', '__name__', '__new__', '__nonzero__', '__package__', '__path__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__setitem__', '__setstate__', '__sizeof__', '__spec__', '__str__', '__subclasshook__', '__version__', '__weakref__']
#Add functions to this list if they cause recursion
ignore += ['size', 'tolist', 'dim', 'is_storage', 'item']
if f in ignore:
return False
return ins.ismethod(attr) or ins.isfunction(attr) or ins.ismethoddescriptor(attr) or ins.isbuiltin(attr)
示例3: default
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import ismethoddescriptor [as 別名]
def default(self, obj):
if isinstance(obj, datetime):
return obj.isoformat() + 'Z'
if hasattr(obj, "to_json"):
return self.default(obj.to_json())
elif hasattr(obj, "__dict__"):
d = dict(
(key, value)
for key, value in inspect.getmembers(obj)
if not key.startswith("_")
and not inspect.isabstract(value)
and not inspect.isbuiltin(value)
and not inspect.isfunction(value)
and not inspect.isgenerator(value)
and not inspect.isgeneratorfunction(value)
and not inspect.ismethod(value)
and not inspect.ismethoddescriptor(value)
and not inspect.isroutine(value)
)
return self.default(d)
return obj
示例4: format_args
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import ismethoddescriptor [as 別名]
def format_args(self):
if inspect.isbuiltin(self.object) or \
inspect.ismethoddescriptor(self.object):
# can never get arguments of a C function or method unless
# a function to do so is supplied
if self.env.config.autodoc_builtin_argspec:
argspec = self.env.config.autodoc_builtin_argspec(self.object)
return inspect.formatargspec(*argspec)
else:
return None
try:
argspec = inspect.getargspec(self.object)
except TypeError:
# if a class should be documented as function (yay duck
# typing) we try to use the constructor signature as function
# signature without the first argument.
try:
argspec = inspect.getargspec(self.object.__new__)
except TypeError:
argspec = inspect.getargspec(self.object.__init__)
if argspec[0]:
del argspec[0][0]
return inspect.formatargspec(*argspec)
示例5: getMethods
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import ismethoddescriptor [as 別名]
def getMethods(self, object, class_name):
try:
key = "%s:%s" % (self.module_name, class_name)
blacklist = BLACKLIST[key]
except KeyError:
blacklist = set()
methods = []
for name in dir(object):
if name in blacklist:
continue
if SKIP_PRIVATE and name.startswith("__"):
continue
attr = getattr(object, name)
if not ismethoddescriptor(attr):
continue
methods.append(name)
return methods
示例6: auto_override_class
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import ismethoddescriptor [as 別名]
def auto_override_class(cls, force = False, force_recursive = False):
"""Works like auto_override, but is only applicable to classes.
"""
if not pytypes.checking_enabled:
return cls
assert(isclass(cls))
if not force and is_no_type_check(cls):
return cls
# To play it safe we avoid to modify the dict while iterating over it,
# so we previously cache keys.
# For this we don't use keys() because of Python 3.
# Todo: Better use inspect.getmembers here
keys = [key for key in cls.__dict__]
for key in keys:
memb = cls.__dict__[key]
if force_recursive or not is_no_type_check(memb):
if isfunction(memb) or ismethod(memb) or ismethoddescriptor(memb):
if util._has_base_method(memb, cls):
setattr(cls, key, override(memb))
elif isclass(memb):
auto_override_class(memb, force_recursive, force_recursive)
return cls
示例7: _is_some_method
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import ismethoddescriptor [as 別名]
def _is_some_method(obj):
return (inspect.isfunction(obj) or
inspect.ismethod(obj) or
inspect.isbuiltin(obj) or
inspect.ismethoddescriptor(obj))
示例8: _is_some_method
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import ismethoddescriptor [as 別名]
def _is_some_method(obj):
return inspect.ismethod(obj) or inspect.ismethoddescriptor(obj)
示例9: test_ipy3_gh230
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import ismethoddescriptor [as 別名]
def test_ipy3_gh230(self):
"""https://github.com/IronLanguages/ironpython3/pull/230"""
import inspect
class test(object): pass
self.assertFalse(inspect.ismethoddescriptor(test.__weakref__))
self.assertFalse(inspect.ismethoddescriptor(test.__dict__["__dict__"]))
示例10: _which_methods_should_we_auto_overload
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import ismethoddescriptor [as 別名]
def _which_methods_should_we_auto_overload(self, tensor_type: type):
"""Creates a list of Torch methods to auto overload.
By default, it looks for the intersection between the methods of
tensor_type and torch_type minus those in the exception list
(syft.torch.exclude).
Args:
tensor_type: Iterate through the properties of this tensor type.
syft_type: Iterate through all attributes in this type.
Returns:
A list of methods to be overloaded.
"""
to_overload = self.boolean_comparators.copy()
native_pattern = re.compile("native*")
for attr in dir(tensor_type):
# Conditions for not overloading the method
# TODO[jvmancuso] separate func exclusion from method exclusion
if attr in syft.framework.exclude:
continue
if not hasattr(tensor_type, attr):
continue
lit = getattr(tensor_type, attr)
is_base = attr in dir(object)
is_desc = inspect.ismethoddescriptor(lit)
is_func = isinstance(lit, types.FunctionType)
is_overloaded = native_pattern.match(attr) is not None
if (is_desc or is_func) and not is_base and not is_overloaded:
to_overload.append(attr)
return set(to_overload)
示例11: _from_module
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import ismethoddescriptor [as 別名]
def _from_module(self, module, object):
"""
Return true if the given object is defined in the given
module.
"""
if module is None:
return True
elif inspect.isfunction(object):
return module.__dict__ is object.func_globals
elif inspect.isbuiltin(object):
return module.__name__ == object.__module__
elif inspect.isclass(object):
return module.__name__ == object.__module__
elif inspect.ismethod(object):
# This one may be a bug in cython that fails to correctly set the
# __module__ attribute of methods, but since the same error is easy
# to make by extension code writers, having this safety in place
# isn't such a bad idea
return module.__name__ == object.im_class.__module__
elif inspect.getmodule(object) is not None:
return module is inspect.getmodule(object)
elif hasattr(object, '__module__'):
return module.__name__ == object.__module__
elif isinstance(object, property):
return True # [XX] no way not be sure.
elif inspect.ismethoddescriptor(object):
# Unbound PyQt signals reach this point in Python 3.4b3, and we want
# to avoid throwing an error. See also http://bugs.python.org/issue3158
return False
else:
raise ValueError("object must be a class or function")
示例12: type
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import ismethoddescriptor [as 別名]
def type(self):
cls = self._cls().obj
if inspect.isclass(cls):
return 'class'
elif inspect.ismodule(cls):
return 'module'
elif inspect.isbuiltin(cls) or inspect.ismethod(cls) \
or inspect.ismethoddescriptor(cls):
return 'function'
示例13: is_class_instance
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import ismethoddescriptor [as 別名]
def is_class_instance(obj):
"""Like inspect.* methods."""
return not (inspect.isclass(obj) or inspect.ismodule(obj)
or inspect.isbuiltin(obj) or inspect.ismethod(obj)
or inspect.ismethoddescriptor(obj) or inspect.iscode(obj)
or inspect.isgenerator(obj))
示例14: __init__
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import ismethoddescriptor [as 別名]
def __init__(self, clazz, object, dispatcher):
self.clazz = clazz
self.object = object
self.dispatcher = dispatcher
# XXX: This is here for inspect.ismethoddescriptor which needs to
# return True for help() to work properly. There may be a better
# way to do this.
示例15: set_doc
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import ismethoddescriptor [as 別名]
def set_doc(obj, docstring):
if ((inspect.ismethoddescriptor(obj) or inspect.isroutine(obj))
and not inspect.isfunction(obj)):
# pybind-generated functions and methods
_core._set_pybind_doc(obj, docstring)
return
obj.__doc__ = docstring