summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2016-05-25 04:55:43 +0000
committerptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2016-05-25 04:55:43 +0000
commitce01d6e59b795879dfabc3433b4507cde1e58b71 (patch)
tree2187e3a4711b4154d3039814a74db8eb98d56d86
parente4d24d41d23ffbd97845d38f18d2c807e7142981 (diff)
downloadpyparsing-ce01d6e59b795879dfabc3433b4507cde1e58b71.tar.gz
Added new numerics.py example
git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/trunk@359 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b
-rw-r--r--src/CHANGES7
-rw-r--r--src/examples/numerics.py62
2 files changed, 69 insertions, 0 deletions
diff --git a/src/CHANGES b/src/CHANGES
index 446e3b4..a24709f 100644
--- a/src/CHANGES
+++ b/src/CHANGES
@@ -45,6 +45,13 @@ Verison 2.1.5 -
value will return True only if all tests *fail* as expected. Also,
parseAll now defaults to True.
+- New example numerics.py, shows samples of parsing integer and real
+ numbers using locale-dependent formats:
+
+ 4.294.967.295,000
+ 4 294 967 295,000
+ 4,294,967,295.000
+
Version 2.1.4 - May, 2016
------------------------------
diff --git a/src/examples/numerics.py b/src/examples/numerics.py
new file mode 100644
index 0000000..5ab99dd
--- /dev/null
+++ b/src/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)