def test_methodcaller(self):
        from _operator import methodcaller

        class X(object):
            def method(self, arg1=2, arg2=3):
                return arg1, arg2

        x = X()
        assert methodcaller("method")(x) == (2, 3)
        assert methodcaller("method", 4)(x) == (4, 3)
        assert methodcaller("method", 4, 5)(x) == (4, 5)
        assert methodcaller("method", 4, arg2=42)(x) == (4, 42)
    def test_methodcaller_self(self):
        from operator import methodcaller

        class X:
            def method(myself, self):
                return self * 6

        assert methodcaller("method", self=7)(X()) == 42
Esempio n. 3
0
 def _MessageToJsonObject(self, message):
     """Converts message to an object according to Proto3 JSON Specification."""
     try:
         message_descriptor = message.DESCRIPTOR
     except:
         message_descriptor = message
     full_name = message_descriptor.full_name
     if _IsWrapperMessage(message_descriptor):
         return self._WrapperMessageToJsonObject(message)
     if full_name in _WKTJSONMETHODS:
         return methodcaller(_WKTJSONMETHODS[full_name][0], message)(self)
     js = {}
     return self._RegularMessageToJsonObject(message, js)
 class A(object):
     getx = operator.attrgetter('x')
     get3 = operator.itemgetter(3)
     callx = operator.methodcaller("append", "x")
 def test_repr_methodcaller(self):
     import _operator as operator
     assert repr(operator.methodcaller(
         "foo", "bar",
         baz=42)) == ("operator.methodcaller('foo', 'bar', baz=42)")