My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
Changes to /src/lepl/_example/columns.py
1bc735ea4745 vs. d0c0c1b67cfa Compare: vs.  Format:
Revision d0c0c1b67cfa
Go to: 
Project members, sign in to write a code review
/src/lepl/_example/columns.py   1bc735ea4745 /src/lepl/_example/columns.py   d0c0c1b67cfa
1 1
2 # The contents of this file are subject to the Mozilla Public License 2 # The contents of this file are subject to the Mozilla Public License
3 # (MPL) Version 1.1 (the "License"); you may not use this file except 3 # (MPL) Version 1.1 (the "License"); you may not use this file except
4 # in compliance with the License. You may obtain a copy of the License 4 # in compliance with the License. You may obtain a copy of the License
5 # at http://www.mozilla.org/MPL/ 5 # at http://www.mozilla.org/MPL/
6 # 6 #
7 # Software distributed under the License is distributed on an "AS IS" 7 # Software distributed under the License is distributed on an "AS IS"
8 # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 8 # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9 # the License for the specific language governing rights and 9 # the License for the specific language governing rights and
10 # limitations under the License. 10 # limitations under the License.
11 # 11 #
12 # The Original Code is LEPL (http://www.acooke.org/lepl) 12 # The Original Code is LEPL (http://www.acooke.org/lepl)
13 # The Initial Developer of the Original Code is Andrew Cooke. 13 # The Initial Developer of the Original Code is Andrew Cooke.
14 # Portions created by the Initial Developer are Copyright (C) 2009-2010 14 # Portions created by the Initial Developer are Copyright (C) 2009-2010
15 # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 15 # Andrew Cooke (andrew@acooke.org). All Rights Reserved.
16 # 16 #
17 # Alternatively, the contents of this file may be used under the terms 17 # Alternatively, the contents of this file may be used under the terms
18 # of the LGPL license (the GNU Lesser General Public License, 18 # of the LGPL license (the GNU Lesser General Public License,
19 # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 19 # http://www.gnu.org/licenses/lgpl.html), in which case the provisions
20 # of the LGPL License are applicable instead of those above. 20 # of the LGPL License are applicable instead of those above.
21 # 21 #
22 # If you wish to allow use of your version of this file only under the 22 # If you wish to allow use of your version of this file only under the
23 # terms of the LGPL License and not to allow others to use your version 23 # terms of the LGPL License and not to allow others to use your version
24 # of this file under the MPL, indicate your decision by deleting the 24 # of this file under the MPL, indicate your decision by deleting the
25 # provisions above and replace them with the notice and other provisions 25 # provisions above and replace them with the notice and other provisions
26 # required by the LGPL License. If you do not delete the provisions 26 # required by the LGPL License. If you do not delete the provisions
27 # above, a recipient may use your version of this file under either the 27 # above, a recipient may use your version of this file under either the
28 # MPL or the LGPL License. 28 # MPL or the LGPL License.
29 29
30 #@PydevCodeAnalysisIgnore 30 #@PydevCodeAnalysisIgnore
31 # pylint: disable-msg=W0401,C0111,W0614,W0622,C0301,C0321,C0324,C0103 31 # pylint: disable-msg=W0401,C0111,W0614,W0622,C0301,C0321,C0324,C0103
32 # (the code style is for documentation, not "real") 32 # (the code style is for documentation, not "real")
33 33
34 ''' 34 '''
35 Process a table of data based on values from 35 Process a table of data based on values from
36 http://www.swivel.com/data_sets/spreadsheet/1002196 36 http://www.swivel.com/data_sets/spreadsheet/1002196
37 ''' 37 '''
38 38
39 from logging import basicConfig, DEBUG 39 from logging import basicConfig, DEBUG
40 40
41 from lepl import * 41 from lepl import *
42 from lepl._example.support import Example 42 from lepl._example.support import Example
43 43
44 44
45 class ColumnExample(Example): 45 class ColumnExample(Example):
46 46
47 def test_columns(self): 47 def test_columns(self):
48 basicConfig(level=DEBUG) 48 #basicConfig(level=DEBUG)
49 49
50 # http://www.swivel.com/data_sets/spreadsheet/1002196 50 # http://www.swivel.com/data_sets/spreadsheet/1002196
51 table = ''' 51 table = '''
52 US Foreign Aid, top recipients, constant dollars 52 US Foreign Aid, top recipients, constant dollars
53 Year Iraq Israel Egypt 53 Year Iraq Israel Egypt
54 2005 6,981,200,000 2,684,100,000 1,541,900,000 54 2005 6,981,200,000 2,684,100,000 1,541,900,000
55 2004 8,333,400,000 2,782,400,000 2,010,600,000 55 2004 8,333,400,000 2,782,400,000 2,010,600,000
56 2003 4,150,000,000 3,878,300,000 1,849,600,000 56 2003 4,150,000,000 3,878,300,000 1,849,600,000
57 2002 41,600,000 2,991,200,000 2,362,800,000 57 2002 41,600,000 2,991,200,000 2,362,800,000
58 ''' 58 '''
59 spaces = ~Space()[:] 59 spaces = ~Space()[:]
60 integer = (spaces & Digit()[1:, ~Optional(','), ...] & spaces) >> int 60 integer = (spaces & Digit()[1:, ~Optional(','), ...] & spaces) >> int
61 cols = Columns((4, integer), 61 cols = Columns((4, integer),
62 # if we give widths, they follow on from each other 62 # if we give widths, they follow on from each other
63 (16, integer), 63 (16, integer),
64 # we can also specify column indices 64 # we can also specify column indices
65 ((23, 36), integer), 65 ((23, 36), integer),
66 # and then start with widths again 66 # and then start with widths again
67 (16, integer)) 67 (16, integer))
68 # by default, Columns consumes a whole line (see skip argument), so 68 # by default, Columns consumes a whole line (see skip argument), so
69 # for the whole table we only need to (1) drop the text and (2) put 69 # for the whole table we only need to (1) drop the text and (2) put
70 # each row in a separate list. 70 # each row in a separate list.
71 parser = ~SkipTo(Digit(), include=False) & (cols > list)[:] 71 matcher = ~SkipTo(Digit(), include=False) & (cols > list)[:]
72 matcher.config#.no_memoize()
73 matcher.config#.no_full_first_match()
74 matcher.config#.record_deepest()
75 parser = matcher.get_parse()
76 print(parser.matcher.tree())
72 77
73 # L included below to check that it's ignored in comparison 78 # L included below to check that it's ignored in comparison
74 # (appears in windows?) 79 # (appears in windows?)
75 self.examples([(lambda: parser.parse(table), 80 self.examples([(lambda: parser(table),
76 '[[2005, 6981200000L, 2684100000, 1541900000], ' 81 '[[2005, 6981200000L, 2684100000, 1541900000], '
77 '[2004, 8333400000, 2782400000, 2010600000], ' 82 '[2004, 8333400000, 2782400000, 2010600000], '
78 '[2003, 4150000000, 3878300000, 1849600000], ' 83 '[2003, 4150000000, 3878300000, 1849600000], '
79 '[2002, 41600000, 2991200000, 2362800000]]')]) 84 '[2002, 41600000, 2991200000, 2362800000]]')])
80 85
81 86
82 87
83 88
84 89
Powered by Google Project Hosting