本文整理汇总了Python中abc.abstractmethod方法的典型用法代码示例。如果您正苦于以下问题:Python abc.abstractmethod方法的具体用法?Python abc.abstractmethod怎么用?Python abc.abstractmethod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类abc
的用法示例。
在下文中一共展示了abc.abstractmethod方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_ignores_return_in_abstract_method_sphinx
# 需要导入模块: import abc [as 别名]
# 或者: from abc import abstractmethod [as 别名]
def test_ignores_return_in_abstract_method_sphinx(self):
"""Example of an abstract method documenting the return type that an
implementation should return.
"""
node = astroid.extract_node("""
import abc
class Foo(object):
@abc.abstractmethod
def foo(self): #@
'''docstring ...
:returns: Ten
:rtype: int
'''
return 10
""")
with self.assertNoMessages():
self.checker.visit_functiondef(node)
示例2: test_ignores_return_in_abstract_method_google
# 需要导入模块: import abc [as 别名]
# 或者: from abc import abstractmethod [as 别名]
def test_ignores_return_in_abstract_method_google(self):
"""Example of an abstract method documenting the return type that an
implementation should return.
"""
node = astroid.extract_node("""
import abc
class Foo(object):
@abc.abstractmethod
def foo(self): #@
'''docstring ...
Returns:
int: Ten
'''
return 10
""")
with self.assertNoMessages():
self.checker.visit_functiondef(node)
示例3: test_ignores_return_in_abstract_method_numpy
# 需要导入模块: import abc [as 别名]
# 或者: from abc import abstractmethod [as 别名]
def test_ignores_return_in_abstract_method_numpy(self):
"""Example of an abstract method documenting the return type that an
implementation should return.
"""
node = astroid.extract_node("""
import abc
class Foo(object):
@abc.abstractmethod
def foo(self): #@
'''docstring ...
Returns
-------
int
Ten
'''
return 10
""")
with self.assertNoMessages():
self.checker.visit_functiondef(node)
示例4: test_cache_leak
# 需要导入模块: import abc [as 别名]
# 或者: from abc import abstractmethod [as 别名]
def test_cache_leak(self):
# See issue #2521.
class A(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def f(self):
pass
class C(A):
def f(self):
A.f(self)
r = weakref.ref(C)
# Trigger cache.
C().f()
del C
test_support.gc_collect()
self.assertEqual(r(), None)
示例5: test_abstractproperty_basics
# 需要导入模块: import abc [as 别名]
# 或者: from abc import abstractmethod [as 别名]
def test_abstractproperty_basics(self):
@property
@abc.abstractmethod
def foo(self): pass
self.assertTrue(foo.__isabstractmethod__)
def bar(self): pass
self.assertFalse(getattr(bar, "__isabstractmethod__", False))
class C(metaclass=abc.ABCMeta):
@property
@abc.abstractmethod
def foo(self): return 3
self.assertRaises(TypeError, C)
class D(C):
@C.foo.getter
def foo(self): return super().foo
self.assertEqual(D().foo, 3)
示例6: test_abstractclassmethod_basics
# 需要导入模块: import abc [as 别名]
# 或者: from abc import abstractmethod [as 别名]
def test_abstractclassmethod_basics(self):
@classmethod
@abc.abstractmethod
def foo(cls): pass
self.assertTrue(foo.__isabstractmethod__)
@classmethod
def bar(cls): pass
self.assertFalse(getattr(bar, "__isabstractmethod__", False))
class C(metaclass=abc.ABCMeta):
@classmethod
@abc.abstractmethod
def foo(cls): return cls.__name__
self.assertRaises(TypeError, C)
class D(C):
@classmethod
def foo(cls): return super().foo()
self.assertEqual(D.foo(), 'D')
self.assertEqual(D().foo(), 'D')
示例7: convert
# 需要导入模块: import abc [as 别名]
# 或者: from abc import abstractmethod [as 别名]
def convert(self, operator: OperatorBase) -> OperatorBase:
r"""
Convert a ``SummedOp`` into a ``ComposedOp`` or ``CircuitOp`` representing an
approximation of e^-i*``op_sum``.
Args:
operator: The ``SummedOp`` to evolve.
Returns:
The Operator approximating op_sum's evolution.
Raises:
TypeError: A non-SummedOps Operator is passed into ``convert``.
"""
raise NotImplementedError
# TODO @abstractmethod - trotter_error_bound
示例8: __init__
# 需要导入模块: import abc [as 别名]
# 或者: from abc import abstractmethod [as 别名]
def __init__(self, protocol, chunk_size=None):
self.chunk_size = chunk_size or CHUNK_SIZE # The number of items to send in a single request
if not isinstance(self.chunk_size, int):
raise ValueError("'chunk_size' %r must be an integer" % chunk_size)
if self.chunk_size < 1:
raise ValueError("'chunk_size' must be a positive number")
self.protocol = protocol
# The following two methods are the minimum required to be implemented by subclasses, but the name and number of
# kwargs differs between services. Therefore, we cannot make these methods abstract.
# @abc.abstractmethod
# def call(self, **kwargs):
# raise NotImplementedError()
# @abc.abstractmethod
# def get_payload(self, **kwargs):
# raise NotImplementedError()
示例9: test_postcondition_in_abstract_class_method
# 需要导入模块: import abc [as 别名]
# 或者: from abc import abstractmethod [as 别名]
def test_postcondition_in_abstract_class_method(self) -> None:
class Abstract(icontract.DBC):
@classmethod
@abc.abstractmethod
@icontract.ensure(lambda result: result != 0)
def some_func(cls: Type['Abstract'], x: int) -> int:
pass
class SomeClass(Abstract):
@classmethod
def some_func(cls: Type['SomeClass'], x: int) -> int:
return x
result = SomeClass.some_func(x=1)
self.assertEqual(1, result)
violation_error = None # type: Optional[icontract.ViolationError]
try:
_ = SomeClass.some_func(x=0)
except icontract.ViolationError as err:
violation_error = err
self.assertIsNotNone(violation_error)
self.assertEqual('result != 0: result was 0', tests.error.wo_mandatory_location(str(violation_error)))
示例10: test_abstract_method
# 需要导入模块: import abc [as 别名]
# 或者: from abc import abstractmethod [as 别名]
def test_abstract_method(self) -> None:
class A(icontract.DBC):
@icontract.ensure(lambda result: result < 100)
@abc.abstractmethod
def func(self) -> int:
pass
class B(A):
def func(self) -> int:
return 1000
b = B()
violation_error = None # type: Optional[icontract.ViolationError]
try:
b.func()
except icontract.ViolationError as err:
violation_error = err
self.assertIsNotNone(violation_error)
self.assertEqual("result < 100: result was 1000", tests.error.wo_mandatory_location(str(violation_error)))
示例11: test_abstract_method_not_implemented
# 需要导入模块: import abc [as 别名]
# 或者: from abc import abstractmethod [as 别名]
def test_abstract_method_not_implemented(self) -> None:
# pylint: disable=abstract-method
class A(icontract.DBC):
@icontract.ensure(lambda result: result < 100)
@abc.abstractmethod
def func(self) -> int:
pass
class B(A):
pass
type_err = None # type: Optional[TypeError]
try:
_ = B() # type: ignore
except TypeError as err:
type_err = err
self.assertIsNotNone(type_err)
self.assertEqual("Can't instantiate abstract class B with abstract methods func", str(type_err))
示例12: test_abstract_method
# 需要导入模块: import abc [as 别名]
# 或者: from abc import abstractmethod [as 别名]
def test_abstract_method(self) -> None:
class A(icontract.DBC):
@icontract.require(lambda x: x > 0)
@abc.abstractmethod
def func(self, x: int) -> int:
pass
class B(A):
def func(self, x: int) -> int:
return 1000
b = B()
violation_error = None # type: Optional[icontract.ViolationError]
try:
b.func(x=-1)
except icontract.ViolationError as err:
violation_error = err
self.assertIsNotNone(violation_error)
self.assertEqual("x > 0: x was -1", tests.error.wo_mandatory_location(str(violation_error)))
示例13: setUpClass
# 需要导入模块: import abc [as 别名]
# 或者: from abc import abstractmethod [as 别名]
def setUpClass(cls):
FinalABCMeta = final_meta_factory(ABCMeta)
class ABCWithFinal(with_metaclass(FinalABCMeta, object)):
a = final('ABCWithFinal: a')
b = 'ABCWithFinal: b'
@final
def f(self):
return 'ABCWithFinal: f'
def g(self):
return 'ABCWithFinal: g'
@abstractmethod
def h(self):
raise NotImplementedError('h')
cls.class_ = ABCWithFinal
示例14: setUpClass
# 需要导入模块: import abc [as 别名]
# 或者: from abc import abstractmethod [as 别名]
def setUpClass(cls):
FinalABCMeta = compose_types(FinalMeta, ABCMeta)
class ABCWithFinal(with_metaclass(FinalABCMeta, object)):
a = final('ABCWithFinal: a')
b = 'ABCWithFinal: b'
@final
def f(self):
return 'ABCWithFinal: f'
def g(self):
return 'ABCWithFinal: g'
@abstractmethod
def h(self):
raise NotImplementedError('h')
cls.class_ = ABCWithFinal
示例15: __store_annotations__
# 需要导入模块: import abc [as 别名]
# 或者: from abc import abstractmethod [as 别名]
def __store_annotations__(self,zooniverse_id,max_users=float("inf"),expert_markings=False):
"""
override the parent method so that we can apply ROIs
read through and return all of the relevant annotations associated with the given zooniverse_id
:param zooniverse_id: id of the subject
:param max_users: maximum number of classifications to read in
:param expert_markings: do we want to read in markings from experts - either yes or no, shouldn't mix
:return:
"""
if not(zooniverse_id in self.roi_dict):
self.roi_dict[zooniverse_id] = self.__get_roi__(zooniverse_id)
self.current_roi = self.roi_dict[zooniverse_id]
OuroborosAPI.__store_annotations__(self,zooniverse_id,max_users,expert_markings)
self.current_roi = None
# @abc.abstractmethod
# def __classification_to_markings__(self,classification,roi):
# """
# This is the main function projects will have to override - given a set of annotations, we need to return the list
# of all markings in that annotation
# """
# return []