python类Attribute()的实例源码

test_interface.py 文件源码 项目:hostapd-mana 作者: adde88 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def test___doc___element(self):
        class I(Interface):
            "xxx"

        self.assertEqual(I.__doc__, "xxx")
        self.assertEqual(list(I), [])

        class I(Interface):
            "xxx"

            __doc__ = Attribute('the doc')

        self.assertEqual(I.__doc__, "")
        self.assertEqual(list(I), ['__doc__'])
test_interface.py 文件源码 项目:hostapd-mana 作者: adde88 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def test_invariant_as_decorator():
        """Invaiants can be deined in line

          >>> class IRange(interface.Interface):
          ...     min = interface.Attribute("Lower bound")
          ...     max = interface.Attribute("Upper bound")
          ...
          ...     @interface.invariant
          ...     def range_invariant(ob):
          ...         if ob.max < ob.min:
          ...             raise Invalid('max < min')


          >>> class Range(object):
          ...     interface.implements(IRange)
          ...
          ...     def __init__(self, min, max):
          ...         self.min, self.max = min, max

          >>> IRange.validateInvariants(Range(1,2))
          >>> IRange.validateInvariants(Range(1,1))
          >>> IRange.validateInvariants(Range(2,1))
          Traceback (most recent call last):
          ...
          Invalid: max < min


        """
test_verify.py 文件源码 项目:hostapd-mana 作者: adde88 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def testMethodForAttr(self):

        class IFoo(Interface):
             foo = Attribute("The foo Attribute")


        class Foo:
             implements(IFoo)

             def foo(self):
                 pass

        verifyClass(IFoo, Foo)
test_schema.py 文件源码 项目:guillotina 作者: plone 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def test_schema_wo_fields(self):
        from zope.interface import Interface
        from zope.interface import Attribute

        class INoFields(Interface):
            def method():
                pass
            attr = Attribute('ignoreme')

        errors = self._callFUT(INoFields, object())
        self.assertEqual(len(errors), 0)
test_interface.py 文件源码 项目:sslstrip-hsts-openwrt 作者: adde88 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def test___doc___element(self):
        class I(Interface):
            "xxx"

        self.assertEqual(I.__doc__, "xxx")
        self.assertEqual(list(I), [])

        class I(Interface):
            "xxx"

            __doc__ = Attribute('the doc')

        self.assertEqual(I.__doc__, "")
        self.assertEqual(list(I), ['__doc__'])
test_interface.py 文件源码 项目:sslstrip-hsts-openwrt 作者: adde88 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def test_invariant_as_decorator():
        """Invaiants can be deined in line

          >>> class IRange(interface.Interface):
          ...     min = interface.Attribute("Lower bound")
          ...     max = interface.Attribute("Upper bound")
          ...
          ...     @interface.invariant
          ...     def range_invariant(ob):
          ...         if ob.max < ob.min:
          ...             raise Invalid('max < min')


          >>> class Range(object):
          ...     interface.implements(IRange)
          ...
          ...     def __init__(self, min, max):
          ...         self.min, self.max = min, max

          >>> IRange.validateInvariants(Range(1,2))
          >>> IRange.validateInvariants(Range(1,1))
          >>> IRange.validateInvariants(Range(2,1))
          Traceback (most recent call last):
          ...
          Invalid: max < min


        """
test_verify.py 文件源码 项目:sslstrip-hsts-openwrt 作者: adde88 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def testMethodForAttr(self):

        class IFoo(Interface):
             foo = Attribute("The foo Attribute")


        class Foo:
             implements(IFoo)

             def foo(self):
                 pass

        verifyClass(IFoo, Foo)
test_interface.py 文件源码 项目:AskTanmay-NLQA-System- 作者: tanmayb123 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def test_get_hit_from_base_wo__v_attrs(self):
        from zope.interface.interface import Attribute
        from zope.interface.interface import Interface
        class IFoo(Interface):
            foo = Attribute('foo')
        class IBar(Interface):
            bar = Attribute('bar')
        spec = self._makeOne([IFoo, IBar])
        self.assertTrue(spec.get('foo') is IFoo.get('foo'))
        self.assertTrue(spec.get('bar') is IBar.get('bar'))
