[pypy-svn] r75688 - in pypy/branch/fast-forward/pypy/module/operator: . test

benjamin at codespeak.net benjamin at codespeak.net
Wed Jun 30 17:52:52 CEST 2010


Author: benjamin
Date: Wed Jun 30 17:52:51 2010
New Revision: 75688

Modified:
   pypy/branch/fast-forward/pypy/module/operator/__init__.py
   pypy/branch/fast-forward/pypy/module/operator/app_operator.py
   pypy/branch/fast-forward/pypy/module/operator/test/test_operator.py
Log:
implement operator.methodcaller

Modified: pypy/branch/fast-forward/pypy/module/operator/__init__.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/operator/__init__.py	(original)
+++ pypy/branch/fast-forward/pypy/module/operator/__init__.py	Wed Jun 30 17:52:51 2010
@@ -21,7 +21,7 @@
                  'countOf', 'delslice', 'getslice', 'indexOf',
                  'isMappingType', 'isNumberType', 'isSequenceType',
                  'repeat', 'setslice',
-                 'attrgetter', 'itemgetter'
+                 'attrgetter', 'itemgetter', 'methodcaller',
              ]
 
     for name in app_names:

Modified: pypy/branch/fast-forward/pypy/module/operator/app_operator.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/operator/app_operator.py	(original)
+++ pypy/branch/fast-forward/pypy/module/operator/app_operator.py	Wed Jun 30 17:52:51 2010
@@ -80,7 +80,7 @@
             return tuple(list)
 
         return result
-    
+
 class itemgetter(object):
 
     def __init__(self, item, *args):
@@ -96,3 +96,12 @@
 
         return result
 
+class methodcaller(object):
+
+    def __init__(self, method_name, *args, **kwargs):
+        self.method_name = method_name
+        self.args = args
+        self.kwargs = kwargs
+
+    def __call__(self, obj):
+        return getattr(obj, self.method_name)(*self.args, **self.kwargs)

Modified: pypy/branch/fast-forward/pypy/module/operator/test/test_operator.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/operator/test/test_operator.py	(original)
+++ pypy/branch/fast-forward/pypy/module/operator/test/test_operator.py	Wed Jun 30 17:52:51 2010
@@ -171,3 +171,13 @@
         assert operator.irepeat(a, 1) is a
         assert a == [0, 1, 2, 0, 1, 2]
 
+    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)



More information about the Pypy-commit mailing list