summaryrefslogtreecommitdiff
path: root/examples/dictExample.py
diff options
context:
space:
mode:
authorptmcg <ptmcg@austin.rr.com>2019-01-30 20:41:33 -0600
committerptmcg <ptmcg@austin.rr.com>2019-01-30 20:41:33 -0600
commit3157a77c9584a69839db0f8274c87438b912e99b (patch)
tree5e93a6e21715f9afcbc55eba56f4a5f962b67a46 /examples/dictExample.py
parentf10a02039a6039f8a42b0cc99bea51623676cf01 (diff)
downloadpyparsing-git-3157a77c9584a69839db0f8274c87438b912e99b.tar.gz
Update examples and unit tests to more preferred coding styles, imports for pyparsing_common as ppc and pyparsing_unicode as ppu
Diffstat (limited to 'examples/dictExample.py')
-rw-r--r--examples/dictExample.py25
1 files changed, 16 insertions, 9 deletions
diff --git a/examples/dictExample.py b/examples/dictExample.py
index 043d18f..7d3d45d 100644
--- a/examples/dictExample.py
+++ b/examples/dictExample.py
@@ -5,8 +5,7 @@
#
# Copyright (c) 2003, Paul McGuire
#
-from pyparsing import Literal, Word, Group, Dict, ZeroOrMore, alphas, nums, delimitedList
-import pprint
+import pyparsing as pp
testData = """
+-------+------+------+------+------+------+------+------+------+
@@ -20,22 +19,30 @@ testData = """
"""
# define grammar for datatable
-heading = (Literal(
+heading = (pp.Literal(
"+-------+------+------+------+------+------+------+------+------+") +
"| | A1 | B1 | C1 | D1 | A2 | B2 | C2 | D2 |" +
"+=======+======+======+======+======+======+======+======+======+").suppress()
-vert = Literal("|").suppress()
-number = Word(nums)
-rowData = Group( vert + Word(alphas) + vert + delimitedList(number,"|") + vert )
-trailing = Literal(
+vert = pp.Literal("|").suppress()
+number = pp.Word(pp.nums)
+rowData = pp.Group( vert + pp.Word(pp.alphas) + vert + pp.delimitedList(number,"|") + vert )
+trailing = pp.Literal(
"+-------+------+------+------+------+------+------+------+------+").suppress()
-datatable = heading + Dict( ZeroOrMore(rowData) ) + trailing
+datatable = heading + pp.Dict(pp.ZeroOrMore(rowData)) + trailing
# now parse data and print results
data = datatable.parseString(testData)
print(data)
-pprint.pprint(data.asList())
+
+# shortcut for import pprint; pprint.pprint(data.asList())
+data.pprint()
+
+# access all data keys
print("data keys=", list(data.keys()))
+
+# use dict-style access to values
print("data['min']=", data['min'])
+
+# use attribute-style access to values (if key is a valid Python identifier)
print("data.max", data.max)