Exemplo n.º 1
0
    def test_flatten(self):
        @arguments.accept(arguments.flatten(int))
        def foo(a):
            self.assertListEqual(a, [123, 456, 789])

        foo([123, 456, 789])
        foo([123, [456, 789]])
        foo([123, [[456], 789]])
        foo([[[123, [[[[456]]], 789]]]])

        with self.assertRaises(TypeError):
            foo([123, ['blah', 789]])
Exemplo n.º 2
0
    def test_flatten(self):
        @arguments.accept(arguments.flatten(int))
        def foo(a):
            self.assertListEqual(a, [123, 456, 789])

        foo([123, 456, 789])
        foo([123, [456, 789]])
        foo([123, [[456], 789]])
        foo([[[123, [[[[456]]], 789]]]])

        with self.assertRaises(TypeError):
            foo([123, ['blah', 789]])
Exemplo n.º 3
0
    def test_repr(self):
        self.assertEqual(
            repr(arguments._make_constraint(int)),
            'int')
        self.assertEqual(
            repr(arguments._make_constraint(arguments.nullable(int))),
            'nullable(int)')
        self.assertEqual(
            repr(arguments._make_constraint([int])),
            '[int]')
        self.assertEqual(
            repr(arguments._make_constraint((23, 42))),
            '(23, 42)')
        self.assertEqual(
            repr(arguments._make_constraint((int, float, str))),
            '(int, float, str)')
        self.assertEqual(
            repr(arguments._make_constraint([int, float, str])),
            '[int, float, str]')
        self.assertEqual(
            repr(arguments._make_constraint({int: str})),
            '{int: str}')
        self.assertEqual(
            repr(arguments._make_constraint(arguments.flatten(int))),
            'flatten(int)')
        self.assertEqual(
            repr(arguments._make_constraint(arguments.each(int, float))),
            'each(int, float)')
        self.assertEqual(
            repr(arguments._make_constraint(arguments.either(int, str))),
            'either(int, str)')
        self.assertEqual(
            repr(arguments._make_constraint(lambda x: x / 2)),
            'lambda x: x / 2')
        self.assertEqual(
            repr(arguments._make_constraint(
                arguments.condition(lambda x: x < 3))),
            'condition(lambda x: x < 3)')

        def foo(x): x

        constraint = arguments.either(
            arguments.each(int, arguments.condition(lambda x: x%2 == 0)),
            arguments.sequenceof(foo),
            str,
        )
        reprs = 'either(each(int, condition(lambda x: x%2 == 0)), [foo], str)'

        self.assertEqual(repr(arguments._make_constraint(constraint)), reprs)
Exemplo n.º 4
0
    def test_repr(self):
        self.assertEqual(repr(arguments._make_constraint(int)), 'int')
        self.assertEqual(
            repr(arguments._make_constraint(arguments.nullable(int))),
            'nullable(int)')
        self.assertEqual(repr(arguments._make_constraint([int])), '[int]')
        self.assertEqual(repr(arguments._make_constraint((23, 42))),
                         '(23, 42)')
        self.assertEqual(repr(arguments._make_constraint((int, float, str))),
                         '(int, float, str)')
        self.assertEqual(repr(arguments._make_constraint([int, float, str])),
                         '[int, float, str]')
        self.assertEqual(repr(arguments._make_constraint({int: str})),
                         '{int: str}')
        self.assertEqual(
            repr(arguments._make_constraint(arguments.flatten(int))),
            'flatten(int)')
        self.assertEqual(
            repr(arguments._make_constraint(arguments.each(int, float))),
            'each(int, float)')
        self.assertEqual(
            repr(arguments._make_constraint(arguments.either(int, str))),
            'either(int, str)')
        self.assertEqual(repr(arguments._make_constraint(lambda x: x / 2)),
                         'lambda x: x / 2')
        self.assertEqual(
            repr(
                arguments._make_constraint(
                    arguments.condition(lambda x: x < 3))),
            'condition(lambda x: x < 3)')

        def foo(x):
            x

        constraint = arguments.either(
            arguments.each(int, arguments.condition(lambda x: x % 2 == 0)),
            arguments.sequenceof(foo),
            str,
        )
        reprs = 'either(each(int, condition(lambda x: x%2 == 0)), [foo], str)'

        self.assertEqual(repr(arguments._make_constraint(constraint)), reprs)
Exemplo n.º 5
0
    if None in d:
        f = Chain(~t(k) for k in dd.keys())
        r.append(f >> d[None])

    return r


def _make_threshold(f, patch_lower, patch_upper):
    return Fork([
        f >> patch_lower,
        ~f >> patch_upper,
    ])


@_arguments.accept({
    _arguments.nullable(_arguments.flatten(_util.port_number, tuple)):
    _UNIT_TYPES
})
def PortSplit(mapping):
    """
    PortSplit(mapping)

    Split events by input port, with *mapping* being a dictionary of the form
    ``{ports: patch, ...}``.
    """
    return _make_split(PortFilter, mapping)


@_arguments.accept({
    _arguments.nullable(_arguments.flatten(_util.channel_number, tuple)):
    _UNIT_TYPES
Exemplo n.º 6
0
# (at your option) any later version.
#

import _mididings

from mididings.units.base import _Filter

import mididings.util as _util
import mididings.overload as _overload
import mididings.arguments as _arguments
import mididings.unitrepr as _unitrepr

import functools as _functools


@_unitrepr.accept(_arguments.flatten(_util.port_number), add_varargs=True)
def PortFilter(ports):
    """
    PortFilter(ports, ...)

    Filter events by port name or number. The *ports* argument can be a single
    port or a list of multiple ports.
    """
    return _Filter(_mididings.PortFilter(map(_util.actual, ports)))


@_unitrepr.accept(_arguments.flatten(_util.channel_number), add_varargs=True)
def ChannelFilter(channels):
    """
    ChannelFilter(channels, ...)
Exemplo n.º 7
0
        f = Chain(~t(k) for k in dd.keys())
        r.append(f >> d[None])

    return r


def _make_threshold(f, patch_lower, patch_upper):
    return Fork([
        f >> patch_lower,
        ~f >> patch_upper,
    ])



@_arguments.accept({
    _arguments.nullable(_arguments.flatten(_util.port_number, tuple)):
        _UNIT_TYPES
})
def PortSplit(mapping):
    """
    PortSplit(mapping)

    Split events by input port, with *mapping* being a dictionary of the form
    ``{ports: patch, ...}``.
    """
    return _make_split(PortFilter, mapping)


@_arguments.accept({
    _arguments.nullable(_arguments.flatten(_util.channel_number, tuple)):
        _UNIT_TYPES