當前位置: 首頁>>代碼示例>>Python>>正文


Python six.get_method_function方法代碼示例

本文整理匯總了Python中six.get_method_function方法的典型用法代碼示例。如果您正苦於以下問題:Python six.get_method_function方法的具體用法?Python six.get_method_function怎麽用?Python six.get_method_function使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在six的用法示例。


在下文中一共展示了six.get_method_function方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: subscribe

# 需要導入模塊: import six [as 別名]
# 或者: from six import get_method_function [as 別名]
def subscribe(func):
    '''
    Add a subscriber function to option events

    Parameters
    ----------
    func : callable
        A callable object that takes two parameters: key and value.
        This function is called with the name and value of any option
        that is set.

    Returns
    -------
    None

    '''
    if isinstance(func, types.MethodType):
        obj = six.get_method_self(func)
        func = six.get_method_function(func)
        _subscribers[func] = (weakref.ref(func), weakref.ref(obj))
    else:
        _subscribers[func] = (weakref.ref(func), None) 
開發者ID:sassoftware,項目名稱:python-esppy,代碼行數:24,代碼來源:config.py

示例2: wrapper

# 需要導入模塊: import six [as 別名]
# 或者: from six import get_method_function [as 別名]
def wrapper(self):
        if not self._wrapped:
            if self._instance or self._class:
                wrapped = self._func.__get__(self._instance, self._class)

                if isinstance(self._func, staticmethod):
                    # we don't need instance or class, however we need scope
                    self.cache.scope = self._instance or self._class
                    self._instance = None
                    self._class = None
                else:
                    wrapped = six.get_method_function(wrapped)
            else:
                wrapped = self._func

            update_wrapper(self.cache, wrapped)
            self.cache.function = wrapped
            self.cache.instance = self._instance
            self.cache.klass = self._class
            self._wrapped = True

        return self.cache 
開發者ID:Bahus,項目名稱:easy_cache,代碼行數:24,代碼來源:decorators.py

示例3: _get_op_handler

# 需要導入模塊: import six [as 別名]
# 或者: from six import get_method_function [as 別名]
def _get_op_handler(operation):
        """ Import and load the operation handler """
        try:
            mod_to_import, attr_path = operation.split('#')
            op = import_module(mod_to_import)
            for part in attr_path.split('.'):
                op = getattr(op, part)
            if isinstance(op, types.FunctionType):
                return op
            return six.get_method_function(op)
        except (ValueError, AttributeError):
            raise ValueError("The operation '{}' is invalid.".format(operation)) 
開發者ID:microsoft,項目名稱:knack,代碼行數:14,代碼來源:commands.py

示例4: _find_method

# 需要導入模塊: import six [as 別名]
# 或者: from six import get_method_function [as 別名]
def _find_method(obj, func):
    if obj:
        try:
            func_self = six.get_method_self(func)
        except AttributeError:  # func has no __self__
            pass
        else:
            if func_self is obj:
                return six.get_method_function(func).__name__
    raise ValueError("Function %s is not a method of: %s" % (func, obj)) 
開發者ID:DarkSand,項目名稱:fetchman,代碼行數:12,代碼來源:reqser.py

示例5: _find_method

# 需要導入模塊: import six [as 別名]
# 或者: from six import get_method_function [as 別名]
def _find_method(obj, func):
    if obj:
        try:
            func_self = six.get_method_self(func)
        except AttributeError:  # func has no __self__
            pass
        else:
            if func_self is obj:
                name = six.get_method_function(func).__name__
                if _is_private_method(name):
                    return _mangle_private_name(obj, func, name)
                return name
    raise ValueError("Function %s is not a method of: %s" % (func, obj)) 
開發者ID:wistbean,項目名稱:learn_python3_spider,代碼行數:15,代碼來源:reqser.py

示例6: unbind

# 需要導入模塊: import six [as 別名]
# 或者: from six import get_method_function [as 別名]
def unbind(func):
    """Get Function from Method (if not already Function)."""
    if isinstance(func, types.MethodType):
        func = six.get_method_function(func)
    return func 
開發者ID:mintel,項目名稱:pytest-localstack,代碼行數:7,代碼來源:utils.py

示例7: test_get_method_function

# 需要導入模塊: import six [as 別名]
# 或者: from six import get_method_function [as 別名]
def test_get_method_function():
    class X(object):
        def m(self):
            pass
    x = X()
    assert six.get_method_function(x.m) is X.__dict__["m"]
    pytest.raises(AttributeError, six.get_method_function, hasattr) 
開發者ID:benjaminp,項目名稱:six,代碼行數:9,代碼來源:test_six.py

示例8: __get_unbound_function

# 需要導入模塊: import six [as 別名]
# 或者: from six import get_method_function [as 別名]
def __get_unbound_function(method):
        try:
            return six.get_method_function(method)
        except AttributeError:
            return method 
開發者ID:google,項目名稱:dotty,代碼行數:7,代碼來源:dispatch.py

示例9: test_get_method_function

# 需要導入模塊: import six [as 別名]
# 或者: from six import get_method_function [as 別名]
def test_get_method_function():
    class X(object):
        def m(self):
            pass
    x = X()
    assert six.get_method_function(x.m) is X.__dict__["m"]
    py.test.raises(AttributeError, six.get_method_function, hasattr) 
開發者ID:NiklasRosenstein,項目名稱:c4ddev,代碼行數:9,代碼來源:test_six.py

示例10: __details__

# 需要導入模塊: import six [as 別名]
# 或者: from six import get_method_function [as 別名]
def __details__(self):
            with utils.patch(
                six.get_method_function(self.tasks.service.destroy),
                '__doc__',
                self.__doc__ + (self.tasks.service.destroy.__doc__ or ''),
            ):
                return get_task_details(self.tasks.service.destroy) 
