def run(*args):
    pattern, value, result = args
    if result is None:
        assert not pm.match(value, pattern)
    else:
        assert pm.match(value, pattern)
        del result['_']
        assert pm.bound == result
def run(*args):
    pattern, value, result = args
    if result is None:
        assert not pm.match(value, pattern)
    else:
        assert pm.match(value, pattern)
        del result['_']
        assert pm.bound == result
예제 #3
0
def test_flatten():
    global FLATTEN
    FLATTEN = True

    YES((), 'foo', 'foo')
    YES((), (), ())

    YES(('whatev',), ANY, 'whatev')
    YES((('whatev', 'whatev'), ), ANY, ('whatev', 'whatev'))

    YES((), ('foo',), ('foo',))

    YES((('foo',),), ANY, ('foo',))
    YES(('foo',), (ANY,), ('foo',))
    YES((), (IGNORE(ANY),), ('whatev',))
    YES(('foo',), (ANY, IGNORE(ANY)), ('foo', 'whatev',))
    YES((), (IGNORE(ANY), IGNORE(ANY)), ('whatev', 'whatev',))
    YES(('foo', 'bar'), (ANY, ANY), ('foo', 'bar',))

    YES((), ('foo', IGNORE(ANY)), ('foo', 'whatev',))

    YES((), ('foo', (IGNORE(ANY), )), ('foo', ('whatev', )))
    YES((1, 2, 3,), ('foo', (ANY, (ANY, (ANY, )))), ('foo', (1, (2, (3,)))))
    YES((2, 3,), ('foo', (IGNORE(ANY), (ANY, (ANY, )))), ('foo', (1, (2, (3,)))))
    YES((3,), ('foo', (IGNORE(ANY), (IGNORE(ANY), (ANY, )))), ('foo', (1, (2, (3,)))))

    ok, v1, v2 = match(('foo', ('bar', ANY, ('baz', ANY))),
                       ('foo', ('bar', 123, ('baz', 456))),
                       flatten=True)
    assert ok and (v1, v2) == (123, 456)
def test_bind_padding_like():
    def odd_num(value):
        return value % 2 and value
    pattern = padding + [like(odd_num, 'value'), like(odd_num, 'other'), like(odd_num, 'value'), 2]
    assert match([3, 5, 3, 5, 3, 5, 2], pattern)
    assert bound.value == 5
    assert bound.other == 3
예제 #5
0
def YES(out, pattern, data):
    x = match(pattern, data, flatten=FLATTEN)
    if not FLATTEN:
        if out is not NOTHING:
            assert x[0], "should have matched"
            assert x[1] == out, "should have returned the correct values but returned %s" % repr(x[1])
        else:
            assert x
    else:
        assert x if out == () else x[0], "should have matched"
        assert out == () or x[1:] == out, "should have returned the correct values but returned %s" % repr(x[1:])
예제 #6
0
def test_bind_padding_like():
    def odd_num(value):
        return value % 2 and value

    pattern = padding + [
        like(odd_num, 'value'),
        like(odd_num, 'other'),
        like(odd_num, 'value'), 2
    ]
    assert match([3, 5, 3, 5, 3, 5, 2], pattern)
    assert bound.value == 5
    assert bound.other == 3
