Пример #1
0
    def test_deepest(self):

        buffer = StringIO()
        # this test fails when run in a batch because logs are configured
        # differently beforehand!
        basicConfig(level=INFO, stream=buffer)
        #        basicConfig(level=INFO)

        name = Word() > 'name'
        phone = Integer() > 'phone'
        line = name / ',' / phone > make_dict
        matcher = line[0:, ~Newline()]
        matcher.config.clear().record_deepest()
        matcher.parse('andrew, 3333253\n bob, 12345')

        trace = buffer.getvalue()
        assert trace == r"""INFO:lepl.core.trace._RecordDeepest:
Up to 6 matches before and including longest match:
00105 '3333253\n' 8/1.9 (0008) 006 (['3333253'], (15, <helper>))  ->  Transform(And, TransformationWrapper(<add>))(8:'3')  ->  (['3333253'], (15, <helper>))
00106 '3333253\n' 8/1.9 (0008) 005 (['3333253'], (15, <helper>))  ->  Transform(Transform, TransformationWrapper(<apply>))(8:'3')  ->  ([('phone', '3333253')], (15, <helper>))
00107 'andrew...' 0/1.1 (0000) 004 ([('phone', '3333253')], (15, <helper>))  ->  And(And, Transform, Transform)(0:'a')  ->  ([('name', 'andrew'), ',', ' ', ('phone', '3333253')], (15, <helper>))
00108 'andrew...' 0/1.1 (0000) 003 ([('name', 'andrew'), ',', ' ', ('phone', '3333253')], (15, <helper>))  ->  Transform(And, TransformationWrapper(<apply>))(0:'a')  ->  ([{'phone': '3333253', 'name': 'andrew'}], (15, <helper>))
00113 '\n'        15/1.16 (0015) 004 next(Literal('\n')(15:'\n'))  ->  (['\n'], (16, <helper>))
00114 '\n'        15/1.16 (0015) 005 (['\n'], (16, <helper>))  ->  Or(Literal, Literal)(15:'\n')  ->  (['\n'], (16, <helper>))
Up to 2 failures following longest match:
00123 ' bob, ...' 16/2.1 (0016) 008 next(NfaRegexp('[^ \t\n\r\x0b\x0c]', <Unicode>)(16:' '))  ->  stop
00124 ' bob, ...' 16/2.1 (0016) 009 stop  ->  And(NfaRegexp, DepthFirst)(16:' ')  ->  stop
Up to 2 successful matches following longest match:
00139 'andrew...' 0/1.1 (0000) 002 stop  ->  DepthFirst(None, None, ([], <built-in function __add__>), rest=And, 0, first=Transform)(0:'a')  ->  ([{'phone': '3333253', 'name': 'andrew'}], (15, <helper>))

""", 'r"""' + trace + '"""'
Пример #2
0
    def test_trace(self):
        buffer = StringIO()
        with TraceVariables(out=buffer):
            word = ~Lookahead('OR') & Word()
            phrase = String()
            with DroppedSpace():
                text = (phrase | word)[1:] > list
                query = text[:, Drop('OR')]
        expected = '''      phrase failed                             stream = 'spicy meatballs OR...
        word = ['spicy']                        stream = ' meatballs OR "el ...
      phrase failed                             stream = 'meatballs OR "el b...
        word = ['meatballs']                    stream = ' OR "el bulli rest...
      phrase failed                             stream = 'OR "el bulli resta...
        word failed                             stream = 'OR "el bulli resta...
      phrase failed                             stream = ' OR "el bulli rest...
        word failed                             stream = ' OR "el bulli rest...
        text = [['spicy', 'meatballs']]         stream = ' OR "el bulli rest...
      phrase = ['el bulli restaurant']          stream = ''
      phrase failed                             stream = ''
        word failed                             stream = ''
        text = [['el bulli restaurant']]        stream = ''
       query = [['spicy', 'meatballs'], ['el... stream = ''
'''
        query.config.auto_memoize(full=True)
        query.parse('spicy meatballs OR "el bulli restaurant"')
        trace = buffer.getvalue()
        assert trace == expected, '"""' + trace + '"""'
        # check caching works
        query.parse('spicy meatballs OR "el bulli restaurant"')
        trace = buffer.getvalue()
        assert trace == expected, '"""' + trace + '"""'

        
