本文整理汇总了Python中abc.ABCMeta方法的典型用法代码示例。如果您正苦于以下问题:Python abc.ABCMeta方法的具体用法?Python abc.ABCMeta怎么用?Python abc.ABCMeta使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类abc
的用法示例。
在下文中一共展示了abc.ABCMeta方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: challenge_meta
# 需要导入模块: import abc [as 别名]
# 或者: from abc import ABCMeta [as 别名]
def challenge_meta(attributes):
"""
Returns a metaclass that will introduce the given attributes into the class
namespace.
Args:
attributes: The dictionary of attributes
Returns:
The metaclass described above
"""
class ChallengeMeta(ABCMeta):
def __new__(cls, name, bases, attr):
attrs = dict(attr)
attrs.update(attributes)
return super().__new__(cls, name, bases, attrs)
return ChallengeMeta
示例2: test_cache_leak
# 需要导入模块: import abc [as 别名]
# 或者: from abc import ABCMeta [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)
示例3: test_slots_descriptor
# 需要导入模块: import abc [as 别名]
# 或者: from abc import ABCMeta [as 别名]
def test_slots_descriptor(self):
# Issue2115: slot descriptors did not correctly check
# the type of the given object
import abc
class MyABC:
__metaclass__ = abc.ABCMeta
__slots__ = "a"
class Unrelated(object):
pass
MyABC.register(Unrelated)
u = Unrelated()
self.assertIsInstance(u, MyABC)
# This used to crash
self.assertRaises(TypeError, MyABC.a.__set__, u, 3)
示例4: test_registration_basics
# 需要导入模块: import abc [as 别名]
# 或者: from abc import ABCMeta [as 别名]
def test_registration_basics(self):
class A:
__metaclass__ = abc.ABCMeta
class B(object):
pass
b = B()
self.assertFalse(issubclass(B, A))
self.assertFalse(issubclass(B, (A,)))
self.assertNotIsInstance(b, A)
self.assertNotIsInstance(b, (A,))
A.register(B)
self.assertTrue(issubclass(B, A))
self.assertTrue(issubclass(B, (A,)))
self.assertIsInstance(b, A)
self.assertIsInstance(b, (A,))
class C(B):
pass
c = C()
self.assertTrue(issubclass(C, A))
self.assertTrue(issubclass(C, (A,)))
self.assertIsInstance(c, A)
self.assertIsInstance(c, (A,))
示例5: test_registration_edge_cases
# 需要导入模块: import abc [as 别名]
# 或者: from abc import ABCMeta [as 别名]
def test_registration_edge_cases(self):
class A:
__metaclass__ = abc.ABCMeta
A.register(A) # should pass silently
class A1(A):
pass
self.assertRaises(RuntimeError, A1.register, A) # cycles not allowed
class B(object):
pass
A1.register(B) # ok
A1.register(B) # should pass silently
class C(A):
pass
A.register(C) # should pass silently
self.assertRaises(RuntimeError, C.register, A) # cycles not allowed
C.register(B) # ok
示例6: __new__
# 需要导入模块: import abc [as 别名]
# 或者: from abc import ABCMeta [as 别名]
def __new__(mcls, name, bases, namespace, **kwargs):
cls = abc.ABCMeta.__new__(mcls, name, bases, namespace, **kwargs)
samplermixins = {name
for name, value in namespace.items()
if getattr(value, "__issamplemixin__", False)}
if len(samplermixins) == 3:
abstracts = samplermixins
else:
abstracts = set()
for base in bases:
samplermixins = {name
for name in getattr(base, "__abstractmethods__", set())
if getattr(getattr(cls, name, None), "__issamplemixin__", False)}
if len(samplermixins) == 3:
abstracts.update(samplermixins)
# if we found any, update abstract methods
if abstracts:
cls.__abstractmethods__ = frozenset(abstracts.union(cls.__abstractmethods__))
return cls
示例7: _save_config_yaml
# 需要导入模块: import abc [as 别名]
# 或者: from abc import ABCMeta [as 别名]
def _save_config_yaml(output_dir, config):
file_name = 'config.yaml'
config_dict = _easy_dict_to_dict(config)
file_path = os.path.join(output_dir, file_name)
class Dumper(yaml.Dumper):
def ignore_aliases(self, data):
return True
Dumper.add_representer(ABCMeta, Representer.represent_name)
if type(config_dict['CLASSES']) != list:
DatasetClass = config.DATASET_CLASS
dataset_kwargs = dict((key.lower(), val) for key, val in config.DATASET.items())
train_dataset = DatasetClass(
subset="train",
**dataset_kwargs,
)
config_dict['CLASSES'] = train_dataset.classes
with gfile.GFile(os.path.join(output_dir, file_name), 'w') as outfile:
yaml.dump(config_dict, outfile, default_flow_style=False, Dumper=Dumper)
return file_path
示例8: test_slots_descriptor
# 需要导入模块: import abc [as 别名]
# 或者: from abc import ABCMeta [as 别名]
def test_slots_descriptor(self):
# Issue2115: slot descriptors did not correctly check
# the type of the given object
import abc
class MyABC(metaclass=abc.ABCMeta):
__slots__ = "a"
class Unrelated(object):
pass
MyABC.register(Unrelated)
u = Unrelated()
self.assertIsInstance(u, MyABC)
# This used to crash
self.assertRaises(TypeError, MyABC.a.__set__, u, 3)
示例9: test_abstractproperty_basics
# 需要导入模块: import abc [as 别名]
# 或者: from abc import ABCMeta [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))
示例10: test_abstractclassmethod_basics
# 需要导入模块: import abc [as 别名]
# 或者: from abc import ABCMeta [as 别名]
def test_abstractclassmethod_basics(self):
@abc.abstractclassmethod
def foo(cls): pass
self.assertTrue(foo.__isabstractmethod__)
@classmethod
def bar(cls): pass
self.assertFalse(getattr(bar, "__isabstractmethod__", False))
class C(metaclass=abc.ABCMeta):
@abc.abstractclassmethod
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')
示例11: test_abstractstaticmethod_basics
# 需要导入模块: import abc [as 别名]
# 或者: from abc import ABCMeta [as 别名]
def test_abstractstaticmethod_basics(self):
@staticmethod
@abc.abstractmethod
def foo(): pass
self.assertTrue(foo.__isabstractmethod__)
@staticmethod
def bar(): pass
self.assertFalse(getattr(bar, "__isabstractmethod__", False))
class C(metaclass=abc.ABCMeta):
@staticmethod
@abc.abstractmethod
def foo(): return 3
self.assertRaises(TypeError, C)
class D(C):
@staticmethod
def foo(): return 4
self.assertEqual(D.foo(), 4)
self.assertEqual(D().foo(), 4)
示例12: test_registration_basics
# 需要导入模块: import abc [as 别名]
# 或者: from abc import ABCMeta [as 别名]
def test_registration_basics(self):
class A(metaclass=abc.ABCMeta):
pass
class B(object):
pass
b = B()
self.assertFalse(issubclass(B, A))
self.assertFalse(issubclass(B, (A,)))
self.assertNotIsInstance(b, A)
self.assertNotIsInstance(b, (A,))
B1 = A.register(B)
self.assertTrue(issubclass(B, A))
self.assertTrue(issubclass(B, (A,)))
self.assertIsInstance(b, A)
self.assertIsInstance(b, (A,))
self.assertIs(B1, B)
class C(B):
pass
c = C()
self.assertTrue(issubclass(C, A))
self.assertTrue(issubclass(C, (A,)))
self.assertIsInstance(c, A)
self.assertIsInstance(c, (A,))
示例13: test_register_as_class_deco
# 需要导入模块: import abc [as 别名]
# 或者: from abc import ABCMeta [as 别名]
def test_register_as_class_deco(self):
class A(metaclass=abc.ABCMeta):
pass
@A.register
class B(object):
pass
b = B()
self.assertTrue(issubclass(B, A))
self.assertTrue(issubclass(B, (A,)))
self.assertIsInstance(b, A)
self.assertIsInstance(b, (A,))
@A.register
class C(B):
pass
c = C()
self.assertTrue(issubclass(C, A))
self.assertTrue(issubclass(C, (A,)))
self.assertIsInstance(c, A)
self.assertIsInstance(c, (A,))
self.assertIs(C, A.register(C))
示例14: test_registration_edge_cases
# 需要导入模块: import abc [as 别名]
# 或者: from abc import ABCMeta [as 别名]
def test_registration_edge_cases(self):
class A(metaclass=abc.ABCMeta):
pass
A.register(A) # should pass silently
class A1(A):
pass
self.assertRaises(RuntimeError, A1.register, A) # cycles not allowed
class B(object):
pass
A1.register(B) # ok
A1.register(B) # should pass silently
class C(A):
pass
A.register(C) # should pass silently
self.assertRaises(RuntimeError, C.register, A) # cycles not allowed
C.register(B) # ok
示例15: test_isabstract
# 需要导入模块: import abc [as 别名]
# 或者: from abc import ABCMeta [as 别名]
def test_isabstract(self):
from abc import ABCMeta, abstractmethod
class AbstractClassExample(metaclass=ABCMeta):
@abstractmethod
def foo(self):
pass
class ClassExample(AbstractClassExample):
def foo(self):
pass
a = ClassExample()
# Test general behaviour.
self.assertTrue(inspect.isabstract(AbstractClassExample))
self.assertFalse(inspect.isabstract(ClassExample))
self.assertFalse(inspect.isabstract(a))
self.assertFalse(inspect.isabstract(int))
self.assertFalse(inspect.isabstract(5))