예제 #7
0
def test_without_flatten():
    global FLATTEN
    FLATTEN = False

    NO('foo', 'bar')
    YES(NOTHING, 'foo', 'foo')
    NO((), 'whatev')
    YES(NOTHING, (), ())

    YES('whatev', ANY, 'whatev')
    YES(('whatev', 'whatev'), ANY, ('whatev', 'whatev'))

    YES(NOTHING, ('foo',), ('foo',))
    NO(('whatev',), ('whatev', 'whatev'))
    NO(('whatev', 'whatev'), ('whatev',))

    YES(('foo',), ANY, ('foo',))
    YES(('foo',), (ANY,), ('foo',))
    YES(NOTHING, (IGNORE(ANY),), ('whatev',))
    YES(('foo',), (ANY, IGNORE(ANY)), ('foo', 'whatev',))
    YES(NOTHING, (IGNORE(ANY), IGNORE(ANY)), ('whatev', 'whatev',))
    YES(('foo', 'bar'), (ANY, ANY), ('foo', 'bar',))

    YES(NOTHING, ('foo', IGNORE(ANY)), ('foo', 'whatev',))
    NO(('foo', IGNORE), ('WRONG', 'whatev',))
    NO(('foo', ANY), ('WRONG', 'whatev',))

    YES(NOTHING, ('foo', (IGNORE(ANY), )), ('foo', ('whatev', )))
    YES(((1, (2, (3,))), ), ('foo', (ANY, (ANY, (ANY, )))), ('foo', (1, (2, (3,)))))
    YES((((2, (3,),),),), ('foo', (IGNORE(ANY), (ANY, (ANY, )))), ('foo', (1, (2, (3,)))))
    YES(((((3,),),),), ('foo', (IGNORE(ANY), (IGNORE(ANY), (ANY, )))), ('foo', (1, (2, (3,)))))

    with assert_not_raises(ValueError):
        _, ((_, (_, (_,))),) = match(
            ('foo', (ANY, (ANY, (ANY, )))),
            ('WRONG', (1, (2, (3,)))),
            flatten=False
            )
예제 #8
0
def test_bind_repeat_alternate():
    pattern = bind.any * repeat + bind.any * group('value') + [2, 1] + bind.any
    assert match([0, 1, 2, 1, 2], pattern)
    assert bound.value == [1]
예제 #9
0
def test_bind_repeat():
    assert match([1, 1, 1, 2, 2, 3], 1 * repeat + bind.value * repeat + 3)
    assert bound.value == 2
예제 #10
0
def test_bound():
    assert match(5, bind.value)
    assert bound.value == 5
def test_bind_any():
    assert match([0, 1, 2], [bind.any, bind.any, bind.any])
def test_bind_repeated():
    assert match((0, 0, 1, 1), (bind.zero, bind.zero, bind.one, bind.one))
    assert not match((0, 1), (bind.value, bind.value))
예제 #13
0
def test_bind_result():
    with pytest.raises(AttributeError):
        if match(0, bind.push):
            return 'zero'
        else:
            return 'nonzero'
예제 #14
0
 def __call__(self, msg, pattern):
     res = patternmatching.match(pattern, msg)
     return (res[0], res[1:])
def test_bind_padding_name():
    pattern = padding + [bind.value, bind.other, bind.value, 3]
    assert match([1, 2, 1, 2, 1, 2, 3], pattern)
    assert bound.value == 2
    assert bound.other == 1
예제 #16
0
def test_bind_any():
    assert match([0, 1, 2], [bind.any, bind.any, bind.any])
def test_bind_repeat_alternate():
    pattern = bind.any * repeat + bind.any * group('value') + [2, 1] + bind.any
    assert match([0, 1, 2, 1, 2], pattern)
    assert bound.value == [1]
def test_bind_repeat():
    assert match([1, 1, 1, 2, 2, 3], 1 * repeat + bind.value * repeat + 3)
    assert bound.value == 2
def test_bound():
    assert match(5, bind.value)
    assert bound.value == 5
예제 #20
0
def test_bind_padding_name():
    pattern = padding + [bind.value, bind.other, bind.value, 3]
    assert match([1, 2, 1, 2, 1, 2, 3], pattern)
    assert bound.value == 2
    assert bound.other == 1
