Python six.get_unbound_function() Examples

The following are 10 code examples of six.get_unbound_function(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module six , or try the search function .
Example #1
Source File: test_stub_service.py    From pysoa with Apache License 2.0 6 votes vote down vote up
def test_one_stub_duplicated_as_context_manager(self):
        original_client_send_request = Client.send_request
        original_client_get_all_responses = Client.get_all_responses

        stub = stub_action('test_service', 'test_action_1', body={'value': 1})

        with stub as stub_test_action_1, stub as second_stub_test_action_1:
            response = self.client.call_action('test_service', 'test_action_1')

        # Check stub is correctly reverted
        for client_func, original_func in (
            (Client.send_request, original_client_send_request),
            (Client.get_all_responses, original_client_get_all_responses),
        ):
            self.assertTrue(
                six.get_unbound_function(client_func) is six.get_unbound_function(original_func)  # type: ignore
            )

        self.assertEqual({'value': 1}, response.body)

        self.assertTrue(stub_test_action_1 is second_stub_test_action_1)
        self.assertEqual(1, stub_test_action_1.call_count)
        self.assertEqual({}, stub_test_action_1.call_body)
        stub_test_action_1.assert_called_once_with({}) 
Example #2
Source File: test_stub_service.py    From pysoa with Apache License 2.0 6 votes vote down vote up
def test_one_stub_duplicated_as_context_manager(self):
        original_client_send_request = Client.send_request
        original_client_get_all_responses = Client.get_all_responses

        stub = stub_action('test_service', 'test_action_1', body={'value': 1})

        with stub as stub_test_action_1, stub as second_stub_test_action_1:
            response = self.client.call_action('test_service', 'test_action_1')

        # Check stub is correctly reverted
        for client_func, original_func in (
            (Client.send_request, original_client_send_request),
            (Client.get_all_responses, original_client_get_all_responses),
        ):
            self.assertTrue(
                six.get_unbound_function(client_func) is six.get_unbound_function(original_func)  # type: ignore
            )

        self.assertEqual({'value': 1}, response.body)

        self.assertTrue(stub_test_action_1 is second_stub_test_action_1)
        self.assertEqual(1, stub_test_action_1.call_count)
        self.assertEqual({}, stub_test_action_1.call_body)
        stub_test_action_1.assert_called_once_with({}) 
Example #3
Source File: __init__.py    From D-VAE with MIT License 5 votes vote down vote up
def get_unbound_function(unbound):
        # Op.make_thunk isn't bound, so don't have a __func__ attr.
        # But bound method, have a __func__ method that point to the
        # not bound method. That is what we want.
        if hasattr(unbound, '__func__'):
            return unbound.__func__
        return unbound 
Example #4
Source File: autodoc.py    From drf_tweaks with MIT License 5 votes vote down vote up
def autodoc(base_doc="", classess=DEFAULT_CLASSESS, add_classess=None, skip_classess=None):
    def copy_method(cls, method_name, method):
        """ create facade for a method with preservation of original docstring """
        @wraps(method)
        def shadow_method(self, *args, **kwargs):
            return method(self, *args, **kwargs)
        shadow_method.__doc__ = method.__doc__
        setattr(cls, method_name, shadow_method)

    def wrapped(cls):
        applies_to = set([])
        classess_to_apply = [c for c in classess if not skip_classess or c not in skip_classess]
        if add_classess:
            classess_to_apply += [c for c in add_classess if not skip_classess or c not in skip_classess]

        for autodoc_class in classess_to_apply:
            applies_to |= set(autodoc_class.applies_to)

        # Create facades for original methods - docstring of methods are immutable, so we need to change docstring of
        # functions. But without shadowing the method.__func__ will point to the same function for classes that
        # inherits them from the same parents.
        for method_name in applies_to:
            method = getattr(cls, method_name, None)
            if method:
                copy_method(cls, method_name, method)

        # update docstrings
        for autodoc_class in classess_to_apply:
            for method_name in autodoc_class.applies_to:
                method = getattr(cls, method_name, None)
                if method:
                    six.get_unbound_function(method).__doc__ = \
                        autodoc_class.update_docstring(cls, base_doc, six.get_unbound_function(method).__doc__,
                                                       method_name)
        return cls
    return wrapped 
Example #5
Source File: __init__.py    From attention-lvcsr with MIT License 5 votes vote down vote up
def get_unbound_function(unbound):
        # Op.make_thunk isn't bound, so don't have a __func__ attr.
        # But bound method, have a __func__ method that point to the
        # not bound method. That is what we want.
        if hasattr(unbound, '__func__'):
            return unbound.__func__
        return unbound 
Example #6
Source File: test_six.py    From six with MIT License 5 votes vote down vote up
def test_get_unbound_function():
    class X(object):
        def m(self):
            pass
    assert six.get_unbound_function(X.m) is X.__dict__["m"] 
Example #7
Source File: test_six.py    From c4ddev with MIT License 5 votes vote down vote up
def test_get_unbound_function():
    class X(object):
        def m(self):
            pass
    assert six.get_unbound_function(X.m) is X.__dict__["m"] 
Example #8
Source File: test_six.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def test_get_unbound_function():
    class X(object):
        def m(self):
            pass
    assert six.get_unbound_function(X.m) is X.__dict__["m"] 
Example #9
Source File: test_six.py    From data with GNU General Public License v3.0 5 votes vote down vote up
def test_get_unbound_function():
    class X(object):
        def m(self):
            pass
    assert six.get_unbound_function(X.m) is X.__dict__["m"] 
Example #10
Source File: element.py    From nerodia with MIT License 5 votes vote down vote up
def __getattr__(self, name):
        if name in (_[0] for _ in getmembers(UserEditable, predicate=isroutine)) and \
                self.is_content_editable:
            self._content_editable = True
            setattr(self, name, six.create_bound_method(
                six.get_unbound_function(getattr(UserEditable, name)), self))
            return getattr(self, name)
        else:
            raise AttributeError("Element '{}' has no attribute "
                                 "'{}'".format(self.__class__.__name__.capitalize(), name))