本文整理匯總了Python中inspect.isdatadescriptor方法的典型用法代碼示例。如果您正苦於以下問題:Python inspect.isdatadescriptor方法的具體用法?Python inspect.isdatadescriptor怎麽用?Python inspect.isdatadescriptor使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類inspect
的用法示例。
在下文中一共展示了inspect.isdatadescriptor方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_excluding_predicates
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isdatadescriptor [as 別名]
def test_excluding_predicates(self):
self.istest(inspect.isbuiltin, 'sys.exit')
self.istest(inspect.isbuiltin, '[].append')
self.istest(inspect.iscode, 'mod.spam.func_code')
self.istest(inspect.isframe, 'tb.tb_frame')
self.istest(inspect.isfunction, 'mod.spam')
self.istest(inspect.ismethod, 'mod.StupidGit.abuse')
self.istest(inspect.ismethod, 'git.argue')
self.istest(inspect.ismodule, 'mod')
self.istest(inspect.istraceback, 'tb')
self.istest(inspect.isdatadescriptor, '__builtin__.file.closed')
self.istest(inspect.isdatadescriptor, '__builtin__.file.softspace')
self.istest(inspect.isgenerator, '(x for x in xrange(2))')
self.istest(inspect.isgeneratorfunction, 'generator_function_example')
if hasattr(types, 'GetSetDescriptorType'):
self.istest(inspect.isgetsetdescriptor,
'type(tb.tb_frame).f_locals')
else:
self.assertFalse(inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals))
if hasattr(types, 'MemberDescriptorType'):
self.istest(inspect.ismemberdescriptor, 'datetime.timedelta.days')
else:
self.assertFalse(inspect.ismemberdescriptor(datetime.timedelta.days))
示例2: test_excluding_predicates
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isdatadescriptor [as 別名]
def test_excluding_predicates(self):
#self.istest(inspect.isbuiltin, 'sys.exit') # Not valid for Jython
self.istest(inspect.isbuiltin, '[].append')
self.istest(inspect.iscode, 'mod.spam.func_code')
self.istest(inspect.isframe, 'tb.tb_frame')
self.istest(inspect.isfunction, 'mod.spam')
self.istest(inspect.ismethod, 'mod.StupidGit.abuse')
self.istest(inspect.ismethod, 'git.argue')
self.istest(inspect.ismodule, 'mod')
self.istest(inspect.istraceback, 'tb')
self.istest(inspect.isdatadescriptor, '__builtin__.file.closed')
self.istest(inspect.isdatadescriptor, '__builtin__.file.softspace')
self.istest(inspect.isgenerator, '(x for x in xrange(2))')
self.istest(inspect.isgeneratorfunction, 'generator_function_example')
if hasattr(types, 'GetSetDescriptorType'):
self.istest(inspect.isgetsetdescriptor,
'type(tb.tb_frame).f_locals')
else:
self.assertFalse(inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals))
if hasattr(types, 'MemberDescriptorType'):
# Not valid for Jython
# self.istest(inspect.ismemberdescriptor, 'datetime.timedelta.days')
pass
else:
self.assertFalse(inspect.ismemberdescriptor(datetime.timedelta.days))
示例3: __init__
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isdatadescriptor [as 別名]
def __init__(cls, name, bases, dct):
def is_public_member(key):
return (
(key.startswith('__') and key.endswith('__')
and len(key) > 4) or
not key.startswith('_'))
for key, val in dct.items():
if ((inspect.isfunction(val) or inspect.isdatadescriptor(val)) and
is_public_member(key) and
val.__doc__ is None):
for base in cls.__mro__[1:]:
super_method = getattr(base, key, None)
if super_method is not None:
val.__doc__ = super_method.__doc__
break
super().__init__(name, bases, dct)
示例4: classify_class_attrs
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isdatadescriptor [as 別名]
def classify_class_attrs(object):
"""Wrap inspect.classify_class_attrs, with fixup for data descriptors."""
results = []
for (name, kind, cls, value) in inspect.classify_class_attrs(object):
if inspect.isdatadescriptor(value):
kind = 'data descriptor'
results.append((name, kind, cls, value))
return results
# ----------------------------------------------------- module manipulation
示例5: _get_exposed_property_value
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isdatadescriptor [as 別名]
def _get_exposed_property_value(obj: Any, propname: str, only_exposed: bool = True) -> Any:
"""
Return the value of an @exposed @property.
If the requested property is not a @property or not exposed,
an AttributeError is raised instead.
"""
v = getattr(obj.__class__, propname)
if inspect.isdatadescriptor(v):
if v.fget and getattr(v.fget, "_pyroExposed", not only_exposed):
return v.fget(obj)
raise AttributeError("attempt to access unexposed or unknown remote attribute '%s'" % propname)
示例6: _set_exposed_property_value
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isdatadescriptor [as 別名]
def _set_exposed_property_value(obj: Any, propname: str, value: Any, only_exposed: bool = True) -> Any:
"""
Sets the value of an @exposed @property.
If the requested property is not a @property or not exposed,
an AttributeError is raised instead.
"""
v = getattr(obj.__class__, propname)
if inspect.isdatadescriptor(v):
pfunc = v.fget or v.fset or v.fdel
if v.fset and getattr(pfunc, "_pyroExposed", not only_exposed):
return v.fset(obj, value)
raise AttributeError("attempt to access unexposed or unknown remote attribute '%s'" % propname)
示例7: classify_class_attrs
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isdatadescriptor [as 別名]
def classify_class_attrs(object):
"""Wrap inspect.classify_class_attrs, with fixup for data descriptors."""
def fixup(data):
name, kind, cls, value = data
if inspect.isdatadescriptor(value):
kind = 'data descriptor'
return name, kind, cls, value
return map(fixup, inspect.classify_class_attrs(object))
# ----------------------------------------------------- module manipulation
示例8: classify_class_attrs
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isdatadescriptor [as 別名]
def classify_class_attrs(object):
"""Wrap inspect.classify_class_attrs, with fixup for data descriptors."""
def fixup(data):
name, kind, cls, value = data
if inspect.isdatadescriptor(value):
kind = 'data descriptor'
return name, kind, cls, value
return map(fixup, inspect.classify_class_attrs(object))
# ----------------------------------------------------- Unicode support helpers
示例9: bind
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isdatadescriptor [as 別名]
def bind(self, target):
if self.func not in dir(target):
return None
funcs = []
if self.read_pattern is not None:
def getter():
return getattr(target, self.func)
# Copy docstring if target is a @property
prop = getattr(type(target), self.func, None)
if prop and inspect.isdatadescriptor(prop):
getter.__doc__ = 'Getter: ' + inspect.getdoc(prop)
funcs.append(
Func(getter, self.read_pattern, return_mapping=self.return_mapping, doc=self.doc))
if self.write_pattern is not None:
def setter(new_value):
setattr(target, self.func, new_value)
# Copy docstring if target is a @property
prop = getattr(type(target), self.func, None)
if prop and inspect.isdatadescriptor(prop):
setter.__doc__ = 'Setter: ' + inspect.getdoc(prop)
funcs.append(
Func(setter, self.write_pattern, argument_mappings=self.argument_mappings,
return_mapping=self.return_mapping, doc=self.doc))
return funcs
示例10: _extract_props
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isdatadescriptor [as 別名]
def _extract_props(cls):
return map(lambda e: e[0], getmembers(cls, predicate=isdatadescriptor))
示例11: test_excluding_predicates
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isdatadescriptor [as 別名]
def test_excluding_predicates(self):
global tb
self.istest(inspect.isbuiltin, 'sys.exit')
self.istest(inspect.isbuiltin, '[].append')
self.istest(inspect.iscode, 'mod.spam.__code__')
try:
1/0
except:
tb = sys.exc_info()[2]
self.istest(inspect.isframe, 'tb.tb_frame')
self.istest(inspect.istraceback, 'tb')
if hasattr(types, 'GetSetDescriptorType'):
self.istest(inspect.isgetsetdescriptor,
'type(tb.tb_frame).f_locals')
else:
self.assertFalse(inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals))
finally:
# Clear traceback and all the frames and local variables hanging to it.
tb = None
self.istest(inspect.isfunction, 'mod.spam')
self.istest(inspect.isfunction, 'mod.StupidGit.abuse')
self.istest(inspect.ismethod, 'git.argue')
self.istest(inspect.ismodule, 'mod')
self.istest(inspect.isdatadescriptor, 'collections.defaultdict.default_factory')
self.istest(inspect.isgenerator, '(x for x in range(2))')
self.istest(inspect.isgeneratorfunction, 'generator_function_example')
with warnings.catch_warnings():
warnings.simplefilter("ignore")
self.istest(inspect.iscoroutine, 'coroutine_function_example(1)')
self.istest(inspect.iscoroutinefunction, 'coroutine_function_example')
if hasattr(types, 'MemberDescriptorType'):
self.istest(inspect.ismemberdescriptor, 'datetime.timedelta.days')
else:
self.assertFalse(inspect.ismemberdescriptor(datetime.timedelta.days))
示例12: test_getmembers_descriptors
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isdatadescriptor [as 別名]
def test_getmembers_descriptors(self):
class A(object):
dd = _BrokenDataDescriptor()
md = _BrokenMethodDescriptor()
def pred_wrapper(pred):
# A quick'n'dirty way to discard standard attributes of new-style
# classes.
class Empty(object):
pass
def wrapped(x):
if '__name__' in dir(x) and hasattr(Empty, x.__name__):
return False
return pred(x)
return wrapped
ismethoddescriptor = pred_wrapper(inspect.ismethoddescriptor)
isdatadescriptor = pred_wrapper(inspect.isdatadescriptor)
self.assertEqual(inspect.getmembers(A, ismethoddescriptor),
[('md', A.__dict__['md'])])
self.assertEqual(inspect.getmembers(A, isdatadescriptor),
[('dd', A.__dict__['dd'])])
class B(A):
pass
self.assertEqual(inspect.getmembers(B, ismethoddescriptor),
[('md', A.__dict__['md'])])
self.assertEqual(inspect.getmembers(B, isdatadescriptor),
[('dd', A.__dict__['dd'])])
示例13: _get_fields
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isdatadescriptor [as 別名]
def _get_fields(cls, base_fields=True) -> List[JsonSchemaField]:
def _get_fields_uncached():
dataclass_bases = [
klass for klass in cls.__bases__ if is_dataclass(klass) and issubclass(klass, JsonSchemaMixin)
]
base_fields_types = set()
for base in dataclass_bases:
base_fields_types |= {(f.name, f.type) for f in fields(base)}
mapped_fields = []
type_hints = get_type_hints(cls)
for f in fields(cls):
# Skip internal fields
if f.name.startswith("__") or (not base_fields and (f.name, f.type) in base_fields_types):
continue
# Note fields() doesn't resolve forward refs
f.type = type_hints[f.name]
mapped_fields.append(JsonSchemaField(f, cls.field_mapping().get(f.name, f.name)))
if cls.__serialise_properties:
include_properties = None
if isinstance(cls.__serialise_properties, tuple):
include_properties = set(cls.__serialise_properties)
members = inspect.getmembers(cls, inspect.isdatadescriptor)
for name, member in members:
if name != "__weakref__" and (include_properties is None or name in include_properties):
f = Field(MISSING, None, None, None, None, None, None)
f.name = name
f.type = member.fget.__annotations__['return']
mapped_fields.append(JsonSchemaField(f, name, is_property=True))
return mapped_fields
if not base_fields:
return _get_fields_uncached()
if not cls.__mapped_fields:
cls.__mapped_fields = _get_fields_uncached()
return cls.__mapped_fields # type: ignore
示例14: classify_class_attrs
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isdatadescriptor [as 別名]
def classify_class_attrs(object):
"""Wrap inspect.classify_class_attrs, with fixup for data descriptors."""
results = []
for (name, kind, cls, value) in inspect.classify_class_attrs(object):
if inspect.isdatadescriptor(value):
kind = 'data descriptor'
results.append((name, kind, cls, value))
return results
示例15: is_descriptor
# 需要導入模塊: import inspect [as 別名]
# 或者: from inspect import isdatadescriptor [as 別名]
def is_descriptor(self) -> bool:
"""Determine if initial method provided is a descriptor."""
method = self.methods[0][0]
instance = self._get_instance(method)
if instance:
attr = inspect.getattr_static(instance, self._name)
return inspect.isdatadescriptor(attr)
return False