summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2015-12-25 05:41:12 +0000
committerptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2015-12-25 05:41:12 +0000
commitce4abdf732915bca32005784ed78c77ceec8ad98 (patch)
tree8d99fe1c8ad1f3fd274af25dc47a33a3fb61db68
parent9e48c8e161bb093ae13de45dd8bcc9947be583c1 (diff)
downloadpyparsing-ce4abdf732915bca32005784ed78c77ceec8ad98.tar.gz
Update wordsToNum.py example
- removed list comprehensions when gen expr is sufficient - changed ignore('-') to ignore(Literal('-')) - converted test code to use runTests git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/trunk@307 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b
-rw-r--r--src/examples/wordsToNum.py27
1 files changed, 8 insertions, 19 deletions
diff --git a/src/examples/wordsToNum.py b/src/examples/wordsToNum.py
index 10bfbe2..60c7c3d 100644
--- a/src/examples/wordsToNum.py
+++ b/src/examples/wordsToNum.py
@@ -38,7 +38,7 @@ unitDefinitions = [
("eighteen", 18),
("nineteen", 19),
]
-units = Or( [ makeLit(s,v) for s,v in unitDefinitions ] )
+units = Or(makeLit(s,v) for s,v in unitDefinitions)
tensDefinitions = [
("ten", 10),
@@ -52,7 +52,7 @@ tensDefinitions = [
("eighty", 80),
("ninety", 90),
]
-tens = Or( [ makeLit(s,v) for s,v in tensDefinitions ] )
+tens = Or(makeLit(s,v) for s,v in tensDefinitions)
hundreds = makeLit("hundred", 100)
@@ -64,7 +64,7 @@ majorDefinitions = [
("quadrillion", int(1e15)),
("quintillion", int(1e18)),
]
-mag = Or( [ makeLit(s,v) for s,v in majorDefinitions ] )
+mag = Or(makeLit(s,v) for s,v in majorDefinitions)
wordprod = lambda t: reduce(mul,t)
wordsum = lambda t: sum(t)
@@ -74,26 +74,15 @@ numPart = (((( units + Optional(hundreds) ).setParseAction(wordprod) +
+ Optional(units) ).setParseAction(wordsum)
numWords = OneOrMore( (numPart + Optional(mag)).setParseAction(wordprod)
).setParseAction(wordsum) + StringEnd()
-numWords.ignore("-")
+numWords.ignore(Literal("-"))
numWords.ignore(CaselessLiteral("and"))
def test(s,expected):
- try:
- val = numWords.parseString(s)[0]
- except ParseException as pe:
- print("Parsing failed:")
- print(s)
- print("%s^" % (' '*(pe.col-1)))
- print(pe.msg)
- else:
- print("'%s' -> %d" % (s, val), end=' ')
- if val == expected:
- print("CORRECT")
- else:
- print("***WRONG***, expected %d" % expected)
+ print ("Expecting %s" % expected)
+ numWords.runTests(s)
-test("one hundred twenty hundred", 120)
-test("one hundred and twennty", 120)
+test("one hundred twenty hundred", None)
+test("one hundred and twennty", None)
test("one hundred and twenty", 120)
test("one hundred and three", 103)
test("one hundred twenty-three", 123)