開發者ID:renskiy,項目名稱:fabricio,代碼行數:9,代碼來源:tasks.py

示例11: object_build

# 需要導入模塊: import six [as 別名]
# 或者: from six import get_method_function [as 別名]
def object_build(self, node, obj):
        """recursive method which create a partial ast from real objects
         (only function, class, and method are handled)
        """
        if obj in self._done:
            return self._done[obj]
        self._done[obj] = node
        for name in dir(obj):
            try:
                member = getattr(obj, name)
            except AttributeError:
                # damned ExtensionClass.Base, I know you're there !
                attach_dummy_node(node, name)
                continue
            if inspect.ismethod(member):
                member = six.get_method_function(member)
            if inspect.isfunction(member):
                _build_from_function(node, name, member, self._module)
            elif inspect.isbuiltin(member):
                if (not _io_discrepancy(member) and
                        self.imported_member(node, member, name)):
                    continue
                object_build_methoddescriptor(node, member, name)
            elif inspect.isclass(member):
                if self.imported_member(node, member, name):
                    continue
                if member in self._done:
                    class_node = self._done[member]
                    if class_node not in node.locals.get(name, ()):
                        node.add_local_node(class_node, name)
                else:
                    class_node = object_build_class(node, member, name)
                    # recursion
                    self.object_build(class_node, member)
                if name == '__class__' and class_node.parent is None:
                    class_node.parent = self._done[self._module]
            elif inspect.ismethoddescriptor(member):
                assert isinstance(member, object)
                object_build_methoddescriptor(node, member, name)
            elif inspect.isdatadescriptor(member):
                assert isinstance(member, object)
                object_build_datadescriptor(node, member, name)
            elif isinstance(member, _CONSTANTS):
                attach_const_node(node, name, member)
            elif inspect.isroutine(member):
                # This should be called for Jython, where some builtin
                # methods aren't caught by isbuiltin branch.
                _build_from_function(node, name, member, self._module)
            else:
                # create an empty node so that the name is actually defined
                attach_dummy_node(node, name, member)
        return None 
開發者ID:AtomLinter,項目名稱:linter-pylama,代碼行數:54,代碼來源:raw_building.py

示例12: _get_observer_key_value

# 需要導入模塊: import six [as 別名]
# 或者: from six import get_method_function [as 別名]
def _get_observer_key_value(observer):
        """
        Subscribing methods of objects is tricky::

            class TheObserver(object):
                def __init__(self):
                    self.received_data = []

                def on_new_data(self, data):
                    self.received_data.append(data)

            observer1 = TheObserver()
            observer2 = TheObserver()

            subscribe(observer1.on_new_data)
            subscribe(observer2.on_new_data)
            subscribe(observer2.on_new_data)

        Even if it looks like 2 different subscriptions they all
        pass 3 different bound-method objects (different id()).
        So, to differentiate them we need to "unwind" out of them:
        1) self                      - 2 different id()
        2) function object of class  - all 3 have same id()

        Observer key is pair: (self-id, function-id)
        """
        try:
            self_or_none = six.get_method_self(observer)
            self_id = instance_id(self_or_none)
            self_or_none = weakref.proxy(self_or_none)
        except AttributeError:
            self_id = 0  # default for not bound methods
            self_or_none = None

        try:
            func = six.get_method_function(observer)
        except AttributeError:
            func = observer
        function_id = instance_id(func)

        observer_key = (self_id, function_id)
        observer_value = (self_or_none, weakref.proxy(func))
        return observer_key, observer_value 
開發者ID:nokia,項目名稱:moler,代碼行數:45,代碼來源:threaded_moler_connection.py

示例13: _get_subscriber_key_and_value

# 需要導入模塊: import six [as 別名]
# 或者: from six import get_method_function [as 別名]
def _get_subscriber_key_and_value(subscriber):
        """
        Allow Subscribers to be garbage collected while still subscribed inside Publisher

        Subscribing methods of objects is tricky::

            class TheObserver(object):
                def __init__(self):
                    self.received_data = []

                def on_new_data(self, data):
                    self.received_data.append(data)

            observer1 = TheObserver()
            observer2 = TheObserver()

            subscribe(observer1.on_new_data)
            subscribe(observer2.on_new_data)
            subscribe(observer2.on_new_data)

        Even if it looks like 2 different subscriptions they all
        pass 3 different bound-method objects (different id()).
        This is so since access via observer1.on_new_data
        creates new object (bound method) on the fly.

        We want to use weakref but weakref to bound method doesn't work
        see: http://code.activestate.com/recipes/81253/
        and : https://stackoverflow.com/questions/599430/why-doesnt-the-weakref-work-on-this-bound-method
        When we wrap bound-method into weakref it may quickly disappear
        if that is only reference to bound method.
        So, we need to unbind it to have access to real method + self instance

        Unbinding above 3 examples of on_new_data will give:
        1) self                      - 2 different id()
        2) function object of class  - all 3 have same id()

        Observer key is pair: (self-id, function-id)
        """
        try:
            self_or_none = six.get_method_self(subscriber)
            self_id = instance_id(self_or_none)
            self_or_none = weakref.proxy(self_or_none)
        except AttributeError:
            self_id = 0  # default for not bound methods
            self_or_none = None

        try:
            func = six.get_method_function(subscriber)
        except AttributeError:
            func = subscriber
        function_id = instance_id(func)

        subscription_key = (self_id, function_id)
        subscription_value = (self_or_none, weakref.proxy(func))
        return subscription_key, subscription_value 
開發者ID:nokia,項目名稱:moler,代碼行數:57,代碼來源:publisher.py


注:本文中的six.get_method_function方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。