本文整理匯總了Python中abc.abstractproperty方法的典型用法代碼示例。如果您正苦於以下問題:Python abc.abstractproperty方法的具體用法?Python abc.abstractproperty怎麽用?Python abc.abstractproperty使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類abc
的用法示例。
在下文中一共展示了abc.abstractproperty方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _create_property
# 需要導入模塊: import abc [as 別名]
# 或者: from abc import abstractproperty [as 別名]
def _create_property(property_name,
docstring=None,
readonly=False,
required=False):
def getter(self):
return self.__getattribute__('_' + property_name)
def setter(self, value):
self.__setattr__('_' + property_name, value)
if readonly is True:
setter = None
if required is False:
absprop = None
else:
absprop = abstractproperty(None)
return property(fget=getter, fset=setter, doc=docstring), absprop
示例2: verify_interface
# 需要導入模塊: import abc [as 別名]
# 或者: from abc import abstractproperty [as 別名]
def verify_interface(iface, klass):
for method in iface.__abstractmethods__:
if not hasattr(klass, method):
raise InterfaceNotImplemented(
"{0} is missing a {1!r} method".format(klass, method)
)
if isinstance(getattr(iface, method), abc.abstractproperty):
# Can't properly verify these yet.
continue
sig = signature(getattr(iface, method))
actual = signature(getattr(klass, method))
if sig != actual:
raise InterfaceNotImplemented(
"{0}.{1}'s signature differs from the expected. Expected: "
"{2!r}. Received: {3!r}".format(
klass, method, sig, actual
)
)
示例3: verify_interface
# 需要導入模塊: import abc [as 別名]
# 或者: from abc import abstractproperty [as 別名]
def verify_interface(iface, klass):
for method in iface.__abstractmethods__:
if not hasattr(klass, method):
raise InterfaceNotImplemented(
"{0} is missing a {1!r} method".format(klass, method)
)
if isinstance(getattr(iface, method), abc.abstractproperty):
# Can't properly verify these yet.
continue
sig = signature(getattr(iface, method))
actual = signature(getattr(klass, method))
if sig != actual:
raise InterfaceNotImplemented(
"{0}.{1}'s signature differs from the expected. Expected: "
"{2!r}. Received: {3!r}".format(
klass, method, sig, actual
)
)
# No longer needed as of 2.2, but retained because we have external consumers
# who use it.
示例4: verify_interface
# 需要導入模塊: import abc [as 別名]
# 或者: from abc import abstractproperty [as 別名]
def verify_interface(iface, klass):
for method in iface.__abstractmethods__:
if not hasattr(klass, method):
raise InterfaceNotImplemented(
"{} is missing a {!r} method".format(klass, method)
)
if isinstance(getattr(iface, method), abc.abstractproperty):
# Can't properly verify these yet.
continue
sig = signature(getattr(iface, method))
actual = signature(getattr(klass, method))
if sig != actual:
raise InterfaceNotImplemented(
"{}.{}'s signature differs from the expected. Expected: "
"{!r}. Received: {!r}".format(
klass, method, sig, actual
)
)
# No longer needed as of 2.2, but retained because we have external consumers
# who use it.
示例5: test_abstractproperty_basics
# 需要導入模塊: import abc [as 別名]
# 或者: from abc import abstractproperty [as 別名]
def test_abstractproperty_basics(self):
@abc.abstractproperty
def foo(self): pass
self.assertTrue(foo.__isabstractmethod__)
def bar(self): pass
self.assertFalse(hasattr(bar, "__isabstractmethod__"))
class C(metaclass=abc.ABCMeta):
@abc.abstractproperty
def foo(self): return 3
self.assertRaises(TypeError, C)
class D(C):
@property
def foo(self): return super().foo
self.assertEqual(D().foo, 3)
self.assertFalse(getattr(D.foo, "__isabstractmethod__", False))
示例6: code_size
# 需要導入模塊: import abc [as 別名]
# 或者: from abc import abstractproperty [as 別名]
def code_size(self):
"""Return the size of each code.
This number is a constant and should agree with the output of the `encode`
op (e.g. if rel_codes is the output of self.encode(...), then it should have
shape [N, code_size()]). This abstractproperty should be overridden by
implementations.
Returns:
an integer constant
"""
pass
示例7: test_property_names
# 需要導入模塊: import abc [as 別名]
# 或者: from abc import abstractproperty [as 別名]
def test_property_names(self):
# If a method is annotated with @property, its name should
# match the attr regex. Since by default the attribute regex is the same
# as the method regex, we override it here.
methods = astroid.extract_node(
"""
import abc
def custom_prop(f):
return property(f)
class FooClass(object):
@property
def FOO(self): #@
pass
@property
def bar(self): #@
pass
@abc.abstractproperty
def BAZ(self): #@
pass
@custom_prop
def QUX(self): #@
pass
"""
)
with self.assertNoMessages():
self.checker.visit_functiondef(methods[0])
self.checker.visit_functiondef(methods[2])
self.checker.visit_functiondef(methods[3])
with self.assertAddsMessages(
Message(
"invalid-name",
node=methods[1],
args=("Attribute", "bar", "'[A-Z]+' pattern"),
)
):
self.checker.visit_functiondef(methods[1])
示例8: test_abstractproperty_basics
# 需要導入模塊: import abc [as 別名]
# 或者: from abc import abstractproperty [as 別名]
def test_abstractproperty_basics(self):
@abc.abstractproperty
def foo(self): pass
self.assertTrue(foo.__isabstractmethod__)
def bar(self): pass
self.assertFalse(hasattr(bar, "__isabstractmethod__"))
class C:
__metaclass__ = abc.ABCMeta
@abc.abstractproperty
def foo(self): return 3
class D(C):
@property
def foo(self): return super(D, self).foo
self.assertEqual(D().foo, 3)
示例9: test_abstractmethod_integration
# 需要導入模塊: import abc [as 別名]
# 或者: from abc import abstractproperty [as 別名]
def test_abstractmethod_integration(self):
for abstractthing in [abc.abstractmethod, abc.abstractproperty]:
class C:
__metaclass__ = abc.ABCMeta
@abstractthing
def foo(self): pass # abstract
def bar(self): pass # concrete
self.assertEqual(C.__abstractmethods__, set(["foo"]))
self.assertRaises(TypeError, C) # because foo is abstract
self.assertTrue(isabstract(C))
class D(C):
def bar(self): pass # concrete override of concrete
self.assertEqual(D.__abstractmethods__, set(["foo"]))
self.assertRaises(TypeError, D) # because foo is still abstract
self.assertTrue(isabstract(D))
class E(D):
def foo(self): pass
self.assertEqual(E.__abstractmethods__, set())
E() # now foo is concrete, too
self.assertFalse(isabstract(E))
class F(E):
@abstractthing
def bar(self): pass # abstract override of concrete
self.assertEqual(F.__abstractmethods__, set(["bar"]))
self.assertRaises(TypeError, F) # because bar is abstract now
self.assertTrue(isabstract(F))