test_interface.py 文件源码 项目:AskTanmay-NLQA-System- 作者: tanmayb123 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_ctor_w_attrs_attrib_methods(self):
        from zope.interface.interface import Attribute
        from zope.interface.interface import fromFunction
        def _bar():
            """DOCSTRING"""
        ATTRS = {'foo': Attribute('Foo', ''),
                 'bar': fromFunction(_bar),
                }
        klass = self._getTargetClass()
        inst = klass('ITesting', attrs=ATTRS)
        self.assertEqual(inst.__name__, 'ITesting')
        self.assertEqual(inst.__doc__, '')
        self.assertEqual(inst.__bases__, ())
        self.assertEqual(inst.names(), ATTRS.keys())
test_interface.py 文件源码 项目:AskTanmay-NLQA-System- 作者: tanmayb123 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def test_names_w_all_False_ignores_bases(self):
        from zope.interface.interface import Attribute
        from zope.interface.interface import fromFunction
        def _bar():
            """DOCSTRING"""
        BASE_ATTRS = {'foo': Attribute('Foo', ''),
                      'bar': fromFunction(_bar),
                     }
        DERIVED_ATTRS = {'baz': Attribute('Baz', ''),
                        }
        base = self._makeOne('IBase', attrs=BASE_ATTRS)
        derived = self._makeOne('IDerived', bases=(base,), attrs=DERIVED_ATTRS)
        self.assertEqual(sorted(derived.names(all=False)), ['baz'])
test_interface.py 文件源码 项目:AskTanmay-NLQA-System- 作者: tanmayb123 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def test_names_w_all_True_no_bases(self):
        from zope.interface.interface import Attribute
        from zope.interface.interface import fromFunction
        def _bar():
            """DOCSTRING"""
        ATTRS = {'foo': Attribute('Foo', ''),
                 'bar': fromFunction(_bar),
                }
        one = self._makeOne(attrs=ATTRS)
        self.assertEqual(sorted(one.names(all=True)), ['bar', 'foo'])
test_interface.py 文件源码 项目:AskTanmay-NLQA-System- 作者: tanmayb123 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def test_names_w_all_True_w_bases_simple(self):
        from zope.interface.interface import Attribute
        from zope.interface.interface import fromFunction
        def _bar():
            """DOCSTRING"""
        BASE_ATTRS = {'foo': Attribute('Foo', ''),
                      'bar': fromFunction(_bar),
                     }
        DERIVED_ATTRS = {'baz': Attribute('Baz', ''),
                        }
        base = self._makeOne('IBase', attrs=BASE_ATTRS)
        derived = self._makeOne('IDerived', bases=(base,), attrs=DERIVED_ATTRS)
        self.assertEqual(sorted(derived.names(all=True)), ['bar', 'baz', 'foo'])
test_interface.py 文件源码 项目:AskTanmay-NLQA-System- 作者: tanmayb123 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def test_namesAndDescriptions_w_all_False_ignores_bases(self):
        from zope.interface.interface import Attribute
        from zope.interface.interface import fromFunction
        def _bar():
            """DOCSTRING"""
        BASE_ATTRS = {'foo': Attribute('Foo', ''),
                      'bar': fromFunction(_bar),
                     }
        DERIVED_ATTRS = {'baz': Attribute('Baz', ''),
                        }
        base = self._makeOne('IBase', attrs=BASE_ATTRS)
        derived = self._makeOne('IDerived', bases=(base,), attrs=DERIVED_ATTRS)
        self.assertEqual(sorted(derived.namesAndDescriptions(all=False)),
                        [('baz', DERIVED_ATTRS['baz']),
                        ])
