summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2016-08-07 14:54:01 +0000
committerptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2016-08-07 14:54:01 +0000
commit53d58687f318cfa9100c8b4e56e9c51725276f8a (patch)
treee9abae9c616fdfdfebd9a8a0931d8f21824f30d2
parentd0b75781d235753b5ddb99de0bed2cd11660bbba (diff)
downloadpyparsing-53d58687f318cfa9100c8b4e56e9c51725276f8a.tar.gz
Update to current pyparsing features
git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/trunk@400 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b
-rw-r--r--src/examples/deltaTime.py15
-rw-r--r--src/examples/select_parser.py16
-rw-r--r--src/examples/urlExtractor.py35
3 files changed, 21 insertions, 45 deletions
diff --git a/src/examples/deltaTime.py b/src/examples/deltaTime.py
index a63b19e..e38da00 100644
--- a/src/examples/deltaTime.py
+++ b/src/examples/deltaTime.py
@@ -202,14 +202,7 @@ if __name__ == "__main__":
noon Sunday
noon last Sunday
2pm next Sunday
- next Sunday at 2pm""".splitlines()
-
- for t in tests:
- t = t.strip()
- print(t, "(relative to %s)" % datetime.now())
- res = nlTimeExpression.parseString(t)
- if "calculatedTime" in res:
- print(res.calculatedTime)
- else:
- print("???")
- print('')
+ next Sunday at 2pm"""
+
+ print("(relative to %s)" % datetime.now())
+ nlTimeExpression.runTests(tests)
diff --git a/src/examples/select_parser.py b/src/examples/select_parser.py
index 7c53325..da106ac 100644
--- a/src/examples/select_parser.py
+++ b/src/examples/select_parser.py
@@ -112,8 +112,7 @@ tests = """\
select * from xyzzy where z > 100
select * from xyzzy where z > 100 order by zz
select * from xyzzy
- select z.* from xyzzy""".splitlines()
-tests = """\
+ select z.* from xyzzy
select a, b from test_table where 1=1 and b='yes'
select a, b from test_table where 1=1 and b in (select bb from foo)
select z.a, b from test_table where 1=1 and b in (select bb from foo)
@@ -122,13 +121,6 @@ tests = """\
select a, db.table.b as BBB from db.table where 1=1 and BBB='yes'
select a, db.table.b as BBB from test_table,db.table where 1=1 and BBB='yes'
select a, db.table.b as BBB from test_table,db.table where 1=1 and BBB='yes' limit 50
- """.splitlines()
-for t in tests:
- t = t.strip()
- if not t: continue
- print(t)
- try:
- print(select_stmt.parseString(t).dump())
- except ParseException as pe:
- print(pe.msg)
- print()
+ """
+
+select_stmt.runTests(tests)
diff --git a/src/examples/urlExtractor.py b/src/examples/urlExtractor.py
index 7c90bd7..2c66d78 100644
--- a/src/examples/urlExtractor.py
+++ b/src/examples/urlExtractor.py
@@ -1,26 +1,21 @@
# URL extractor
# Copyright 2004, Paul McGuire
-from pyparsing import Literal,Suppress,CharsNotIn,CaselessLiteral,\
- Word,dblQuotedString,alphanums,SkipTo
-import urllib.request, urllib.parse, urllib.error
+from pyparsing import makeHTMLTags, SkipTo, pyparsing_common
+import urllib.request
+from contextlib import closing
import pprint
-# Define the pyparsing grammar for a URL, that is:
-# URLlink ::= <a href= URL>linkText</a>
-# URL ::= doubleQuotedString | alphanumericWordPath
-# Note that whitespace may appear just about anywhere in the link. Note also
-# that it is not necessary to explicitly show this in the pyparsing grammar; by default,
-# pyparsing skips over whitespace between tokens.
-linkOpenTag = (Literal("<") + "a" + "href" + "=").suppress() + \
- ( dblQuotedString | Word(alphanums+"/") ) + \
- Suppress(">")
-linkCloseTag = Literal("<") + "/" + CaselessLiteral("a") + ">"
-link = linkOpenTag + SkipTo(linkCloseTag) + linkCloseTag.suppress()
+linkOpenTag, linkCloseTag = makeHTMLTags('a')
+
+linkBody = SkipTo(linkCloseTag)
+linkBody.setParseAction(pyparsing_common.stripHTMLTags)
+linkBody.addParseAction(lambda toks: ' '.join(toks[0].strip().split()))
+
+link = linkOpenTag + linkBody("body") + linkCloseTag.suppress()
# Go get some HTML with some links in it.
-serverListPage = urllib.request.urlopen( "http://www.yahoo.com" )
-htmlText = serverListPage.read()
-serverListPage.close()
+with closing(urllib.request.urlopen("http://www.yahoo.com")) as serverListPage:
+ htmlText = serverListPage.read().decode("UTF-8")
# scanString is a generator that loops through the input htmlText, and for each
# match yields the tokens and start and end locations (for this application, we are
@@ -28,14 +23,10 @@ serverListPage.close()
for toks,strt,end in link.scanString(htmlText):
print(toks.asList())
-# Rerun scanString, but this time create a dict of text:URL key-value pairs.
-# Need to reverse the tokens returned by link, using a parse action.
-link.setParseAction( lambda st,loc,toks: [ toks[1], toks[0] ] )
-
# Create dictionary from list comprehension, assembled from each pair of tokens returned
# from a matched URL.
pprint.pprint(
- dict( [ toks for toks,strt,end in link.scanString(htmlText) ] )
+ dict((toks.body, toks.href) for toks,strt,end in link.scanString(htmlText))
)