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/chemicalFormulas.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/chemicalFormulas.py')
-rw-r--r-- | examples/chemicalFormulas.py | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/examples/chemicalFormulas.py b/examples/chemicalFormulas.py new file mode 100644 index 0000000..ce66afd --- /dev/null +++ b/examples/chemicalFormulas.py @@ -0,0 +1,67 @@ +# chemicalFormulas.py
+#
+# Copyright (c) 2003, Paul McGuire
+#
+
+from pyparsing import Word, Optional, OneOrMore, Group, ParseException, Regex
+from pyparsing import alphas
+
+atomicWeight = {
+ "O" : 15.9994,
+ "H" : 1.00794,
+ "Na" : 22.9897,
+ "Cl" : 35.4527,
+ "C" : 12.0107
+ }
+
+def test( bnf, strg, fn=None ):
+ try:
+ print(strg,"->", bnf.parseString( strg ), end=' ')
+ except ParseException as pe:
+ print(pe)
+ else:
+ if fn != None:
+ print(fn( bnf.parseString( strg ) ))
+ else:
+ print()
+
+digits = "0123456789"
+
+# Version 1
+element = Regex("A[cglmrstu]|B[aehikr]?|C[adeflmorsu]?|D[bsy]|"
+ "E[rsu]|F[emr]?|G[ade]|H[efgos]?|I[nr]?|Kr?|L[airu]|"
+ "M[dgnot]|N[abdeiop]?|Os?|P[abdmortu]?|R[abefghnu]|"
+ "S[bcegimnr]?|T[abcehilm]|U(u[bhopqst])?|V|W|Xe|Yb?|Z[nr]")
+
+element = Word( alphas.upper(), alphas.lower(), max=2)
+elementRef = Group( element + Optional( Word( digits ), default="1" ) )
+formula = OneOrMore( elementRef )
+
+fn = lambda elemList : sum(atomicWeight[elem]*int(qty) for elem,qty in elemList)
+test( formula, "H2O", fn )
+test( formula, "C6H5OH", fn )
+test( formula, "NaCl", fn )
+print()
+
+# Version 2 - access parsed items by field name
+elementRef = Group( element("symbol") + Optional( Word( digits ), default="1" )("qty") )
+formula = OneOrMore( elementRef )
+
+fn = lambda elemList : sum(atomicWeight[elem.symbol]*int(elem.qty) for elem in elemList)
+test( formula, "H2O", fn )
+test( formula, "C6H5OH", fn )
+test( formula, "NaCl", fn )
+print()
+
+# Version 3 - convert integers during parsing process
+integer = Word( digits ).setParseAction(lambda t:int(t[0]))
+elementRef = Group( element("symbol") + Optional( integer, default=1 )("qty") )
+formula = OneOrMore( elementRef )
+
+fn = lambda elemList : sum(atomicWeight[elem.symbol]*elem.qty for elem in elemList)
+test( formula, "H2O", fn )
+test( formula, "C6H5OH", fn )
+test( formula, "NaCl", fn )
+
+
+
|