本文整理汇总了Python中types.DynamicClassAttribute方法的典型用法代码示例。如果您正苦于以下问题:Python types.DynamicClassAttribute方法的具体用法?Python types.DynamicClassAttribute怎么用?Python types.DynamicClassAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类types
的用法示例。
在下文中一共展示了types.DynamicClassAttribute方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_property___isabstractmethod__descriptor
# 需要导入模块: import types [as 别名]
# 或者: from types import DynamicClassAttribute [as 别名]
def test_property___isabstractmethod__descriptor(self):
for val in (True, False, [], [1], '', '1'):
class C(object):
def foo(self):
pass
foo.__isabstractmethod__ = val
foo = DynamicClassAttribute(foo)
self.assertIs(C.__dict__['foo'].__isabstractmethod__, bool(val))
# check that the DynamicClassAttribute's __isabstractmethod__ descriptor does the
# right thing when presented with a value that fails truth testing:
class NotBool(object):
def __bool__(self):
raise ValueError()
__len__ = __bool__
with self.assertRaises(ValueError):
class C(object):
def foo(self):
pass
foo.__isabstractmethod__ = NotBool()
foo = DynamicClassAttribute(foo)
示例2: test_DynamicClassAttribute
# 需要导入模块: import types [as 别名]
# 或者: from types import DynamicClassAttribute [as 别名]
def test_DynamicClassAttribute(self):
class Meta(type):
def __getattr__(self, name):
if name == 'ham':
return 'spam'
return super().__getattr__(name)
class DA(metaclass=Meta):
@types.DynamicClassAttribute
def ham(self):
return 'eggs'
expected_text_data_docstrings = tuple('\n | ' + s if s else ''
for s in expected_data_docstrings)
output = StringIO()
helper = pydoc.Helper(output=output)
helper(DA)
expected_text = expected_dynamicattribute_pattern % (
(__name__,) + expected_text_data_docstrings[:2])
result = output.getvalue().strip()
self.assertEqual(expected_text, result)
示例3: test_DynamicClassAttribute
# 需要导入模块: import types [as 别名]
# 或者: from types import DynamicClassAttribute [as 别名]
def test_DynamicClassAttribute(self):
class Meta(type):
def __getattr__(self, name):
if name == 'ham':
return 'spam'
return super().__getattr__(name)
class DA(metaclass=Meta):
@types.DynamicClassAttribute
def ham(self):
return 'eggs'
expected_text_data_docstrings = tuple('\n | ' + s if s else ''
for s in expected_data_docstrings)
output = StringIO()
helper = pydoc.Helper(output=output)
helper(DA)
expected_text = expected_dynamicattribute_pattern % (
(__name__,) + expected_text_data_docstrings[:2])
result = output.getvalue().strip()
if result != expected_text:
print_diffs(expected_text, result)
self.fail("outputs are not equal, see diff above")
示例4: __reduce_ex__
# 需要导入模块: import types [as 别名]
# 或者: from types import DynamicClassAttribute [as 别名]
def __reduce_ex__(self, proto):
return self.__class__, (self._value_, )
# DynamicClassAttribute is used to provide access to the `name` and
# `value` properties of enum members while keeping some measure of
# protection from modification, while still allowing for an enumeration
# to have members named `name` and `value`. This works because enumeration
# members are not set directly on the enum class -- __getattr__ is
# used to look them up.
示例5: test_abstract_virtual
# 需要导入模块: import types [as 别名]
# 或者: from types import DynamicClassAttribute [as 别名]
def test_abstract_virtual(self):
self.assertRaises(TypeError, ClassWithAbstractVirtualProperty)
self.assertRaises(TypeError, ClassWithPropertyAbstractVirtual)
class APV(ClassWithPropertyAbstractVirtual):
pass
self.assertRaises(TypeError, APV)
class AVP(ClassWithAbstractVirtualProperty):
pass
self.assertRaises(TypeError, AVP)
class Okay1(ClassWithAbstractVirtualProperty):
@DynamicClassAttribute
def color(self):
return self._color
def __init__(self):
self._color = 'cyan'
with self.assertRaises(AttributeError):
Okay1.color
self.assertEqual(Okay1().color, 'cyan')
class Okay2(ClassWithAbstractVirtualProperty):
@DynamicClassAttribute
def color(self):
return self._color
def __init__(self):
self._color = 'magenta'
with self.assertRaises(AttributeError):
Okay2.color
self.assertEqual(Okay2().color, 'magenta')
# Issue 5890: subclasses of DynamicClassAttribute do not preserve method __doc__ strings
示例6: test_docstring_copy
# 需要导入模块: import types [as 别名]
# 或者: from types import DynamicClassAttribute [as 别名]
def test_docstring_copy(self):
class Foo(object):
@PropertySub
def spam(self):
"""spam wrapped in DynamicClassAttribute subclass"""
return 1
self.assertEqual(
Foo.__dict__['spam'].__doc__,
"spam wrapped in DynamicClassAttribute subclass")
示例7: test_classify_DynamicClassAttribute
# 需要导入模块: import types [as 别名]
# 或者: from types import DynamicClassAttribute [as 别名]
def test_classify_DynamicClassAttribute(self):
class Meta(type):
def __getattr__(self, name):
if name == 'ham':
return 'spam'
return super().__getattr__(name)
class VA(metaclass=Meta):
@types.DynamicClassAttribute
def ham(self):
return 'eggs'
should_find_dca = inspect.Attribute('ham', 'data', VA, VA.__dict__['ham'])
self.assertIn(should_find_dca, inspect.classify_class_attrs(VA))
should_find_ga = inspect.Attribute('ham', 'data', Meta, 'spam')
self.assertIn(should_find_ga, inspect.classify_class_attrs(VA))
示例8: test_getmembers_VirtualAttribute
# 需要导入模块: import types [as 别名]
# 或者: from types import DynamicClassAttribute [as 别名]
def test_getmembers_VirtualAttribute(self):
class M(type):
def __getattr__(cls, name):
if name == 'eggs':
return 'scrambled'
return super().__getattr__(name)
class A(metaclass=M):
@types.DynamicClassAttribute
def eggs(self):
return 'spam'
self.assertIn(('eggs', 'scrambled'), inspect.getmembers(A))
self.assertIn(('eggs', 'spam'), inspect.getmembers(A()))
示例9: test_classVirtualAttribute
# 需要导入模块: import types [as 别名]
# 或者: from types import DynamicClassAttribute [as 别名]
def test_classVirtualAttribute(self):
class Thing(object):
@types.DynamicClassAttribute
def x(self):
return self._x
_x = object()
self.assertEqual(inspect.getattr_static(Thing, 'x'), Thing.__dict__['x'])