summaryrefslogtreecommitdiff
path: root/src/examples/romanNumerals.py
diff options
context:
space:
mode:
authorPaul McGuire <ptmcg@austin.rr.com>2015-12-31 00:55:47 +0000
committerPaul McGuire <ptmcg@austin.rr.com>2015-12-31 00:55:47 +0000
commit401ba4a945b88f12e26495a82d4059d4adbaec92 (patch)
tree9570287813d7358750ddd1ffe50ba60c0a999972 /src/examples/romanNumerals.py
parentc8e8c6592d625d24bc7c0ca34bcc527ec380c2d5 (diff)
downloadpyparsing-git-401ba4a945b88f12e26495a82d4059d4adbaec92.tar.gz
Added new example parseTabularData.py
Updated several examples to more current Python and pyparsing practices (and better Py2/Py3 cross-compatibility)
Diffstat (limited to 'src/examples/romanNumerals.py')
-rw-r--r--src/examples/romanNumerals.py13
1 files changed, 5 insertions, 8 deletions
diff --git a/src/examples/romanNumerals.py b/src/examples/romanNumerals.py
index 03605be..27361f0 100644
--- a/src/examples/romanNumerals.py
+++ b/src/examples/romanNumerals.py
@@ -31,9 +31,8 @@ romanNumeral = OneOrMore(numeral).setParseAction( lambda s,l,t : sum(t) )
# unit tests
def makeRomanNumeral(n):
def addDigit(n,limit,c,s):
- if n >= limit:
- n -= limit
- s += c
+ n -= limit
+ s += c
return n,s
ret = ""
@@ -51,18 +50,16 @@ def makeRomanNumeral(n):
while n >= 4: n,ret = addDigit(n, 4,"IV",ret)
while n >= 1: n,ret = addDigit(n, 1,"I",ret)
return ret
-tests = " ".join([makeRomanNumeral(i) for i in range(1,5000+1)])
+tests = " ".join(makeRomanNumeral(i) for i in range(1,5000+1))
expected = 1
for t,s,e in romanNumeral.scanString(tests):
if t[0] != expected:
- print("==>", end=' ')
- print(t,tests[s:e])
+ print("{} {} {}".format("==>", t, tests[s:e]))
expected += 1
-print()
def test(rn):
- print(rn,romanNumeral.parseString(rn))
+ print("{} -> {}".format(rn, romanNumeral.parseString(rn)[0]))
test("XVI")
test("XXXIX")
test("XIV")