test_interface.py 文件源码 项目:AskTanmay-NLQA-System- 作者: tanmayb123 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def test_namesAndDescriptions_w_all_True_no_bases(self):
        from zope.interface.interface import Attribute
        from zope.interface.interface import fromFunction
        def _bar():
            """DOCSTRING"""
        ATTRS = {'foo': Attribute('Foo', ''),
                 'bar': fromFunction(_bar),
                }
        one = self._makeOne(attrs=ATTRS)
        self.assertEqual(sorted(one.namesAndDescriptions(all=False)),
                        [('bar', ATTRS['bar']),
                         ('foo', ATTRS['foo']),
                        ])
test_interface.py 文件源码 项目:AskTanmay-NLQA-System- 作者: tanmayb123 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def test___getitem___hit(self):
        from zope.interface.interface import Attribute
        from zope.interface.interface import fromFunction
        def _bar():
            """DOCSTRING"""
        ATTRS = {'foo': Attribute('Foo', ''),
                 'bar': fromFunction(_bar),
                }
        one = self._makeOne(attrs=ATTRS)
        self.assertEqual(one['foo'], ATTRS['foo'])
        self.assertEqual(one['bar'], ATTRS['bar'])
test_interface.py 文件源码 项目:AskTanmay-NLQA-System- 作者: tanmayb123 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def test___contains___hit(self):
        from zope.interface.interface import Attribute
        from zope.interface.interface import fromFunction
        def _bar():
            """DOCSTRING"""
        ATTRS = {'foo': Attribute('Foo', ''),
                 'bar': fromFunction(_bar),
                }
        one = self._makeOne(attrs=ATTRS)
        self.assertTrue('foo' in one)
        self.assertTrue('bar' in one)
test_interface.py 文件源码 项目:AskTanmay-NLQA-System- 作者: tanmayb123 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def test_queryDescriptionFor_hit(self):
        from zope.interface import Attribute
        ATTRS = {'attr': Attribute('Title', 'Description')}
        iface = self._makeOne(attrs=ATTRS)
        self.assertEqual(iface.queryDescriptionFor('attr'), ATTRS['attr'])
test_interface.py 文件源码 项目:AskTanmay-NLQA-System- 作者: tanmayb123 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def test_attributes_link_to_interface(self):
        from zope.interface import Interface
        from zope.interface import Attribute

        class I1(Interface):
            attr = Attribute("My attr")

        self.assertTrue(I1['attr'].interface is I1)
test_interface.py 文件源码 项目:AskTanmay-NLQA-System- 作者: tanmayb123 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def test_names_simple(self):
        from zope.interface import Attribute
        from zope.interface import Interface


        class ISimple(Interface):
            attr = Attribute(u'My attr')

            def method():
                pass

        self.assertEqual(sorted(ISimple.names()), ['attr', 'method'])
test_interface.py 文件源码 项目:AskTanmay-NLQA-System- 作者: tanmayb123 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def test_getDescriptionFor_derived(self):
        from zope.interface import Attribute
        from zope.interface.interface import Method
        from zope.interface import Interface


        class IBase(Interface):
            attr = Attribute(u'My attr')

            def method():
                "My method"

        class IDerived(IBase):
            attr2 = Attribute(u'My attr2')

            def method():
                "My method, overridden"

            def method2():
                "My method2"

        a_desc = IDerived.getDescriptionFor('attr')
        self.assertTrue(isinstance(a_desc, Attribute))
        self.assertEqual(a_desc.__name__, 'attr')
        self.assertEqual(a_desc.__doc__, 'My attr')

        m_desc = IDerived.getDescriptionFor('method')
        self.assertTrue(isinstance(m_desc, Method))
        self.assertEqual(m_desc.__name__, 'method')
        self.assertEqual(m_desc.__doc__, 'My method, overridden')

        a2_desc = IDerived.getDescriptionFor('attr2')
        self.assertTrue(isinstance(a2_desc, Attribute))
        self.assertEqual(a2_desc.__name__, 'attr2')
        self.assertEqual(a2_desc.__doc__, 'My attr2')

        m2_desc = IDerived.getDescriptionFor('method2')
        self.assertTrue(isinstance(m2_desc, Method))
        self.assertEqual(m2_desc.__name__, 'method2')
        self.assertEqual(m2_desc.__doc__, 'My method2')


问题


面经


文章

微信
公众号

扫码关注公众号