当前位置: 首页>>代码示例>>Python>>正文


Python six.create_unbound_method方法代码示例

本文整理汇总了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 
开发者ID:openstack,项目名称:designate,代码行数:34,代码来源:utils.py

示例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 
开发者ID:benjaminp,项目名称:six,代码行数:14,代码来源:test_six.py

示例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 } 
开发者ID:python-xlib,项目名称:python-xlib,代码行数:43,代码来源:display.py

示例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 
开发者ID:NiklasRosenstein,项目名称:c4ddev,代码行数:14,代码来源:test_six.py


注:本文中的six.create_unbound_method方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。