Пример #3
0
    def test_deepest(self):

        buffer = StringIO()
        # this test fails when run in a batch because logs are configured 
        # differently beforehand!
        basicConfig(level=INFO, stream=buffer)
#        basicConfig(level=INFO)
        
        name    = Word()              > 'name'
        phone   = Integer()           > 'phone'
        line    = name / ',' / phone  > make_dict
        matcher = line[0:,~Newline()]
        matcher.config.clear().record_deepest()
        matcher.parse('andrew, 3333253\n bob, 12345')
        
        trace = buffer.getvalue()
        assert trace == r"""INFO:lepl.core.trace._RecordDeepest:
Up to 6 matches before and including longest match:
00105 '3333253\n' 8/1.9 (0008) 006 (['3333253'], (15, <helper>))  ->  Transform(And, TransformationWrapper(<add>))(8:'3')  ->  (['3333253'], (15, <helper>))
00106 '3333253\n' 8/1.9 (0008) 005 (['3333253'], (15, <helper>))  ->  Transform(Transform, TransformationWrapper(<apply>))(8:'3')  ->  ([('phone', '3333253')], (15, <helper>))
00107 'andrew...' 0/1.1 (0000) 004 ([('phone', '3333253')], (15, <helper>))  ->  And(And, Transform, Transform)(0:'a')  ->  ([('name', 'andrew'), ',', ' ', ('phone', '3333253')], (15, <helper>))
00108 'andrew...' 0/1.1 (0000) 003 ([('name', 'andrew'), ',', ' ', ('phone', '3333253')], (15, <helper>))  ->  Transform(And, TransformationWrapper(<apply>))(0:'a')  ->  ([{'phone': '3333253', 'name': 'andrew'}], (15, <helper>))
00113 '\n'        15/1.16 (0015) 004 next(Literal('\n')(15:'\n'))  ->  (['\n'], (16, <helper>))
00114 '\n'        15/1.16 (0015) 005 (['\n'], (16, <helper>))  ->  Or(Literal, Literal)(15:'\n')  ->  (['\n'], (16, <helper>))
Up to 2 failures following longest match:
00123 ' bob, ...' 16/2.1 (0016) 008 next(NfaRegexp('[^ \t\n\r\x0b\x0c]', <Unicode>)(16:' '))  ->  stop
00124 ' bob, ...' 16/2.1 (0016) 009 stop  ->  And(NfaRegexp, DepthFirst)(16:' ')  ->  stop
Up to 2 successful matches following longest match:
00139 'andrew...' 0/1.1 (0000) 002 stop  ->  DepthFirst(None, None, ([], <built-in function __add__>), rest=And, 0, first=Transform)(0:'a')  ->  ([{'phone': '3333253', 'name': 'andrew'}], (15, <helper>))

""", 'r"""'+trace+'"""'
Пример #4
0
    def test_context(self):
        #basicConfig(level=DEBUG)
        output = StringIO()
        with TraceVariables(out=output):
            bar = Any()
        bar.config.no_full_first_match()
        repr(bar)
        list(bar.match('abc'))
        text = output.getvalue()
        assert_str(text, '''         bar = ['a']                            stream = 'bc'
         bar failed                             stream = 'abc'
''')

        
Пример #5
0
    def test_context(self):
        # basicConfig(level=DEBUG)
        output = StringIO()
        with TraceVariables(out=output):
            bar = Any()
        bar.config.no_full_first_match()
        repr(bar)
        list(bar.match("abc"))
        text = output.getvalue()
        assert_str(
            text,
            """         bar = ['a']                            stream = 'bc'
         bar failed                             stream = 'abc'
""",
        )
