diff options
author | Paul McGuire <ptmcg@austin.rr.com> | 2012-11-23 08:54:10 +0000 |
---|---|---|
committer | Paul McGuire <ptmcg@austin.rr.com> | 2012-11-23 08:54:10 +0000 |
commit | 774e46526945ea91265734a2dc82d15eed515577 (patch) | |
tree | 734ae210c20a98f01fe029f7e6eeb7a93b4617fb /src/examples/configParse.py | |
parent | 6b12041d4656f4cda910f24acda8d71013166fbd (diff) | |
download | pyparsing-git-774e46526945ea91265734a2dc82d15eed515577.tar.gz |
Clean up examples to be Python 3 compatible
Diffstat (limited to 'src/examples/configParse.py')
-rw-r--r-- | src/examples/configParse.py | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/src/examples/configParse.py b/src/examples/configParse.py index a01bef8..edea293 100644 --- a/src/examples/configParse.py +++ b/src/examples/configParse.py @@ -8,7 +8,7 @@ from pyparsing import \
Literal, Word, ZeroOrMore, Group, Dict, Optional, \
- printables, ParseException, restOfLine
+ printables, ParseException, restOfLine, empty
import pprint
@@ -30,7 +30,11 @@ def inifile_BNF(): nonequals = "".join( [ c for c in printables if c != "=" ] ) + " \t"
sectionDef = lbrack + Word( nonrbrack ) + rbrack
- keyDef = ~lbrack + Word( nonequals ) + equals + restOfLine
+ keyDef = ~lbrack + Word( nonequals ) + equals + empty + restOfLine
+ # strip any leading or trailing blanks from key
+ def stripKey(tokens):
+ tokens[0] = tokens[0].strip()
+ keyDef.setParseAction(stripKey)
# using Dict will allow retrieval of named data fields as attributes of the parsed results
inibnf = Dict( ZeroOrMore( Group( sectionDef + Dict( ZeroOrMore( Group( keyDef ) ) ) ) ) )
@@ -43,26 +47,26 @@ def inifile_BNF(): pp = pprint.PrettyPrinter(2)
def test( strng ):
- print strng
+ print(strng)
try:
- iniFile = file(strng)
+ iniFile = open(strng)
iniData = "".join( iniFile.readlines() )
bnf = inifile_BNF()
tokens = bnf.parseString( iniData )
pp.pprint( tokens.asList() )
- except ParseException, err:
- print err.line
- print " "*(err.column-1) + "^"
- print err
+ except ParseException as err:
+ print(err.line)
+ print(" "*(err.column-1) + "^")
+ print(err)
iniFile.close()
- print
+ print()
return tokens
ini = test("setup.ini")
-print "ini['Startup']['modemid'] =", ini['Startup']['modemid']
-print "ini.Startup =", ini.Startup
-print "ini.Startup.modemid =", ini.Startup.modemid
+print("ini['Startup']['modemid'] =", ini['Startup']['modemid'])
+print("ini.Startup =", ini.Startup)
+print("ini.Startup.modemid =", ini.Startup.modemid)
|