Пример #1
0
 def test_notNone(self):
     """
     If the first parameter to a `maybe`-wrapped function is not ``None``
     then the result of the wrapped function is returned.
     """
     self.assertThat(
         maybe(identity)(42),
         Equals(42))
     self.assertThat(
         maybe(identityv)(42, 1, a=2),
         Equals(((42, 1), {'a': 2})))
Пример #2
0
 def test_customDefault(self):
     """
     A custom value can be passed for the ``default`` parameter.
     """
     default = object()
     self.assertThat(
         maybe(identity, default=default)(None),
         Is(default))
     self.assertThat(
         maybe(identityv, default=default)(None, 1, a=2),
         Is(default))
Пример #3
0
 def test_none(self):
     """
     If the first parameter to a `maybe`-wrapped function is ``None`` then
     the result of that function is immediately ``None``.
     """
     self.assertThat(
         maybe(identity)(None),
         Is(None))
     self.assertThat(
         maybe(identityv)(None, 1, a=2),
         Is(None))
Пример #4
0
def many(func):
    """
    Create a callable that applies ``func`` to every value in a sequence.

    If the value is not a sequence then an empty list is returned.

    :type  func: `callable`
    :param func: Callable to be applied to the first result.
    """
    def _many(result):
        if _isSequenceTypeNotText(result):
            return map(func, result)
        return []
    return maybe(_many, default=[])
Пример #5
0
def one(func, n=0):
    """
    Create a callable that applies ``func`` to a value in a sequence.

    If the value is not a sequence or is an empty sequence then ``None`` is
    returned.

    :type  func: `callable`
    :param func: Callable to be applied to each result.

    :type  n: `int`
    :param n: Index of the value to apply ``func`` to.
    """
    def _one(result):
        if _isSequenceTypeNotText(result) and len(result) > n:
            return func(result[n])
        return None
    return maybe(_one)