[ADD] composition function utility
bzr revid: xmo@openerp.com-20130925145205-vumu9cgr5kob0900
This commit is contained in:
parent
c36bd85c27
commit
2837c1dc27
|
@ -22,6 +22,7 @@ import test_translate
|
||||||
import test_uninstall
|
import test_uninstall
|
||||||
import test_view_validation
|
import test_view_validation
|
||||||
import test_qweb
|
import test_qweb
|
||||||
|
import test_func
|
||||||
# This need a change in `oe run-tests` to only run fast_suite + checks by default.
|
# This need a change in `oe run-tests` to only run fast_suite + checks by default.
|
||||||
# import test_xmlrpc
|
# import test_xmlrpc
|
||||||
|
|
||||||
|
@ -43,6 +44,7 @@ checks = [
|
||||||
test_osv,
|
test_osv,
|
||||||
test_translate,
|
test_translate,
|
||||||
test_qweb,
|
test_qweb,
|
||||||
|
test_func,
|
||||||
]
|
]
|
||||||
|
|
||||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import functools
|
||||||
|
import unittest2
|
||||||
|
|
||||||
|
from ..tools.func import compose
|
||||||
|
|
||||||
|
class TestCompose(unittest2.TestCase):
|
||||||
|
def test_basic(self):
|
||||||
|
str_add = compose(str, lambda a, b: a + b)
|
||||||
|
self.assertEqual(
|
||||||
|
str_add(1, 2),
|
||||||
|
"3")
|
||||||
|
|
||||||
|
def test_decorator(self):
|
||||||
|
""" ensure compose() can be partially applied as a decorator
|
||||||
|
"""
|
||||||
|
@functools.partial(compose, unicode)
|
||||||
|
def mul(a, b):
|
||||||
|
return a * b
|
||||||
|
|
||||||
|
self.assertEqual(mul(5, 42), u"210")
|
||||||
|
|
|
@ -57,4 +57,19 @@ def frame_codeinfo(fframe, back=0):
|
||||||
except Exception:
|
except Exception:
|
||||||
return "<unknown>", ''
|
return "<unknown>", ''
|
||||||
|
|
||||||
|
def compose(a, b):
|
||||||
|
""" Composes the callables ``a`` and ``b``. ``compose(a, b)(*args)`` is
|
||||||
|
equivalent to ``a(b(*args))``.
|
||||||
|
|
||||||
|
Can be used as a decorator by partially applying ``a``::
|
||||||
|
|
||||||
|
@partial(compose, a)
|
||||||
|
def b():
|
||||||
|
...
|
||||||
|
"""
|
||||||
|
@wraps(b)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
return a(b(*args, **kwargs))
|
||||||
|
return wrapper
|
||||||
|
|
||||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
||||||
|
|
Loading…
Reference in New Issue