当前位置: 首页>>代码示例>>Python>>正文


Python types.DynamicClassAttribute方法代码示例

本文整理汇总了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) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:23,代码来源:test_dynamicclassattribute.py

示例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) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:21,代码来源:test_pydoc.py

示例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") 
开发者ID:IronLanguages,项目名称:ironpython3,代码行数:23,代码来源:test_pydoc.py

示例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. 
开发者ID:Relph1119,项目名称:GraphicDesignPatternByPython,代码行数:11,代码来源:enum.py

示例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 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:32,代码来源:test_dynamicclassattribute.py

示例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") 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:11,代码来源:test_dynamicclassattribute.py

示例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)) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:16,代码来源:test_inspect.py

示例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())) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:14,代码来源:test_inspect.py

示例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']) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:10,代码来源:test_inspect.py


注:本文中的types.DynamicClassAttribute方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。