This page is for Pyparsing users to share handy helper/expression construction methods, or nifty parse actions.

(Be sure to enclose code in [[code]] tags, each tag on its own line before and after the source code!)


withoutAttribute


Looks for HTML tags that do not have the specified attribute.
def withoutAttribute(attrname):
    exception_msg = "tag includes attribute '%s'" % attrname
    def pa(tokens):
        if tokens.attrname:
            raise ParseException(exception_msg)
    return pa

alternate more general version

Looks for HTML tags that do not have the specified attribute
equal to the specified value (or withoutAttribute.ANY_VALUE)
def withoutAttribute(*args,**attrDict):
    """Helper to create a validating parse action to be used with start tags created
       with makeXMLTags or makeHTMLTags. Use withoutAttribute to qualify a starting tag
       with an unwanted value, to avoid false matches on common tags such as
       <TD> or <DIV>.
 
       Call withoutAttribute with a series of attribute names and values. Specify the list
       of filter attributes names and values as:
        - keyword arguments, as in (class="Customer",align="right"), or
        - 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.
 
       To verify that the attribute exists, but without specifying a value, pass
       withoutAttribute.ANY_VALUE as the value.
       """
    if args:
        attrs = args[:]
    else:
        attrs = attrDict.items()
    attrs = [(k,v) for k,v in attrs]
    def pa(s,l,tokens):
        for attrName,attrValue in attrs:
            if attrName in tokens:
                if attrValue == withoutAttribute.ANY_VALUE or tokens[attrName] == attrValue:
                    raise ParseException(s,l,"attribute '%s' has an unwanted value '%s'" %
                                                (attrName, attrValue))
    return pa
 
withoutAttribute.ANY_VALUE = object()