summaryrefslogtreecommitdiff
path: root/trunk/src/examples/numerics.py
blob: 5ab99ddb7c425a007999a89988aa3d25ff7feea0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#
# numerics.py
#
# Examples of parsing real and integers using various grouping and
# decimal point characters, varying by locale.
#
# Copyright 2016, Paul McGuire
#
# Format samples from https://docs.oracle.com/cd/E19455-01/806-0169/overview-9/index.html
#
tests = """\
# Canadian (English and French) 
4 294 967 295,000  

# Danish 
4 294 967 295,000 

# Finnish 
4 294 967 295,000 

# French 
4 294 967 295,000  

# German 
4 294 967 295,000  

# Italian 
4.294.967.295,000 

# Norwegian 
4.294.967.295,000  

# Spanish 
4.294.967.295,000  

# Swedish 
4 294 967 295,000  

# GB-English 
4,294,967,295.000  

# US-English 
4,294,967,295.000  

# Thai 
4,294,967,295.000 
"""

from pyparsing import Regex

comma_decimal = Regex(r'\d{1,2}(([ .])\d\d\d(\2\d\d\d)*)?,\d*')
comma_decimal.setParseAction(lambda t: float(t[0].replace(' ','').replace('.','').replace(',','.')))

dot_decimal = Regex(r'\d{1,2}(([ ,])\d\d\d(\2\d\d\d)*)?\.\d*')
dot_decimal.setParseAction(lambda t: float(t[0].replace(' ','').replace(',','')))

decimal = comma_decimal ^ dot_decimal
decimal.runTests(tests, parseAll=True)

grouped_integer = Regex(r'\d{1,2}(([ .,])\d\d\d(\2\d\d\d)*)?')
grouped_integer.setParseAction(lambda t: int(t[0].replace(' ','').replace(',','').replace('.','')))
grouped_integer.runTests(tests, parseAll=False)