From 64bab20f061526e13ac7298c7d5fc165b385f1e8 Mon Sep 17 00:00:00 2001 From: ptmcg Date: Sat, 24 Oct 2015 21:27:14 +0000 Subject: Added withClass helper method, a simplified version of withAttribute for the common but annoying case when defining a filter on a div's class - made difficult because 'class' is a Python reserved word. git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/trunk@289 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b --- src/CHANGES | 6 +++++- src/pyparsing.py | 16 ++++++++++++++-- src/unitTests.py | 32 ++++++++++++++++++++------------ 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/CHANGES b/src/CHANGES index 7f54de6..e5f8fb2 100644 --- a/src/CHANGES +++ b/src/CHANGES @@ -14,7 +14,7 @@ Version 2.0.4 - such as "[а-яА-Я]" instead of "[\u0430-\u044f\u0410-\u042f]". Thanks to Alexandr Suchkov for the patch! -- Enhanced implementation of replaceWith +- Enhanced implementation of replaceWith. - Fixed enhanced ParseResults.dump() method when the results consists only of an unnamed array of sub-structure results. Reported by Robin @@ -34,6 +34,10 @@ Version 2.0.4 - tired of writing the same test code over and over, and finally added it as a test point method on ParserElement. +- Added withClass helper method, a simplified version of withAttribute for + the common but annoying case when defining a filter on a div's class - + made difficult because 'class' is a Python reserved word. + Version 2.0.3 - --------------------------- diff --git a/src/pyparsing.py b/src/pyparsing.py index 6a64487..bbcebe6 100644 --- a/src/pyparsing.py +++ b/src/pyparsing.py @@ -58,7 +58,7 @@ The pyparsing module handles some of the problems that are typically vexing when """ __version__ = "2.0.4" -__versionTime__ = "14 Oct 2015 03:33" +__versionTime__ = "24 Oct 2015 16:26" __author__ = "Paul McGuire " import string @@ -92,7 +92,7 @@ __all__ = [ 'punc8bit', 'pythonStyleComment', 'quotedString', 'removeQuotes', 'replaceHTMLEntity', 'replaceWith', 'restOfLine', 'sglQuotedString', 'srange', 'stringEnd', 'stringStart', 'traceParseAction', 'unicodeString', 'upcaseTokens', 'withAttribute', -'indentedBlock', 'originalTextFor', 'ungroup', 'infixNotation','locatedExpr', +'indentedBlock', 'originalTextFor', 'ungroup', 'infixNotation','locatedExpr', 'withClass', ] PY_3 = sys.version.startswith('3') @@ -3523,6 +3523,8 @@ def withAttribute(*args,**attrDict): - a list of name-value tuples, as in ( ("ns1:class", "Customer"), ("ns2:align","right") ) For attribute names with a namespace prefix, you must use the second form. Attribute names are matched insensitive to upper/lower case. + + If just testing for C{class} (with or without a namespace), use C{L{withClass}}. To verify that the attribute exists, but without specifying a value, pass C{withAttribute.ANY_VALUE} as the value. @@ -3542,6 +3544,16 @@ def withAttribute(*args,**attrDict): return pa withAttribute.ANY_VALUE = object() +def withClass(classname, namespace=''): + """Simplified version of C{L{withAttribute}} when matching on a div class - made + difficult because C{class} is a reserved word in Python. + """ + if namespace: + key = "%s:class" % namespace + else: + key = "class" + return withAttribute(**{key : classname}) + opAssoc = _Constants() opAssoc.LEFT = object() opAssoc.RIGHT = object() diff --git a/src/unitTests.py b/src/unitTests.py index bef6a73..76dc394 100644 --- a/src/unitTests.py +++ b/src/unitTests.py @@ -1851,7 +1851,7 @@ class WithAttributeParseActionTest(ParseTestCase): (Unit test written by voigts as part of the Google Highly Open Participation Contest) """ - from pyparsing import makeHTMLTags, Word, withAttribute, nums + from pyparsing import makeHTMLTags, Word, withAttribute, withClass, nums data = """ 1 @@ -1859,25 +1859,32 @@ class WithAttributeParseActionTest(ParseTestCase): 3 4 5 + 8 """ tagStart, tagEnd = makeHTMLTags("a") expr = tagStart + Word(nums).setResultsName("value") + tagEnd - expected = [['a', ['b', 'x'], False, '2', ''], ['a', ['b', 'x'], False, '3', '']] - - for attrib in [ + expected = ([['a', ['b', 'x'], False, '2', ''], + ['a', ['b', 'x'], False, '3', '']], + [['a', ['b', 'x'], False, '2', ''], + ['a', ['b', 'x'], False, '3', '']], + [['a', ['class', 'boo'], False, '8', '']], + ) + + for attrib, exp in zip([ withAttribute(b="x"), #withAttribute(B="x"), withAttribute(("b","x")), #withAttribute(("B","x")), - ]: + withClass("boo"), + ], expected): - tagStart.setParseAction(attrib) - result = expr.searchString(data) + tagStart.setParseAction(attrib) + result = expr.searchString(data) - print_(result.dump()) - assert result.asList() == expected, "Failed test, expected %s, got %s" % (expected, result.asList()) + print_(result.dump()) + assert result.asList() == exp, "Failed test, expected %s, got %s" % (expected, result.asList()) class NestedExpressionsTest(ParseTestCase): def runTest(self): @@ -2468,7 +2475,8 @@ def makeTestSuiteTemp(): #~ suite.addTest( OptionalEachTest() ) #~ suite.addTest( RepeaterTest() ) #~ suite.addTest( LocatedExprTest() ) - suite.addTest( AddConditionTest() ) + #~ suite.addTest( AddConditionTest() ) + suite.addTest( WithAttributeParseActionTest() ) return suite @@ -2484,8 +2492,8 @@ console = True if console: #~ # console mode testRunner = TextTestRunner() - testRunner.run( makeTestSuite() ) - #~ testRunner.run( makeTestSuiteTemp() ) + #~ testRunner.run( makeTestSuite() ) + testRunner.run( makeTestSuiteTemp() ) #~ lp.run("testRunner.run( makeTestSuite() )") else: # HTML mode -- cgit v1.2.1