예제 #21
0
    c.subscribe(topics, on_assign=print_assignment)

    # Read messages from Kafka, print to stdout
    try:
        while True:
            msg = c.poll(timeout=1.0)
            if msg is None:
                continue
            if msg.error():
                raise KafkaException(msg.error())
            else:
                # Proper message
                sys.stderr.write('%% %s [%d] at offset %d with key %s:\n' %
                                 (msg.topic(), msg.partition(), msg.offset(),
                                  str(msg.key())))

                value = msg.value().decode("utf-8").strip()

                if match(value, str('beep')):
                    mixer.play()
                else:
                    print("Unknown command: {cmd}".format(cmd=value))

    except KeyboardInterrupt:
        mixer.stop()
        sys.stderr.write('%% Aborted by user\n')

    finally:
        # Close down consumer to commit final offsets.
        c.close()
def match_basic(value):
    if match(value, None):
        return 'case-1'
    elif match(value, True):
        return 'case-2'
    elif match(value, False):
        return 'case-3'
    elif match(value, -100):
        return 'case-4'
    elif match(value, 1.234):
        return 'case-5'
    elif match(value, 12345678901234567890):
        return 'case-6'
    elif match(value, complex(1, 2)):
        return 'case-7'
    elif match(value, str('alpha')):
        return 'case-8'
    elif match(value, bytes(b'beta')):
        return 'case-9'
    elif match(value, (1, 2, 3, 4)):
        return 'case-15'
    elif match(value, [bind.first, bind.second, bind.third]):
        return 'case-11'
    elif match(value, like('^abc..abc$')):
        return 'case-12'
    elif match(value, like(lambda val: val % 17 == 0)):
        return 'case-13'
    elif match(value, Point(0, 0, 0, 0)):
        return 'case-14'
    elif match(value, [1, 2, 3, 4]):
        return 'case-16'
    elif match(value, (0, [1, (2, [3, (4, [5])])])):
        return 'case-17'
    elif match(value, tuple):
        return 'case-10'
    elif match(value, like(lambda val: val % 19 == 0)):
        return 'case-18'
    elif match(value, object):
        return 'case-19'
    else:
        raise Exception('no match')
예제 #23
0
 def __call__(self, msg, pattern):
     res = patternmatching.match(pattern, msg)
     return (res[0], res[1:])
예제 #24
0
def match_basic(value):
    if match(value, None):
        return 'case-1'
    elif match(value, True):
        return 'case-2'
    elif match(value, False):
        return 'case-3'
    elif match(value, -100):
        return 'case-4'
    elif match(value, 1.234):
        return 'case-5'
    elif match(value, 12345678901234567890):
        return 'case-6'
    elif match(value, complex(1, 2)):
        return 'case-7'
    elif match(value, str('alpha')):
        return 'case-8'
    elif match(value, bytes(b'beta')):
        return 'case-9'
    elif match(value, (1, 2, 3, 4)):
        return 'case-15'
    elif match(value, [bind.first, bind.second, bind.third]):
        return 'case-11'
    elif match(value, like('^abc..abc$')):
        return 'case-12'
    elif match(value, like(lambda val: val % 17 == 0)):
        return 'case-13'
    elif match(value, Point(0, 0, 0, 0)):
        return 'case-14'
    elif match(value, [1, 2, 3, 4]):
        return 'case-16'
    elif match(value, (0, [1, (2, [3, (4, [5])])])):
        return 'case-17'
    elif match(value, tuple):
        return 'case-10'
    elif match(value, like(lambda val: val % 19 == 0)):
        return 'case-18'
    elif match(value, object):
        return 'case-19'
    else:
        raise Exception('no match')
예제 #25
0
def NO(pattern, data):
    x = match(pattern, data)
    assert isinstance(x, tuple) and not x[0] or not x, x
예제 #26
0
def test_bind_repeated():
    assert match((0, 0, 1, 1), (bind.zero, bind.zero, bind.one, bind.one))
    assert not match((0, 1), (bind.value, bind.value))
def test_bind_result():
    with pytest.raises(AttributeError):
        if match(0, bind.push):
            return 'zero'
        else:
            return 'nonzero'