Пример #6
0
    def test_wrapper(self):
        output = StringIO()
        matcher = NamedResult('foo', Any()[:], out=output)
        repr(matcher)
        matcher.config.clear()
        parser = matcher.get_match_string()
        list(parser('abc'))
        text = output.getvalue()
        assert_str(text, '''foo = ['a', 'b', 'c']
    'abc' -> <EOS>
foo (2) = ['a', 'b']
    'abc' -> 'c'
foo (3) = ['a']
    'abc' -> 'bc'
foo (4) = []
    'abc' -> 'abc'
! foo (after 4 matches)
    'abc'
''')
Пример #7
0
    def test_luca(self):
        '''
        See mailing list.
        '''
        integer = Token(Integer()) >> int 
        uletter = Token(Upper()) 
        real = Token(Real()) >> float 
        data_line = Line(integer & uletter & real[6]) 
        table = data_line[1:] 
        
        source = '''1      G      0.0            0.0 0.0            0.0            0.0            0.0 
2      G      0.0            0.0 0.0            0.0            0.0            0.0 
3      G      0.0            0.0 0.0            0.0            0.0            0.0 
4      G      0.0            0.0 0.0            0.0            0.0            0.0 
5      G      0.0            0.0 0.0            0.0            0.0            0.0 
6      G      0.0            0.0 0.0            0.0            0.0            0.0 
7      G      0.0            0.0 0.0            0.0            0.0            0.0 
8      G      0.0            0.0 0.0            0.0            0.0            0.0 
9      G      0.0            0.0           -9.856000E-05 -1.444699E-17   1.944000E-03   0.0 
10      G      0.0            0.0           -9.856000E-05 -1.427843E-17   1.944000E-03   0.0 
11      G      0.0            0.0           -1.085216E-02 -2.749537E-16   1.874400E-02   0.0 
12      G      0.0            0.0           -1.085216E-02 -2.748317E-16   1.874400E-02   0.0 
13      G      0.0            0.0           -3.600576E-02 -6.652665E-16   3.074400E-02   0.0 
14      G      0.0            0.0           -3.600576E-02 -6.717988E-16   3.074400E-02   0.0 
15      G      0.0            0.0           -7.075936E-02 -8.592844E-16   3.794400E-02   0.0 
16      G      0.0            0.0           -7.075936E-02 -8.537008E-16   3.794400E-02   0.0 
17      G      0.0            0.0           -1.103130E-01 -9.445027E-16   4.034400E-02   0.0 
18      G      0.0            0.0           -1.103130E-01 -9.538811E-16   4.034400E-02   0.0 
100      G      0.0            0.0 0.0            0.0            0.0            0.0 
200      G      0.0            0.0 0.0            0.0            0.0            0.0 
''' 

        out = StringIO()
        print_timing(source, 
            {'Real()': table.clone().config.lines().matcher,
             'Real() no memoize': table.clone().config.lines().no_memoize().matcher},
             count_compile=1, out=out)
        table = out.getvalue()
        print(table)
        assert 'Timing Results' in table, table
Пример #8
0
    def test_wrapper(self):
        output = StringIO()
        matcher = NamedResult("foo", Any()[:], out=output)
        repr(matcher)
        matcher.config.clear()
        parser = matcher.get_match_string()
        list(parser("abc"))
        text = output.getvalue()
        assert_str(
            text,
            """foo = ['a', 'b', 'c']
    'abc' -> <EOS>
foo (2) = ['a', 'b']
    'abc' -> 'c'
foo (3) = ['a']
    'abc' -> 'bc'
foo (4) = []
    'abc' -> 'abc'
! foo (after 4 matches)
    'abc'
""",
        )