Skip to content

Commit

Permalink
Functional additions.
Browse files Browse the repository at this point in the history
  • Loading branch information
yesudeep committed Jan 1, 2013
1 parent d940324 commit 9563312
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
30 changes: 29 additions & 1 deletion mom/functional.py 100644 → 100755
Expand Up @@ -217,10 +217,12 @@ def oldest(person1, person2):
Predicates, transforms, and walkers
-----------------------------------
.. autofunction:: always
.. autofunction:: constant
.. autofunction:: identity
.. autofunction:: loob
.. autofunction:: always
.. autofunction:: never
.. autofunction:: nothing
"""

# pylint: disable-msg=C0302
Expand Down Expand Up @@ -1472,3 +1474,29 @@ def never(_):
``False``.
"""
return False


def constant(c):
"""Returns a predicate function that returns the given constant.
:param c:
The constant that the generated predicate function will return.
:returns:
A predicate function that returns the specified constant.
"""

def _func(*args, **kwargs):
return c

return _func


def nothing(*args, **kwargs):
"""A function that does nothing.
:param args:
Any number of positional arguments.
:param kwargs:
Any number of keyword arguments.
"""
pass
17 changes: 17 additions & 0 deletions mom/tests/test_mom_functional.py 100644 → 100755
Expand Up @@ -766,6 +766,23 @@ def test_never_true(self):
self.assertFalse(functional.never(True))


class Test_constant(unittest2.TestCase):
def test_returns_a_function(self):
func = functional.constant(4)
self.assertTrue(callable(func))
self.assertEqual('_func', func.func_name)

def test_generate_returns_constant(self):
func = functional.constant(4)
self.assertEqual(4, func(45))


class Test_nothing(unittest2.TestCase):
def test_does_nothing(self):
self.assertEqual(None, funtional.nothing())
self.assertEqual(None, funtional.nothing(34, 21, a="something"))


class Test_partition_dict(unittest2.TestCase):
def test_properly_partitions(self):
args = dict(
Expand Down
8 changes: 3 additions & 5 deletions tox.ini
@@ -1,8 +1,8 @@
[tox]
envlist = py25,py26,py27,py32,pypy
envlist = py25,py26,py27,py33,pypy

[pytest]
# Add -v for verbosity.
#Add -v for verbosity.
#addopts = -n4 -v --cov mom --cov-report term-missing # --doctest-modules
addopts = --cov mom --cov-report term-missing # --doctest-modules

Expand Down Expand Up @@ -41,18 +41,16 @@ deps=pyasn1 >=0.0.13
pytest-cov
sphinx

[testenv:py32]
[testenv:py33]
deps=coverage >= 3.5
unittest2py3k
PyTest
pytest-xdist
pytest-cov
# sphinx

[testenv:pypy]
deps=pyasn1 >=0.0.13
coverage >= 3.5
# PyCrypto ==2.0.1
unittest2
PyTest
pytest-xdist
Expand Down

0 comments on commit 9563312

Please sign in to comment.