From 7d2eba2687f39e0149b2f488c692d20efc972eba Mon Sep 17 00:00:00 2001 From: ptmcg Date: Fri, 26 Aug 2016 04:02:31 +0000 Subject: Copied upcaseTokens and downcaseTokens to pyparsing_common; renamed pyparsing_common's signedInteger and sciReal to signed_integer and sci_real git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/trunk@428 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b --- src/CHANGES | 22 ++++++++++++++++++++++ src/pyparsing.py | 38 +++++++++++++++++++++++++++----------- src/unitTests.py | 10 ++++++++++ 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/src/CHANGES b/src/CHANGES index 40c08ff..e32cc21 100644 --- a/src/CHANGES +++ b/src/CHANGES @@ -8,6 +8,28 @@ Version 2.1.9 - "close" matches, that is, strings with at most 'n' mismatching characters. +- Minor API change in pyparsing_common. Renamed some of the common + expressions to PEP8 format (to be consistent with the other + pyparsing_common expressions): + . signedInteger -> signed_integer + . sciReal -> sci_real + + Also, in trying to stem the API bloat of pyparsing, I've copied + some of the global expressions and helper parse actions into + pyparsing_common, with the originals to be deprecated and removed + in a future release: + . commaSeparatedList -> pyparsing_common.comma_separated_list + . upcaseTokens -> pyparsing_common.upcaseTokens + . downcaseTokens -> pyparsing_common.downcaseTokens + + (I don't expect any other expressions, like the comment expressions, + quotedString, or the Word-helping strings like alphas, nums, etc. + to migrate to pyparsing_common - they are just too pervasive. As for + the PEP8 vs camelCase naming, all the expressions are PEP8, while + the parse actions in pyparsing_common are still camelCase. It's a + small step - when pyparsing 3.0 comes around, everything will change + to PEP8 snake case.) + Version 2.1.8 - ------------------------------ diff --git a/src/pyparsing.py b/src/pyparsing.py index 269e789..50ffe84 100644 --- a/src/pyparsing.py +++ b/src/pyparsing.py @@ -61,7 +61,7 @@ The pyparsing module handles some of the problems that are typically vexing when """ __version__ = "2.1.9" -__versionTime__ = "17 Aug 2016 23:06 UTC" +__versionTime__ = "18 Aug 2016 13:32 UTC" __author__ = "Paul McGuire " import string @@ -4810,10 +4810,10 @@ def tokenMap(func, *args): return pa upcaseTokens = tokenMap(lambda t: _ustr(t).upper()) -"""Helper parse action to convert tokens to upper case.""" +"""(Deprecated) Helper parse action to convert tokens to upper case. Deprecated in favor of L{pyparsing_common.upcaseTokens}""" downcaseTokens = tokenMap(lambda t: _ustr(t).lower()) -"""Helper parse action to convert tokens to lower case.""" +"""(Deprecated) Helper parse action to convert tokens to lower case. Deprecated in favor of L{pyparsing_common.downcaseTokens}""" def _makeTags(tagStr, xml): """Internal helper to construct opening and closing tag expressions, given a tag name""" @@ -5005,7 +5005,7 @@ def infixNotation( baseExpr, opList, lpar=Suppress('('), rpar=Suppress(')') ): Example:: # simple example of four-function arithmetic with ints and variable names - integer = pyparsing_common.signedInteger + integer = pyparsing_common.signed_integer varname = pyparsing_common.identifier arith_expr = infixNotation(integer | varname, @@ -5325,23 +5325,27 @@ _commasepitem = Combine(OneOrMore(Word(printables, excludeChars=',') + Optional( Word(" \t") + ~Literal(",") + ~LineEnd() ) ) ).streamline().setName("commaItem") commaSeparatedList = delimitedList( Optional( quotedString.copy() | _commasepitem, default="") ).setName("commaSeparatedList") -"""Predefined expression of 1 or more printable words or quoted strings, separated by commas.""" +"""(Deprecated) Predefined expression of 1 or more printable words or quoted strings, separated by commas. + This expression is deprecated in favor of L{pyparsing_common.comma_separated_list}.""" # some other useful expressions - using lower-case class name since we are really using this as a namespace class pyparsing_common: """ Here are some common low-level expressions that may be useful in jump-starting parser development: - - numeric forms (L{integers}, L{reals}, L{scientific notation}) + - numeric forms (L{integers}, L{reals}, L{scientific notation}) - common L{programming identifiers} - network addresses (L{MAC}, L{IPv4}, L{IPv6}) - ISO8601 L{dates} and L{datetime} - L{UUID} + - L{comma-separated list} Parse actions: - C{L{convertToInteger}} - C{L{convertToFloat}} - C{L{convertToDate}} - C{L{convertToDatetime}} - C{L{stripHTMLTags}} + - C{L{upcaseTokens}} + - C{L{downcaseTokens}} Example:: pyparsing_common.number.runTests(''' @@ -5477,25 +5481,25 @@ class pyparsing_common: hex_integer = Word(hexnums).setName("hex integer").setParseAction(tokenMap(int,16)) """expression that parses a hexadecimal integer, returns an int""" - signedInteger = Regex(r'[+-]?\d+').setName("signed integer").setParseAction(convertToInteger) + signed_integer = Regex(r'[+-]?\d+').setName("signed integer").setParseAction(convertToInteger) """expression that parses an integer with optional leading sign, returns an int""" - fraction = (signedInteger().setParseAction(convertToFloat) + '/' + signedInteger().setParseAction(convertToFloat)).setName("fraction") + fraction = (signed_integer().setParseAction(convertToFloat) + '/' + signed_integer().setParseAction(convertToFloat)).setName("fraction") """fractional expression of an integer divided by an integer, returns a float""" fraction.addParseAction(lambda t: t[0]/t[-1]) - mixed_integer = (fraction | signedInteger + Optional(Optional('-').suppress() + fraction)).setName("fraction or mixed integer-fraction") + mixed_integer = (fraction | signed_integer + Optional(Optional('-').suppress() + fraction)).setName("fraction or mixed integer-fraction") """mixed integer of the form 'integer - fraction', with optional leading integer, returns float""" mixed_integer.addParseAction(sum) real = Regex(r'[+-]?\d+\.\d*').setName("real number").setParseAction(convertToFloat) """expression that parses a floating point number and returns a float""" - sciReal = Regex(r'[+-]?\d+([eE][+-]?\d+|\.\d*([eE][+-]?\d+)?)').setName("real number with scientific notation").setParseAction(convertToFloat) + sci_real = Regex(r'[+-]?\d+([eE][+-]?\d+|\.\d*([eE][+-]?\d+)?)').setName("real number with scientific notation").setParseAction(convertToFloat) """expression that parses a floating point number with optional scientific notation and returns a float""" # streamlining this expression makes the docs nicer-looking - number = (sciReal | real | signedInteger).streamline() + number = (sci_real | real | signed_integer).streamline() """any numeric expression, returns the corresponding Python type""" fnumber = Regex(r'[+-]?\d+\.?\d*([eE][+-]?\d+)?').setName("fnumber").setParseAction(convertToFloat) @@ -5587,6 +5591,18 @@ class pyparsing_common: """ return pyparsing_common._html_stripper.transformString(tokens[0]) + _commasepitem = Combine(OneOrMore(~Literal(",") + ~LineEnd() + Word(printables, excludeChars=',') + + Optional( White(" \t") ) ) ).streamline().setName("commaItem") + comma_separated_list = delimitedList( Optional( quotedString.copy() | _commasepitem, default="") ).setName("comma separated list") + """Predefined expression of 1 or more printable words or quoted strings, separated by commas.""" + + upcaseTokens = staticmethod(tokenMap(lambda t: _ustr(t).upper())) + """Parse action to convert tokens to upper case.""" + + downcaseTokens = staticmethod(tokenMap(lambda t: _ustr(t).lower())) + """Parse action to convert tokens to lower case.""" + + if __name__ == "__main__": selectToken = CaselessLiteral("select") diff --git a/src/unitTests.py b/src/unitTests.py index 6a46154..0e09be9 100644 --- a/src/unitTests.py +++ b/src/unitTests.py @@ -1470,6 +1470,16 @@ class UpcaseDowncaseUnicode(ParseTestCase): print(ret.rname) assert ret.rname=='MYKEY', "failed to upcase with named result" + kw = pp.Keyword('mykey', caseless=True).setParseAction(pp.pyparsing_common.upcaseTokens).setResultsName('rname') + ret = kw.parseString('mykey') + print(ret.rname) + assert ret.rname=='MYKEY', "failed to upcase with named result (pyparsing_common)" + + kw = pp.Keyword('MYKEY', caseless=True).setParseAction(pp.pyparsing_common.downcaseTokens).setResultsName('rname') + ret = kw.parseString('mykey') + print(ret.rname) + assert ret.rname=='mykey', "failed to upcase with named result" + if not IRON_PYTHON_ENV: #test html data html = " \ -- cgit v1.2.1