summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2015-12-31 00:55:47 +0000
committerptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2015-12-31 00:55:47 +0000
commit92562084aa4c90ac21dc868edb12c8f6c0cf1a2b (patch)
tree9570287813d7358750ddd1ffe50ba60c0a999972
parent289665740e0bbd7b752d6bcb83a0da9ea331d5c2 (diff)
downloadpyparsing-92562084aa4c90ac21dc868edb12c8f6c0cf1a2b.tar.gz
Added new example parseTabularData.py
Updated several examples to more current Python and pyparsing practices (and better Py2/Py3 cross-compatibility) git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/trunk@309 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b
-rw-r--r--src/CHANGES6
-rw-r--r--src/examples/datetimeParseActions.py24
-rw-r--r--src/examples/jsonParser.py2
-rw-r--r--src/examples/listAllMatches.py34
-rw-r--r--src/examples/lucene_grammar.py4
-rw-r--r--src/examples/macroExpander.py7
-rw-r--r--src/examples/mozillaCalendarParser.py5
-rw-r--r--src/examples/oc.py4
-rw-r--r--src/examples/parseListString.py42
-rw-r--r--src/examples/parseTabularData.py50
-rw-r--r--src/examples/pgn.py6
-rw-r--r--src/examples/protobuf_parser.py9
-rw-r--r--src/examples/rangeCheck.py43
-rw-r--r--src/examples/romanNumerals.py13
-rw-r--r--src/examples/searchParserAppDemo.py2
-rw-r--r--src/examples/searchparser.py109
16 files changed, 191 insertions, 169 deletions
diff --git a/src/CHANGES b/src/CHANGES
index e7956b1..007fcb8 100644
--- a/src/CHANGES
+++ b/src/CHANGES
@@ -19,6 +19,12 @@ Version 2.0.7 -
- Fixed bug in ignore() that was introduced in pyparsing 1.5.3, that would
not accept a string literal as the ignore expression.
+- Added new example parseTabularData.py to illustrate parsing of data
+ formatted in columns, with detection of empty cells.
+
+- Updated a number of examples to more current Python and pyparsing
+ forms.
+
Version 2.0.6 -
---------------------------
diff --git a/src/examples/datetimeParseActions.py b/src/examples/datetimeParseActions.py
index 38e660e..26d96a3 100644
--- a/src/examples/datetimeParseActions.py
+++ b/src/examples/datetimeParseActions.py
@@ -10,7 +10,7 @@ from pyparsing import *
# define an integer string, and a parse action to convert it
# to an integer at parse time
-integer = Word(nums)
+integer = Word(nums).setName("integer")
def convertToInt(tokens):
# no need to test for validity - we can't get here
# unless tokens[0] contains all numeric digits
@@ -20,28 +20,24 @@ integer.setParseAction(convertToInt)
#integer = Word(nums).setParseAction(lambda t: int(t[0]))
# define a pattern for a year/month/day date
-date = integer('year') + '/' + integer('month') + '/' + integer('day')
+date_expr = integer('year') + '/' + integer('month') + '/' + integer('day')
def convertToDatetime(s,loc,tokens):
try:
# note that the year, month, and day fields were already
# converted to ints from strings by the parse action defined
# on the integer expression above
- return datetime(tokens.year, tokens.month, tokens.day)
+ return datetime(tokens.year, tokens.month, tokens.day).date()
except Exception as ve:
errmsg = "'%d/%d/%d' is not a valid date, %s" % \
(tokens.year, tokens.month, tokens.day, ve)
raise ParseException(s, loc, errmsg)
-date.setParseAction(convertToDatetime)
+date_expr.setParseAction(convertToDatetime)
-def test(s):
- try:
- print(date.parseString(s))
- except ParseException as pe:
- print(pe)
-
-test("2000/1/1")
-test("2000/13/1") # invalid month
-test("1900/2/29") # 1900 was not a leap year
-test("2000/2/29") # but 2000 was
+date_expr.runTests("""\
+ 2000/1/1
+ 2000/13/1 # invalid month
+ 1900/2/29 # 1900 was not a leap year
+ 2000/2/29 # but 2000 was
+ """) \ No newline at end of file
diff --git a/src/examples/jsonParser.py b/src/examples/jsonParser.py
index 5149b22..45cdef3 100644
--- a/src/examples/jsonParser.py
+++ b/src/examples/jsonParser.py
@@ -45,7 +45,7 @@ jsonNumber = Combine( Optional('-') + ( '0' | Word('123456789',nums) ) +
jsonObject = Forward()
jsonValue = Forward()
jsonElements = delimitedList( jsonValue )
-jsonArray = Group(Suppress('[') + Optional(jsonElements) + Suppress(']') )
+jsonArray = Group(Suppress('[') + Optional(jsonElements, []) + Suppress(']') )
jsonValue << ( jsonString | jsonNumber | Group(jsonObject) | jsonArray | TRUE | FALSE | NULL )
memberDef = Group( jsonString + Suppress(':') + jsonValue )
jsonMembers = delimitedList( memberDef )
diff --git a/src/examples/listAllMatches.py b/src/examples/listAllMatches.py
index 43f998b..1b1bdd4 100644
--- a/src/examples/listAllMatches.py
+++ b/src/examples/listAllMatches.py
@@ -12,13 +12,13 @@ nonAlphas = [ c for c in printables if not c.isalpha() ]
print("Extract vowels, consonants, and special characters from this test string:")
print("'" + test + "'")
-print()
+print('')
print("Define grammar using normal results names")
print("(only last matching symbol is saved)")
-vowels = oneOf(list("aeiouy"), caseless=True).setResultsName("vowels")
-cons = oneOf(list("bcdfghjklmnpqrstvwxz"), caseless=True).setResultsName("cons")
-other = oneOf(list(nonAlphas)).setResultsName("others")
+vowels = oneOf(list("aeiouy"), caseless=True)("vowels")
+cons = oneOf(list("bcdfghjklmnpqrstvwxz"), caseless=True)("cons")
+other = oneOf(nonAlphas)("others")
letters = OneOrMore(cons | vowels | other) + StringEnd()
results = letters.parseString(test)
@@ -26,27 +26,27 @@ print(results)
print(results.vowels)
print(results.cons)
print(results.others)
-print()
+print('')
print("Define grammar using results names, with listAllMatches=True")
print("(all matching symbols are saved)")
-vowels = oneOf(list("aeiouy"), caseless=True).setResultsName("vowels",listAllMatches=True)
-cons = oneOf(list("bcdfghjklmnpqrstvwxz"), caseless=True).setResultsName("cons",listAllMatches=True)
-other = oneOf(list(nonAlphas)).setResultsName("others",listAllMatches=True)
+vowels = oneOf(list("aeiouy"), caseless=True)("vowels*")
+cons = oneOf(list("bcdfghjklmnpqrstvwxz"), caseless=True)("cons*")
+other = oneOf(nonAlphas)("others*")
-letters = OneOrMore(cons | vowels | other) + StringEnd()
+letters = OneOrMore(cons | vowels | other)
-results = letters.parseString(test)
+results = letters.parseString(test, parseAll=True)
print(results)
-print(sorted(list(set(results))))
-print()
+print(sorted(set(results)))
+print('')
print(results.vowels)
-print(sorted(list(set(results.vowels))))
-print()
+print(sorted(set(results.vowels)))
+print('')
print(results.cons)
-print(sorted(list(set(results.cons))))
-print()
+print(sorted(set(results.cons)))
+print('')
print(results.others)
-print(sorted(list(set(results.others))))
+print(sorted(set(results.others)))
diff --git a/src/examples/lucene_grammar.py b/src/examples/lucene_grammar.py
index c098cce..85e294b 100644
--- a/src/examples/lucene_grammar.py
+++ b/src/examples/lucene_grammar.py
@@ -325,6 +325,6 @@ for t in [_f for _f in map(str.strip,tests) if _f]:
print(t)
print(pe)
allpass = False
- print()
+ print('')
-print(("OK", "FAIL")[not allpass])
+print(("FAIL", "OK")[allpass])
diff --git a/src/examples/macroExpander.py b/src/examples/macroExpander.py
index 89562a4..327976c 100644
--- a/src/examples/macroExpander.py
+++ b/src/examples/macroExpander.py
@@ -16,11 +16,12 @@ from pyparsing import *
# define the structure of a macro definition (the empty term is used
# to advance to the next non-whitespace character)
-macroDef = "#def" + Word(alphas+"_",alphanums+"_").setResultsName("macro") + \
- empty + restOfLine.setResultsName("value")
+identifier = Word(alphas+"_",alphanums+"_")
+macroDef = "#def" + identifier("macro") + empty + restOfLine("value")
# define a placeholder for defined macros - initially nothing
macroExpr = Forward()
+macroExpr << NoMatch()
# global dictionary for macro definitions
macros = {}
@@ -29,7 +30,7 @@ macros = {}
def processMacroDefn(s,l,t):
macroVal = macroExpander.transformString(t.value)
macros[t.macro] = macroVal
- macroExpr << MatchFirst( list(map(Keyword,list(macros.keys()))) )
+ macroExpr << MatchFirst(map(Keyword, macros.keys()))
return "#def " + t.macro + " " + macroVal
# parse action to replace macro references with their respective definition
diff --git a/src/examples/mozillaCalendarParser.py b/src/examples/mozillaCalendarParser.py
index 2805c95..210f284 100644
--- a/src/examples/mozillaCalendarParser.py
+++ b/src/examples/mozillaCalendarParser.py
@@ -16,8 +16,7 @@ License: Free for any use
BEGIN = Literal("BEGIN:").suppress()
END = Literal("END:").suppress()
-str = printables + "\xe4\xf6\xe5\xd6\xc4\xc5"
-valstr = str + " "
+valstr = printables + "\xe4\xf6\xe5\xd6\xc4\xc5 "
EQ = Literal("=").suppress()
SEMI = Literal(";").suppress()
@@ -70,7 +69,7 @@ calendars = OneOrMore(calendar)
def gotEvent(s,loc,toks):
for event in toks:
- print (event['summary'], "from", event["begin"], "to", event["end"])
+ print(event.dump())
event.setParseAction(gotEvent)
diff --git a/src/examples/oc.py b/src/examples/oc.py
index 5a1ff8e..1d49190 100644
--- a/src/examples/oc.py
+++ b/src/examples/oc.py
@@ -190,6 +190,4 @@ main()
"""
ast = program.parseString(test,parseAll=True)
-
-import pprint
-pprint.pprint(ast.asList())
+ast.pprint()
diff --git a/src/examples/parseListString.py b/src/examples/parseListString.py
index a6f07a1..d74f3af 100644
--- a/src/examples/parseListString.py
+++ b/src/examples/parseListString.py
@@ -27,8 +27,7 @@ rbrack = Literal("]").suppress()
cvtInt = lambda s,l,toks: int(toks[0])
integer = Word(nums).setName("integer").setParseAction( cvtInt )
cvtReal = lambda s,l,toks: float(toks[0])
-real = Combine(Optional(oneOf("+ -")) + Word(nums) + "." +
- Optional(Word(nums))).setName("real").setParseAction( cvtReal )
+real = Regex(r'[+-]?\d+\.\d*').setName("floating-point number").setParseAction( cvtReal )
listItem = real | integer | quotedString.setParseAction( removeQuotes )
listStr = lbrack + delimitedList(listItem) + rbrack
@@ -44,8 +43,7 @@ cvtReal = lambda s,l,toks: float(toks[0])
lbrack = Literal("[").suppress()
rbrack = Literal("]").suppress()
integer = Word(nums).setName("integer").setParseAction( cvtInt )
-real = Combine(Optional(oneOf("+ -")) + Word(nums) + "." +
- Optional(Word(nums))).setName("real").setParseAction( cvtReal )
+real = Regex(r'[+-]?\d+\.\d*').setName("floating-point number").setParseAction( cvtReal )
tupleStr = Forward()
listStr = Forward()
listItem = real | integer | quotedString.setParseAction(removeQuotes) | Group(listStr) | tupleStr
@@ -56,25 +54,7 @@ listStr << lbrack + delimitedList(listItem) + Optional(Suppress(",")) + rbrack
test = "['a', 100, ('A', [101,102]), 3.14, [ +2.718, 'xyzzy', -1.414] ]"
print(listStr.parseString(test))
-# fourth pass, just parsing tuples of numbers
-#~ from pyparsing import *
-
-#~ integer = (Word(nums)|Word('-+',nums)).setName("integer")
-#~ real = Combine(integer + "." + Optional(Word(nums))).setName("real")
-#~ tupleStr = Forward().setName("tuple")
-#~ tupleItem = real | integer | tupleStr
-#~ tupleStr << ( Suppress("(") + delimitedList(tupleItem) +
- #~ Optional(Suppress(",")) + Suppress(")") )
-
-#~ # add parse actions to do conversion during parsing
-#~ integer.setParseAction( lambda toks: int(toks[0]) )
-#~ real.setParseAction( lambda toks: float(toks[0]) )
-#~ tupleStr.setParseAction( lambda toks: tuple(toks) )
-
-#~ s = '((1,2), (3,4), (-5,9.2),)'
-#~ print tupleStr.parseString(s)[0]
-
-
+# fourth pass, add parsing of dicts
cvtInt = lambda s,l,toks: int(toks[0])
cvtReal = lambda s,l,toks: float(toks[0])
cvtDict = lambda s,l,toks: dict(toks[0])
@@ -85,16 +65,18 @@ lbrace = Literal("{").suppress()
rbrace = Literal("}").suppress()
colon = Literal(":").suppress()
integer = Word(nums).setName("integer").setParseAction( cvtInt )
-real = Combine(Optional(oneOf("+ -")) + Word(nums) + "." +
- Optional(Word(nums))).setName("real").setParseAction( cvtReal )
+real = Regex(r'[+-]?\d+\.\d*').setName("real").setParseAction( cvtReal )
+
tupleStr = Forward()
listStr = Forward()
dictStr = Forward()
listItem = real | integer | quotedString.setParseAction(removeQuotes) | Group(listStr) | tupleStr | dictStr
-tupleStr << ( Suppress("(") + delimitedList(listItem) + Optional(Suppress(",")) + Suppress(")") )
+tupleStr <<= ( Suppress("(") + delimitedList(listItem) + Optional(Suppress(",")) + Suppress(")") )
tupleStr.setParseAction( lambda t:tuple(t.asList()) )
-listStr << lbrack + delimitedList(listItem) + Optional(Suppress(",")) + rbrack
-dictStr << rbrace + delimitedList( Group( listItem + colon + listItem ) ) + rbrace
-test = "['a', 100, ('A', [101,102]), 3.14, [ +2.718, 'xyzzy', -1.414] ]"
-test = '[{0: [2], 1: []}, {0: [], 1: [], 2: []}, {0: [1, 2]}]'
+listStr <<= (lbrack + Optional(delimitedList(listItem)) + Optional(Suppress(",")) + rbrack)
+dictKeyStr = real | integer | quotedString.setParseAction(removeQuotes)
+dictStr <<= lbrace + Optional(delimitedList( Group( dictKeyStr + colon + listItem ))) + Optional(Suppress(",")) + rbrace
+dictStr.setParseAction(lambda t: dict((k_v[0],(k_v[1].asList() if isinstance(k_v[1],ParseResults) else k_v[1])) for k_v in t))
+
+test = '[{0: [2], 1: []}, {0: [], 1: [], 2: [,]}, {0: [1, 2,],}]'
print(listStr.parseString(test))
diff --git a/src/examples/parseTabularData.py b/src/examples/parseTabularData.py
new file mode 100644
index 0000000..3846310
--- /dev/null
+++ b/src/examples/parseTabularData.py
@@ -0,0 +1,50 @@
+#
+# parseTabularData.py
+#
+# Example of parsing data that is formatted in a tabular listing, with
+# potential for missing values. Uses new addCondition method on
+# ParserElements.
+#
+# Copyright 2015, Paul McGuire
+#
+from pyparsing import col,Word,Optional,alphas,nums,ParseException
+
+table = """\
+ 1 2
+12345678901234567890
+COLOR S M L
+RED 10 2 2
+BLUE 5 10
+GREEN 3 5
+PURPLE 8"""
+
+# function to create column-specific parse conditions
+def mustMatchCols(startloc,endloc):
+ return lambda s,l,t: startloc <= col(l,s) <= endloc
+
+# helper to define values in a space-delimited table
+# (change empty_cell_is_zero to True if a value of 0 is desired for empty cells)
+def tableValue(expr, colstart, colend):
+ empty_cell_is_zero = False
+ if empty_cell_is_zero:
+ return Optional(expr.copy().addCondition(mustMatchCols(colstart,colend),
+ message="text not in expected columns"),
+ default=0)
+ else:
+ return Optional(expr.copy().addCondition(mustMatchCols(colstart,colend),
+ message="text not in expected columns"))
+
+
+# define the grammar for this simple table
+colorname = Word(alphas)
+integer = Word(nums).setParseAction(lambda t: int(t[0])).setName("integer")
+row = (colorname("name") +
+ tableValue(integer, 11, 12)("S") +
+ tableValue(integer, 15, 16)("M") +
+ tableValue(integer, 19, 20)("L"))
+
+# parse the sample text - skip over the header and counter lines
+for line in table.splitlines()[3:]:
+ print(line)
+ print(row.parseString(line).dump())
+ print('')
diff --git a/src/examples/pgn.py b/src/examples/pgn.py
index c645f73..d13f83e 100644
--- a/src/examples/pgn.py
+++ b/src/examples/pgn.py
@@ -26,8 +26,8 @@ file_coord = oneOf("a b c d e f g h")
rank_coord = oneOf("1 2 3 4 5 6 7 8")
capture = oneOf("x :")
promote = Literal("=")
-castle_queenside = Literal("O-O-O") | Literal("0-0-0") | Literal("o-o-o")
-castle_kingside = Literal("O-O") | Literal("0-0") | Literal("o-o")
+castle_queenside = oneOf("O-O-O 0-0-0 o-o-o")
+castle_kingside = oneOf("O-O 0-0 o-o")
move_number = Optional(comment) + Word(nums) + dot
m1 = file_coord + rank_coord # pawn move e.g. d4
@@ -91,4 +91,4 @@ Bxe5 Rxe5 21. Rg5 Rxe1# {Black wins} 0-1
"""
# parse input string
tokens = parsePGN(pgn, pgnGrammar)
- print("tokens = ", tokens)
+ print(tokens.dump())
diff --git a/src/examples/protobuf_parser.py b/src/examples/protobuf_parser.py
index 8940e14..04ce0d8 100644
--- a/src/examples/protobuf_parser.py
+++ b/src/examples/protobuf_parser.py
@@ -7,7 +7,7 @@
from pyparsing import (Word, alphas, alphanums, Regex, Suppress, Forward,
Group, oneOf, ZeroOrMore, Optional, delimitedList, Keyword,
- restOfLine, quotedString)
+ restOfLine, quotedString, Dict)
ident = Word(alphas+"_",alphanums+"_").setName("identifier")
integer = Regex(r"[+-]?\d+")
@@ -31,7 +31,7 @@ fieldDefn = (( REQUIRED_ | OPTIONAL_ | REPEATED_ )("fieldQualifier") -
typespec("typespec") + ident("ident") + EQ + integer("fieldint") + ZeroOrMore(fieldDirective) + SEMI)
# enumDefn ::= 'enum' ident '{' { ident '=' integer ';' }* '}'
-enumDefn = ENUM_ - ident + LBRACE + ZeroOrMore( Group(ident + EQ + integer + SEMI) ) + RBRACE
+enumDefn = ENUM_("typespec") - ident('name') + LBRACE + Dict( ZeroOrMore( Group(ident + EQ + integer + SEMI) ))('values') + RBRACE
# extensionsDefn ::= 'extensions' integer 'to' integer ';'
extensionsDefn = EXTENSIONS_ - integer + TO_ + integer + SEMI
@@ -97,7 +97,4 @@ message AddressBook {
repeated Person person = 1;
}"""
-from pprint import pprint
-#~ print parser.parseString(test2, parseAll=True).dump()
-pprint( parser.parseString(test2, parseAll=True).asList())
-
+parser.runTests([test1, test2])
diff --git a/src/examples/rangeCheck.py b/src/examples/rangeCheck.py
index 35c9ef3..59bd49c 100644
--- a/src/examples/rangeCheck.py
+++ b/src/examples/rangeCheck.py
@@ -3,46 +3,42 @@
# A sample program showing how parse actions can convert parsed
# strings into a data type or object, and to validate the parsed value.
#
-# Copyright 2011, Paul T. McGuire
+# Updated to use new addCondition method and expr() copy.
+#
+# Copyright 2011,2015 Paul T. McGuire
#
from pyparsing import Word, nums, Suppress, ParseException, empty, Optional
from datetime import datetime
-def rangeCheck(minval=None, maxval=None):
+def ranged_value(expr, minval=None, maxval=None):
# have to specify at least one range boundary
if minval is None and maxval is None:
raise ValueError("minval or maxval must be specified")
-
+
# set range testing function and error message depending on
# whether either or both min and max values are given
- inRangeFn = {
- (True, False) : lambda x : x <= maxval,
- (False, True) : lambda x : minval <= x,
- (False, False) : lambda x : minval <= x <= maxval,
+ inRangeCondition = {
+ (True, False) : lambda s,l,t : t[0] <= maxval,
+ (False, True) : lambda s,l,t : minval <= t[0],
+ (False, False) : lambda s,l,t : minval <= t[0] <= maxval,
}[minval is None, maxval is None]
outOfRangeMessage = {
- (True, False) : "value %%s is greater than %s" % maxval,
- (False, True) : "value %%s is less than %s" % minval,
- (False, False) : "value %%s is not in the range (%s to %s)" % (minval,maxval),
+ (True, False) : "value is greater than %s" % maxval,
+ (False, True) : "value is less than %s" % minval,
+ (False, False) : "value is not in the range (%s to %s)" % (minval,maxval),
}[minval is None, maxval is None]
- # define the actual range checking parse action
- def rangeCheckParseAction(string, loc, tokens):
- parsedval = tokens[0]
- if not inRangeFn(parsedval):
- raise ParseException(string, loc, outOfRangeMessage % parsedval)
-
- return rangeCheckParseAction
+ return expr().addCondition(inRangeCondition, message=outOfRangeMessage)
# define the expressions for a date of the form YYYY/MM/DD or YYYY/MM (assumes YYYY/MM/01)
integer = Word(nums).setName("integer")
integer.setParseAction(lambda t:int(t[0]))
-month = integer.copy().addParseAction(rangeCheck(1,12))
-day = integer.copy().addParseAction(rangeCheck(1,31))
-year = integer.copy().addParseAction(rangeCheck(2000, None))
-
+month = ranged_value(integer, 1, 12)
+day = ranged_value(integer, 1, 31)
+year = ranged_value(integer, 2000, None)
+
SLASH = Suppress('/')
dateExpr = year("year") + SLASH + month("month") + Optional(SLASH + day("day"))
dateExpr.setName("date")
@@ -53,7 +49,7 @@ dateExpr.setParseAction(lambda t: datetime(t.year, t.month, t.day or 1).date())
# add range checking on dates
mindate = datetime(2002,1,1).date()
maxdate = datetime.now().date()
-dateExpr.addParseAction(rangeCheck(mindate, maxdate))
+dateExpr = ranged_value(dateExpr, mindate, maxdate)
tests = """
@@ -62,6 +58,7 @@ tests = """
2004/2/29
2004/2/30
2004/2
+ 1999/12/31
""".splitlines()
for t in tests:
t = t.strip()
@@ -71,6 +68,6 @@ for t in tests:
print(dateExpr.parseString(t)[0])
except Exception as e:
print(str(e))
- print()
+ print('')
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")
diff --git a/src/examples/searchParserAppDemo.py b/src/examples/searchParserAppDemo.py
index 57749f4..259e0e3 100644
--- a/src/examples/searchParserAppDemo.py
+++ b/src/examples/searchParserAppDemo.py
@@ -31,4 +31,4 @@ tests = """\
for t in tests:
print(t.strip())
print(parser.Parse(t))
- print() \ No newline at end of file
+ print('') \ No newline at end of file
diff --git a/src/examples/searchparser.py b/src/examples/searchparser.py
index 86e30ca..e5b40a7 100644
--- a/src/examples/searchparser.py
+++ b/src/examples/searchparser.py
@@ -58,7 +58,6 @@ TODO:
- add more kinds of wildcards ('*' at the beginning and '*' inside a word)?
"""
from pyparsing import Word, alphanums, Keyword, Group, Combine, Forward, Suppress, Optional, OneOrMore, oneOf
-from sets import Set
class SearchQueryParser:
@@ -144,7 +143,7 @@ class SearchQueryParser:
function GetQuoted to only return the subset of ID's that contain the
literal string.
"""
- r = Set()
+ r = set()
search_terms = []
for item in argument:
search_terms.append(item[0])
@@ -168,16 +167,16 @@ class SearchQueryParser:
return self.evaluate(self._parser(query)[0])
def GetWord(self, word):
- return Set()
+ return set()
def GetWordWildcard(self, word):
- return Set()
+ return set()
def GetQuotes(self, search_string, tmp_result):
- return Set()
+ return set()
def GetNot(self, not_set):
- return Set().difference(not_set)
+ return set().difference(not_set)
class ParserTest(SearchQueryParser):
@@ -185,40 +184,40 @@ class ParserTest(SearchQueryParser):
tests containts a dictionary with tests and expected results.
"""
tests = {
- 'help': Set([1, 2, 4, 5]),
- 'help or hulp': Set([1, 2, 3, 4, 5]),
- 'help and hulp': Set([2]),
- 'help hulp': Set([2]),
- 'help and hulp or hilp': Set([2, 3, 4]),
- 'help or hulp and hilp': Set([1, 2, 3, 4, 5]),
- 'help or hulp or hilp or halp': Set([1, 2, 3, 4, 5, 6]),
- '(help or hulp) and (hilp or halp)': Set([3, 4, 5]),
- 'help and (hilp or halp)': Set([4, 5]),
- '(help and (hilp or halp)) or hulp': Set([2, 3, 4, 5]),
- 'not help': Set([3, 6, 7, 8]),
- 'not hulp and halp': Set([5, 6]),
- 'not (help and halp)': Set([1, 2, 3, 4, 6, 7, 8]),
- '"help me please"': Set([2]),
- '"help me please" or hulp': Set([2, 3]),
- '"help me please" or (hulp and halp)': Set([2]),
- 'help*': Set([1, 2, 4, 5, 8]),
- 'help or hulp*': Set([1, 2, 3, 4, 5]),
- 'help* and hulp': Set([2]),
- 'help and hulp* or hilp': Set([2, 3, 4]),
- 'help* or hulp or hilp or halp': Set([1, 2, 3, 4, 5, 6, 8]),
- '(help or hulp*) and (hilp* or halp)': Set([3, 4, 5]),
- 'help* and (hilp* or halp*)': Set([4, 5]),
- '(help and (hilp* or halp)) or hulp*': Set([2, 3, 4, 5]),
- 'not help* and halp': Set([6]),
- 'not (help* and helpe*)': Set([1, 2, 3, 4, 5, 6, 7]),
- '"help* me please"': Set([2]),
- '"help* me* please" or hulp*': Set([2, 3]),
- '"help me please*" or (hulp and halp)': Set([2]),
- '"help me please" not (hulp and halp)': Set([2]),
- '"help me please" hulp': Set([2]),
- 'help and hilp and not holp': Set([4]),
- 'help hilp not holp': Set([4]),
- 'help hilp and not holp': Set([4]),
+ 'help': set([1, 2, 4, 5]),
+ 'help or hulp': set([1, 2, 3, 4, 5]),
+ 'help and hulp': set([2]),
+ 'help hulp': set([2]),
+ 'help and hulp or hilp': set([2, 3, 4]),
+ 'help or hulp and hilp': set([1, 2, 3, 4, 5]),
+ 'help or hulp or hilp or halp': set([1, 2, 3, 4, 5, 6]),
+ '(help or hulp) and (hilp or halp)': set([3, 4, 5]),
+ 'help and (hilp or halp)': set([4, 5]),
+ '(help and (hilp or halp)) or hulp': set([2, 3, 4, 5]),
+ 'not help': set([3, 6, 7, 8]),
+ 'not hulp and halp': set([5, 6]),
+ 'not (help and halp)': set([1, 2, 3, 4, 6, 7, 8]),
+ '"help me please"': set([2]),
+ '"help me please" or hulp': set([2, 3]),
+ '"help me please" or (hulp and halp)': set([2]),
+ 'help*': set([1, 2, 4, 5, 8]),
+ 'help or hulp*': set([1, 2, 3, 4, 5]),
+ 'help* and hulp': set([2]),
+ 'help and hulp* or hilp': set([2, 3, 4]),
+ 'help* or hulp or hilp or halp': set([1, 2, 3, 4, 5, 6, 8]),
+ '(help or hulp*) and (hilp* or halp)': set([3, 4, 5]),
+ 'help* and (hilp* or halp*)': set([4, 5]),
+ '(help and (hilp* or halp)) or hulp*': set([2, 3, 4, 5]),
+ 'not help* and halp': set([6]),
+ 'not (help* and helpe*)': set([1, 2, 3, 4, 5, 6, 7]),
+ '"help* me please"': set([2]),
+ '"help* me* please" or hulp*': set([2, 3]),
+ '"help me please*" or (hulp and halp)': set([2]),
+ '"help me please" not (hulp and halp)': set([2]),
+ '"help me please" hulp': set([2]),
+ 'help and hilp and not holp': set([4]),
+ 'help hilp not holp': set([4]),
+ 'help hilp and not holp': set([4]),
}
docs = {
@@ -233,41 +232,41 @@ class ParserTest(SearchQueryParser):
}
index = {
- 'help': Set((1, 2, 4, 5)),
- 'me': Set((2,)),
- 'please': Set((2,)),
- 'hulp': Set((2, 3,)),
- 'hilp': Set((3, 4,)),
- 'halp': Set((5, 6,)),
- 'thinks': Set((5,)),
- 'he': Set((5, 6,)),
- 'needs': Set((5, 6,)),
- 'nothing': Set((7,)),
- 'helper': Set((8,)),
+ 'help': set((1, 2, 4, 5)),
+ 'me': set((2,)),
+ 'please': set((2,)),
+ 'hulp': set((2, 3,)),
+ 'hilp': set((3, 4,)),
+ 'halp': set((5, 6,)),
+ 'thinks': set((5,)),
+ 'he': set((5, 6,)),
+ 'needs': set((5, 6,)),
+ 'nothing': set((7,)),
+ 'helper': set((8,)),
}
def GetWord(self, word):
if (word in self.index):
return self.index[word]
else:
- return Set()
+ return set()
def GetWordWildcard(self, word):
- result = Set()
+ result = set()
for item in list(self.index.keys()):
if word == item[0:len(word)]:
result = result.union(self.index[item])
return result
def GetQuotes(self, search_string, tmp_result):
- result = Set()
+ result = set()
for item in tmp_result:
if self.docs[item].count(search_string):
result.add(item)
return result
def GetNot(self, not_set):
- all = Set(list(self.docs.keys()))
+ all = set(list(self.docs.keys()))
return all.difference(not_set)
def Test(self):