本文整理汇总了Python中six.create_unbound_method方法的典型用法代码示例。如果您正苦于以下问题:Python six.create_unbound_method方法的具体用法?Python six.create_unbound_method怎么用?Python six.create_unbound_method使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six
的用法示例。
在下文中一共展示了six.create_unbound_method方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parameterized_class
# 需要导入模块: import six [as 别名]
# 或者: from six import create_unbound_method [as 别名]
def parameterized_class(cls):
"""A class decorator for running parameterized test cases.
Mark your class with @parameterized_class.
Mark your test cases with @parameterized.
"""
test_functions = {
k: v for k, v in vars(cls).items() if k.startswith('test')
}
for name, f in test_functions.items():
if not hasattr(f, '_test_data'):
continue
# remove the original test function from the class
delattr(cls, name)
# add a new test function to the class for each entry in f._test_data
for tag, args in f._test_data.items():
new_name = "{0}_{1}".format(f.__name__, tag)
if hasattr(cls, new_name):
raise Exception(
"Parameterized test case '{0}.{1}' created from '{0}.{2}' "
"already exists".format(cls.__name__, new_name, name))
# Using `def new_method(self): f(self, **args)` is not sufficient
# (all new_methods use the same args value due to late binding).
# Instead, use this factory function.
new_method = def_method(f, **args)
# To add a method to a class, available for all instances:
# MyClass.method = types.MethodType(f, None, MyClass)
setattr(cls, new_name, six.create_unbound_method(new_method, cls))
return cls
示例2: test_create_unbound_method
# 需要导入模块: import six [as 别名]
# 或者: from six import create_unbound_method [as 别名]
def test_create_unbound_method():
class X(object):
pass
def f(self):
return self
u = six.create_unbound_method(f, X)
pytest.raises(TypeError, u)
if six.PY2:
assert isinstance(u, types.MethodType)
x = X()
assert f(x) is x
示例3: extension_add_method
# 需要导入模块: import six [as 别名]
# 或者: from six import create_unbound_method [as 别名]
def extension_add_method(self, object, name, function):
"""extension_add_method(object, name, function)
Add an X extension module method. OBJECT is the type of
object to add the function to, a string from this list:
display
resource
drawable
window
pixmap
fontable
font
gc
colormap
cursor
NAME is the name of the method, a string. FUNCTION is a
normal function whose first argument is a 'self'.
"""
if object == 'display':
if hasattr(self, name):
raise AssertionError('attempting to replace display method: %s' % name)
self.display_extension_methods[name] = function
else:
class_list = (object, ) + _resource_hierarchy.get(object, ())
for class_name in class_list:
cls = _resource_baseclasses[class_name]
if hasattr(cls, name):
raise AssertionError('attempting to replace %s method: %s' % (class_name, name))
method = create_unbound_method(function, cls)
# Maybe should check extension overrides too
try:
self.class_extension_dicts[class_name][name] = method
except KeyError:
self.class_extension_dicts[class_name] = { name: method }
示例4: test_create_unbound_method
# 需要导入模块: import six [as 别名]
# 或者: from six import create_unbound_method [as 别名]
def test_create_unbound_method():
class X(object):
pass
def f(self):
return self
u = six.create_unbound_method(f, X)
py.test.raises(TypeError, u)
if six.PY2:
assert isinstance(u, types.MethodType)
x = X()
assert f(x) is x