diff options
author | Paul McGuire <ptmcg@users.noreply.github.com> | 2018-01-06 23:38:53 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-06 23:38:53 -0600 |
commit | 430c5ad767cc946e9da7cd5f4673a4e3bd135a3c (patch) | |
tree | 5a7df11e0fd52ab320b0ef3e670e260f315ca9ae /examples/numerics.py | |
parent | f1d12567a8da4d254e6d62bb0d650c87c7d0bb89 (diff) | |
parent | d953150a6db3ac247a64b047edc2df7156f3e56b (diff) | |
download | pyparsing-git-430c5ad767cc946e9da7cd5f4673a4e3bd135a3c.tar.gz |
Merge pull request #1 from cngkaygusuz/master
Add Scrutinizer-CI configuration and other niceties
Diffstat (limited to 'examples/numerics.py')
-rw-r--r-- | examples/numerics.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/examples/numerics.py b/examples/numerics.py new file mode 100644 index 0000000..5ab99dd --- /dev/null +++ b/examples/numerics.py @@ -0,0 +1,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)
|