summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2016-05-17 21:56:43 +0000
committerptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2016-05-17 21:56:43 +0000
commitdde3b6a7c469e79dd68408f3d36283b688927d3f (patch)
tree11830a4b8060758111f88f6dc770828555019b34
parenta7488072af8f7db1a046484d5c8d38e60293d398 (diff)
downloadpyparsing-dde3b6a7c469e79dd68408f3d36283b688927d3f.tar.gz
Update pyparsing code practices
git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/trunk@351 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b
-rw-r--r--src/examples/dictExample2.py11
-rw-r--r--src/examples/invRegex.py4
-rw-r--r--src/examples/simpleSQL.py114
-rw-r--r--src/examples/urlExtractorNew.py2
4 files changed, 30 insertions, 101 deletions
diff --git a/src/examples/dictExample2.py b/src/examples/dictExample2.py
index ccb7d3c..cae463b 100644
--- a/src/examples/dictExample2.py
+++ b/src/examples/dictExample2.py
@@ -6,7 +6,7 @@
#
# Copyright (c) 2004, Paul McGuire
#
-from pyparsing import Literal, Word, Group, Dict, ZeroOrMore, alphas, nums, delimitedList
+from pyparsing import Literal, Word, Group, Dict, ZeroOrMore, alphas, nums, delimitedList, pyparsing_common
import pprint
testData = """
@@ -22,13 +22,14 @@ testData = """
# define grammar for datatable
underline = Word("-=")
-number = Word(nums).setParseAction( lambda t : int(t[0]) )
+number = pyparsing_common.integer
+
vert = Literal("|").suppress()
rowDelim = ("+" + ZeroOrMore( underline + "+" ) ).suppress()
columnHeader = Group(vert + vert + delimitedList(Word(alphas + nums), "|") + vert)
-heading = rowDelim + columnHeader.setResultsName("columns") + rowDelim
+heading = rowDelim + columnHeader("columns") + rowDelim
rowData = Group( vert + Word(alphas) + vert + delimitedList(number,"|") + vert )
trailing = rowDelim
@@ -36,9 +37,7 @@ datatable = heading + Dict( ZeroOrMore(rowData) ) + trailing
# now parse data and print results
data = datatable.parseString(testData)
-print(data)
-print(data.asXML("DATA"))
-pprint.pprint(data.asList())
+print(data.dump())
print("data keys=", list(data.keys()))
print("data['min']=", data['min'])
print("sum(data['min']) =", sum(data['min']))
diff --git a/src/examples/invRegex.py b/src/examples/invRegex.py
index 5d9a393..b6fe1f1 100644
--- a/src/examples/invRegex.py
+++ b/src/examples/invRegex.py
@@ -161,8 +161,8 @@ def parser():
reNonCaptureGroup = Suppress("?:")
reDot = Literal(".")
repetition = (
- ( lbrace + Word(nums).setResultsName("count") + rbrace ) |
- ( lbrace + Word(nums).setResultsName("minCount")+","+ Word(nums).setResultsName("maxCount") + rbrace ) |
+ ( lbrace + Word(nums)("count") + rbrace ) |
+ ( lbrace + Word(nums)("minCount")+","+ Word(nums)("maxCount") + rbrace ) |
oneOf(list("*+?"))
)
diff --git a/src/examples/simpleSQL.py b/src/examples/simpleSQL.py
index e19d5c2..66dc18c 100644
--- a/src/examples/simpleSQL.py
+++ b/src/examples/simpleSQL.py
@@ -3,30 +3,17 @@
# simple demo of using the parsing library to do simple-minded SQL parsing
# could be extended to include where clauses etc.
#
-# Copyright (c) 2003, Paul McGuire
+# Copyright (c) 2003,2016, Paul McGuire
#
from pyparsing import Literal, CaselessLiteral, Word, delimitedList, Optional, \
Combine, Group, alphas, nums, alphanums, ParseException, Forward, oneOf, quotedString, \
ZeroOrMore, restOfLine, Keyword, upcaseTokens
-def test( str ):
- print(str + " ->")
- try:
- tokens = simpleSQL.parseString( str )
- print("tokens = ", tokens)
- print("tokens.columns =", tokens.columns)
- print("tokens.tables =", tokens.tables)
- print("tokens.where =", tokens.where)
- except ParseException as err:
- print(" "*err.loc + "^")
- print(err)
- print('')
-
-
# define SQL tokens
selectStmt = Forward()
-selectToken = Keyword("select", caseless=True)
-fromToken = Keyword("from", caseless=True)
+SELECT = Keyword("select", caseless=True)
+FROM = Keyword("from", caseless=True)
+WHERE = Keyword("where", caseless=True)
ident = Word( alphas, alphanums + "_$" ).setName("identifier")
columnName = ( delimitedList( ident, ".", combine=True ) ).addParseAction(upcaseTokens)
@@ -58,11 +45,9 @@ whereCondition = Group(
whereExpression << whereCondition + ZeroOrMore( ( and_ | or_ ) + whereExpression )
# define the grammar
-selectStmt << ( selectToken +
- ( '*' | columnNameList ).setResultsName( "columns" ) +
- fromToken +
- tableNameList.setResultsName( "tables" ) +
- Optional( Group( CaselessLiteral("where") + whereExpression ), "" ).setResultsName("where") )
+selectStmt <<= (SELECT + ('*' | columnNameList)("columns") +
+ FROM + tableNameList( "tables" ) +
+ Optional(Group(WHERE + whereExpression), "")("where"))
simpleSQL = selectStmt
@@ -70,73 +55,18 @@ simpleSQL = selectStmt
oracleSqlComment = "--" + restOfLine
simpleSQL.ignore( oracleSqlComment )
-
-test( "SELECT * from XYZZY, ABC" )
-test( "select * from SYS.XYZZY" )
-test( "Select A from Sys.dual" )
-test( "Select A,B,C from Sys.dual" )
-test( "Select A, B, C from Sys.dual" )
-test( "Select A, B, C from Sys.dual, Table2 " )
-test( "Xelect A, B, C from Sys.dual" )
-test( "Select A, B, C frox Sys.dual" )
-test( "Select" )
-test( "Select &&& frox Sys.dual" )
-test( "Select A from Sys.dual where a in ('RED','GREEN','BLUE')" )
-test( "Select A from Sys.dual where a in ('RED','GREEN','BLUE') and b in (10,20,30)" )
-test( "Select A,b from table1,table2 where table1.id eq table2.id -- test out comparison operators" )
-
-"""
-Test output:
->pythonw -u simpleSQL.py
-SELECT * from XYZZY, ABC ->
-tokens = ['select', '*', 'from', ['XYZZY', 'ABC']]
-tokens.columns = *
-tokens.tables = ['XYZZY', 'ABC']
-
-select * from SYS.XYZZY ->
-tokens = ['select', '*', 'from', ['SYS.XYZZY']]
-tokens.columns = *
-tokens.tables = ['SYS.XYZZY']
-
-Select A from Sys.dual ->
-tokens = ['select', ['A'], 'from', ['SYS.DUAL']]
-tokens.columns = ['A']
-tokens.tables = ['SYS.DUAL']
-
-Select A,B,C from Sys.dual ->
-tokens = ['select', ['A', 'B', 'C'], 'from', ['SYS.DUAL']]
-tokens.columns = ['A', 'B', 'C']
-tokens.tables = ['SYS.DUAL']
-
-Select A, B, C from Sys.dual ->
-tokens = ['select', ['A', 'B', 'C'], 'from', ['SYS.DUAL']]
-tokens.columns = ['A', 'B', 'C']
-tokens.tables = ['SYS.DUAL']
-
-Select A, B, C from Sys.dual, Table2 ->
-tokens = ['select', ['A', 'B', 'C'], 'from', ['SYS.DUAL', 'TABLE2']]
-tokens.columns = ['A', 'B', 'C']
-tokens.tables = ['SYS.DUAL', 'TABLE2']
-
-Xelect A, B, C from Sys.dual ->
-^
-Expected 'select'
-Expected 'select' (0), (1,1)
-
-Select A, B, C frox Sys.dual ->
- ^
-Expected 'from'
-Expected 'from' (15), (1,16)
-
-Select ->
- ^
-Expected '*'
-Expected '*' (6), (1,7)
-
-Select &&& frox Sys.dual ->
- ^
-Expected '*'
-Expected '*' (7), (1,8)
-
->Exit code: 0
-""" \ No newline at end of file
+if __name__ == "__main__":
+ simpleSQL.runTests("""\
+ SELECT * from XYZZY, ABC
+ select * from SYS.XYZZY
+ Select A from Sys.dual
+ Select A,B,C from Sys.dual
+ Select A, B, C from Sys.dual
+ Select A, B, C from Sys.dual, Table2
+ Xelect A, B, C from Sys.dual
+ Select A, B, C frox Sys.dual
+ Select
+ Select &&& frox Sys.dual
+ Select A from Sys.dual where a in ('RED','GREEN','BLUE')
+ Select A from Sys.dual where a in ('RED','GREEN','BLUE') and b in (10,20,30)
+ Select A,b from table1,table2 where table1.id eq table2.id -- test out comparison operators""")
diff --git a/src/examples/urlExtractorNew.py b/src/examples/urlExtractorNew.py
index 0569b6c..0aac875 100644
--- a/src/examples/urlExtractorNew.py
+++ b/src/examples/urlExtractorNew.py
@@ -12,7 +12,7 @@ import pprint
# that it is not necessary to explicitly show this in the pyparsing grammar; by default,
# pyparsing skips over whitespace between tokens.
linkOpenTag,linkCloseTag = makeHTMLTags("a")
-link = linkOpenTag + SkipTo(linkCloseTag).setResultsName("body") + linkCloseTag.suppress()
+link = linkOpenTag + SkipTo(linkCloseTag)("body") + linkCloseTag.suppress()
# Go get some HTML with some links in it.
serverListPage = urllib.request.urlopen( "http://www.google.com" )