pyparsing library internals
This page is all about the pyparsing python library, the contents below is auto-generated by inspecting a python package’s code base.
Package Files
- pyparsing/__init__.py
- pyparsing/actions.py
- pyparsing/common.py
- pyparsing/core.py
- pyparsing/diagram
- pyparsing/diagram/__init__.py
- pyparsing/diagram/template.jinja2
- pyparsing/exceptions.py
- pyparsing/helpers.py
- pyparsing/results.py
- pyparsing/testing.py
- pyparsing/unicode.py
- pyparsing/util.py
Package Classes
- <class ‘abc.ABC’>
- <class ‘pyparsing.core.And’>
- <class ‘pyparsing.core.AtLineStart’>
- <class ‘pyparsing.core.AtStringStart’>
- <class ‘pyparsing.core.CaselessKeyword’>
- <class ‘pyparsing.core.CaselessLiteral’>
- <class ‘pyparsing.core.Char’>
- <class ‘pyparsing.core.CharsNotIn’>
- <class ‘pyparsing.core.CloseMatch’>
- <class ‘pyparsing.core.Combine’>
- <enum ‘Diagnostics’>
- <class ‘pyparsing.core.Dict’>
- <class ‘pyparsing.core.Each’>
- <class ‘pyparsing.core.Empty’>
- <enum ‘Enum’>
- <class ‘pyparsing.exceptions.ExceptionWordUnicode’>
- <class ‘pyparsing.core.FollowedBy’>
- <class ‘pyparsing.core.Forward’>
- <class ‘pyparsing.core.GoToColumn’>
- <class ‘pyparsing.core.Group’>
- <class ‘pyparsing.core.IndentedBlock’>
- <class ‘collections.abc.Iterable’>
- <class ‘collections.abc.Iterator’>
- <class ‘pyparsing.core.Keyword’>
- <class ‘pyparsing.util.LRUMemo’>
- <class ‘pyparsing.core.LineEnd’>
- <class ‘pyparsing.core.LineStart’>
- <class ‘pyparsing.core.Literal’>
- <class ‘pyparsing.core.Located’>
- <class ‘collections.abc.Mapping’>
- <class ‘pyparsing.core.MatchFirst’>
- <class ‘collections.abc.MutableMapping’>
- <class ‘collections.abc.MutableSequence’>
- <class ‘pyparsing.core.NoMatch’>
- <class ‘pyparsing.core.NotAny’>
- <class ‘pyparsing.core.OneOrMore’>
- <class ‘pyparsing.actions.OnlyOnce’>
- <enum ‘OpAssoc’>
- <class ‘pyparsing.core.Opt’>
- <class ‘pyparsing.core.Opt’>
- <class ‘pyparsing.core.Or’>
- <class ‘pyparsing.exceptions.ParseBaseException’>
- <class ‘pyparsing.core.ParseElementEnhance’>
- <class ‘pyparsing.exceptions.ParseException’>
- <class ‘pyparsing.core.ParseExpression’>
- <class ‘pyparsing.exceptions.ParseFatalException’>
- <class ‘pyparsing.results.ParseResults’>
- <class ‘pyparsing.exceptions.ParseSyntaxException’>
- <class ‘pyparsing.core.ParserElement’>
- <class ‘pathlib.Path’>
- <class ‘pyparsing.core.PositionToken’>
- <class ‘pyparsing.core.PrecededBy’>
- <class ‘pyparsing.core.QuotedString’>
- <class ‘pyparsing.exceptions.RecursiveGrammarException’>
- <class ‘pyparsing.core.Regex’>
- <class ‘pyparsing.core.SkipTo’>
- <class ‘pyparsing.core.StringEnd’>
- <class ‘pyparsing.core.StringStart’>
- <class ‘pyparsing.core.Suppress’>
- <class ’typing.TextIO'>
- <class ‘pyparsing.core.Token’>
- <class ‘pyparsing.core.TokenConverter’>
- <class ‘pyparsing.util.UnboundedMemo’>
- <class ‘pyparsing.core.White’>
- <class ‘pyparsing.core.Word’>
- <class ‘pyparsing.core.WordEnd’>
- <class ‘pyparsing.core.WordStart’>
- <class ‘pyparsing.core.ZeroOrMore’>
- <class ‘pyparsing.core.compat'>
- <class ‘pyparsing.core.diag'>
- <class ‘pyparsing.common.pyparsing_common’>
- <class ‘operator.itemgetter’>
- <enum ‘OpAssoc’>
- <class ‘pyparsing.unicode.pyparsing_unicode’>
- <class ‘pyparsing.common.pyparsing_common’>
- <class ‘pyparsing.testing.pyparsing_test’>
- <class ‘pyparsing.unicode.pyparsing_unicode’>
- <class ‘pyparsing.testing.pyparsing_test’>
- <class ‘pyparsing.unicode.pyparsing_unicode’>
- <class ‘pyparsing.unicode.unicode_set’>
- <class ‘pyparsing.version_info’>
- <class ‘weakref’>
Package Methods
pyparsing.NamedTuple()
Usage in Python versions >= 3.6::
class Employee(NamedTuple):
name: str
id: int
This is equivalent to::
Employee = collections.namedtuple('Employee', ['name', 'id'])
The resulting class has an extra annotations attribute, giving a dict that maps field names to types. (The field names are also in the _fields attribute, which is part of the namedtuple API.) Alternative equivalent keyword syntax is also accepted::
Employee = NamedTuple('Employee', name=str, id=int)
In Python versions <= 3.5 use::
Employee = NamedTuple('Employee', [('name', str), ('id', int)])
pyparsing.NamedTuple(typename, fields=None, /, **kwargs)
pyparsing.NamedTuple(
typename,
fields,
kwargs,
)
pyparsing.RLock()
A reentrant lock must be released by the thread that acquired it. Once a thread has acquired a reentrant lock, the same thread may acquire it again without blocking; the thread must release it once for each time it has acquired it.
pyparsing.RLock(*args, **kwargs)
pyparsing.RLock(
args,
kwargs,
)
pyparsing.abstractmethod()
Requires that the metaclass is ABCMeta or derived from it. A class that has a metaclass derived from ABCMeta cannot be instantiated unless all of its abstract methods are overridden. The abstract methods can be called using any of the normal ‘super’ call mechanisms. abstractmethod() may be used to declare abstract methods for properties and descriptors.
Usage:
class C(metaclass=ABCMeta):
@abstractmethod
def my_abstract_method(self, ...):
...
pyparsing.abstractmethod(funcobj)
pyparsing.abstractmethod(
funcobj,
)
pyparsing.autoname_elements()
generating railroad diagram with named subdiagrams.
pyparsing.autoname_elements()
pyparsing.autoname_elements(
)
pyparsing.conditionAsParseAction()
into a parse action. Can be used in places when a parse action is required
and :class:ParserElement.add_condition
cannot be used (such as when adding a condition
to an operator level in :class:infix_notation
).
Optional keyword arguments:
message
- define a custom message to be used in the raised exceptionfatal
- if True, will raise :class:ParseFatalException
to stop parsing immediately; otherwise will raise :class:ParseException
pyparsing.conditionAsParseAction(fn: Union[Callable[[], bool], Callable[[pyparsing.results.ParseResults], bool], Callable[[int, pyparsing.results.ParseResults], bool], Callable[[str, int, pyparsing.results.ParseResults], bool]], message: str = None, fatal: bool = False)
pyparsing.conditionAsParseAction(
fn,
message,
fatal,
)
pyparsing.condition_as_parse_action()
into a parse action. Can be used in places when a parse action is required
and :class:ParserElement.add_condition
cannot be used (such as when adding a condition
to an operator level in :class:infix_notation
).
Optional keyword arguments:
message
- define a custom message to be used in the raised exceptionfatal
- if True, will raise :class:ParseFatalException
to stop parsing immediately; otherwise will raise :class:ParseException
pyparsing.condition_as_parse_action(fn: Union[Callable[[], bool], Callable[[pyparsing.results.ParseResults], bool], Callable[[int, pyparsing.results.ParseResults], bool], Callable[[str, int, pyparsing.results.ParseResults], bool]], message: str = None, fatal: bool = False)
pyparsing.condition_as_parse_action(
fn,
message,
fatal,
)
pyparsing.countedArray()
This helper defines a pattern of the form::
integer expr expr expr...
where the leading integer tells how many expr expressions follow. The matched tokens returns the array of expr tokens as a list - the leading count token is suppressed.
If int_expr
is specified, it should be a pyparsing expression
that produces an integer value.
Example::
counted_array(Word(alphas)).parse_string('2 ab cd ef') # -\> ['ab', 'cd']
# in this parser, the leading integer value is given in binary,
# '10' indicating that 2 values are in the array
binary_constant = Word('01').set_parse_action(lambda t: int(t[0], 2))
counted_array(Word(alphas), int_expr=binary_constant).parse_string('10 ab cd ef') # -\> ['ab', 'cd']
# if other fields must be parsed after the count but before the
# list items, give the fields results names and they will
# be preserved in the returned ParseResults:
count_with_metadata = integer + Word(alphas)("type")
typed_array = counted_array(Word(alphanums), int_expr=count_with_metadata)("items")
result = typed_array.parse_string("3 bool True True False")
print(result.dump())
# prints
# ['True', 'True', 'False']
# - items: ['True', 'True', 'False']
# - type: 'bool'
pyparsing.countedArray(expr: pyparsing.core.ParserElement, int_expr: Optional[pyparsing.core.ParserElement] = None, *, intExpr: Optional[pyparsing.core.ParserElement] = None) -> pyparsing.core.ParserElement
pyparsing.countedArray(
expr,
int_expr,
intExpr,
)
pyparsing.counted_array()
This helper defines a pattern of the form::
integer expr expr expr...
where the leading integer tells how many expr expressions follow. The matched tokens returns the array of expr tokens as a list - the leading count token is suppressed.
If int_expr
is specified, it should be a pyparsing expression
that produces an integer value.
Example::
counted_array(Word(alphas)).parse_string('2 ab cd ef') # -\> ['ab', 'cd']
# in this parser, the leading integer value is given in binary,
# '10' indicating that 2 values are in the array
binary_constant = Word('01').set_parse_action(lambda t: int(t[0], 2))
counted_array(Word(alphas), int_expr=binary_constant).parse_string('10 ab cd ef') # -\> ['ab', 'cd']
# if other fields must be parsed after the count but before the
# list items, give the fields results names and they will
# be preserved in the returned ParseResults:
count_with_metadata = integer + Word(alphas)("type")
typed_array = counted_array(Word(alphanums), int_expr=count_with_metadata)("items")
result = typed_array.parse_string("3 bool True True False")
print(result.dump())
# prints
# ['True', 'True', 'False']
# - items: ['True', 'True', 'False']
# - type: 'bool'
pyparsing.counted_array(expr: pyparsing.core.ParserElement, int_expr: Optional[pyparsing.core.ParserElement] = None, *, intExpr: Optional[pyparsing.core.ParserElement] = None) -> pyparsing.core.ParserElement
pyparsing.counted_array(
expr,
int_expr,
intExpr,
)
pyparsing.delimitedList()
defaults to ‘,’. By default, the list elements and delimiters can
have intervening whitespace, and comments, but this can be
overridden by passing combine=True
in the constructor. If
combine
is set to True
, the matching tokens are
returned as a single token string, with the delimiters included;
otherwise, the matching tokens are returned as a list of tokens,
with the delimiters suppressed.
If allow_trailing_delim
is set to True, then the list may end with
a delimiter.
Example::
delimited_list(Word(alphas)).parse_string("aa,bb,cc") # -\> ['aa', 'bb', 'cc']
delimited_list(Word(hexnums), delim=':', combine=True).parse_string("AA:BB:CC:DD:EE") # -\> ['AA:BB:CC:DD:EE']
pyparsing.delimitedList(expr: Union[str, pyparsing.core.ParserElement], delim: Union[str, pyparsing.core.ParserElement] = ',', combine: bool = False, *, allow_trailing_delim: bool = False) -> pyparsing.core.ParserElement
pyparsing.delimitedList(
expr,
delim,
combine,
allow_trailing_delim,
)
pyparsing.delimited_list()
defaults to ‘,’. By default, the list elements and delimiters can
have intervening whitespace, and comments, but this can be
overridden by passing combine=True
in the constructor. If
combine
is set to True
, the matching tokens are
returned as a single token string, with the delimiters included;
otherwise, the matching tokens are returned as a list of tokens,
with the delimiters suppressed.
If allow_trailing_delim
is set to True, then the list may end with
a delimiter.
Example::
delimited_list(Word(alphas)).parse_string("aa,bb,cc") # -\> ['aa', 'bb', 'cc']
delimited_list(Word(hexnums), delim=':', combine=True).parse_string("AA:BB:CC:DD:EE") # -\> ['AA:BB:CC:DD:EE']
pyparsing.delimited_list(expr: Union[str, pyparsing.core.ParserElement], delim: Union[str, pyparsing.core.ParserElement] = ',', combine: bool = False, *, allow_trailing_delim: bool = False) -> pyparsing.core.ParserElement
pyparsing.delimited_list(
expr,
delim,
combine,
allow_trailing_delim,
)
pyparsing.dictOf()
the respective patterns for the key and value. Takes care of
defining the :class:Dict
, :class:ZeroOrMore
, and
pyparsing.dictOf(key: pyparsing.core.ParserElement, value: pyparsing.core.ParserElement) -> pyparsing.core.ParserElement
pyparsing.dictOf(
key,
value,
)
pyparsing.dict_of()
the respective patterns for the key and value. Takes care of
defining the :class:Dict
, :class:ZeroOrMore
, and
pyparsing.dict_of(key: pyparsing.core.ParserElement, value: pyparsing.core.ParserElement) -> pyparsing.core.ParserElement
pyparsing.dict_of(
key,
value,
)
pyparsing.disable_diag()
pyparsing.disable_diag(diag_enum)
pyparsing.disable_diag(
diag_enum,
)
pyparsing.enable_all_warnings()
pyparsing.enable_all_warnings()
pyparsing.enable_all_warnings(
)
pyparsing.enable_diag()
pyparsing.enable_diag(diag_enum)
pyparsing.enable_diag(
diag_enum,
)
pyparsing.indentedBlock()
Helper method for defining space-delimited indentation blocks, such as those used to define block statements in Python source code.
Parameters:
blockStatementExpr
- expression defining syntax of statement that is repeated within the indented blockindentStack
- list created by caller to manage indentation stack (multiplestatementWithIndentedBlock
expressions within a single grammar should share a commonindentStack
)indent
- boolean indicating whether block must be indented beyond the current level; set toFalse
for block of left-most statements (default=True
)
A valid block must contain at least one blockStatement
.
(Note that indentedBlock uses internal parse actions which make it incompatible with packrat parsing.)
Example::
data = '''
def A(z):
A1
B = 100
G = A2
A2
A3
B
def BB(a,b,c):
BB1
def BBA():
bba1
bba2
bba3
C
D
def spam(x,y):
def eggs(z):
pass
'''
indentStack = [1]
stmt = Forward()
identifier = Word(alphas, alphanums)
funcDecl = ("def" + identifier + Group("(" + Opt(delimitedList(identifier)) + ")") + ":")
func_body = indentedBlock(stmt, indentStack)
funcDef = Group(funcDecl + func_body)
rvalue = Forward()
funcCall = Group(identifier + "(" + Opt(delimitedList(rvalue)) + ")")
rvalue \<\< (funcCall | identifier | Word(nums))
assignment = Group(identifier + "=" + rvalue)
stmt \<\< (funcDef | assignment | identifier)
module_body = OneOrMore(stmt)
parseTree = module_body.parseString(data)
parseTree.pprint()
prints::
[['def',
'A',
['(', 'z', ')'],
':',
[['A1'], [['B', '=', '100']], [['G', '=', 'A2']], ['A2'], ['A3']]],
'B',
['def',
'BB',
['(', 'a', 'b', 'c', ')'],
':',
[['BB1'], [['def', 'BBA', ['(', ')'], ':', [['bba1'], ['bba2'], ['bba3']]]]]],
'C',
'D',
['def',
'spam',
['(', 'x', 'y', ')'],
':',
[[['def', 'eggs', ['(', 'z', ')'], ':', [['pass']]]]]]]
pyparsing.indentedBlock(blockStatementExpr, indentStack, indent=True, backup_stacks=[])
pyparsing.indentedBlock(
blockStatementExpr,
indentStack,
indent,
backup_stacks,
)
pyparsing.infixNotation()
operators working in a precedence hierarchy. Operators may be unary or binary, left- or right-associative. Parse actions can also be attached to operator expressions. The generated parser will also recognize the use of parentheses to override operator precedences (see example below).
Note: if you define a deep operator list, you may see performance issues when using infix_notation. See
pyparsing.infixNotation(base_expr: pyparsing.core.ParserElement, op_list: List[Union[Tuple[Union[pyparsing.core.ParserElement, str, Tuple[Union[pyparsing.core.ParserElement, str], Union[pyparsing.core.ParserElement, str]]], int, pyparsing.helpers.OpAssoc, Union[Callable[[], Any], Callable[[pyparsing.results.ParseResults], Any], Callable[[int, pyparsing.results.ParseResults], Any], Callable[[str, int, pyparsing.results.ParseResults], Any], NoneType]], Tuple[Union[pyparsing.core.ParserElement, str, Tuple[Union[pyparsing.core.ParserElement, str], Union[pyparsing.core.ParserElement, str]]], int, pyparsing.helpers.OpAssoc]]], lpar: Union[str, pyparsing.core.ParserElement] = Suppress:('('), rpar: Union[str, pyparsing.core.ParserElement] = Suppress:(')')) -> pyparsing.core.ParserElement
pyparsing.infixNotation(
base_expr,
op_list,
lpar,
rpar,
)
pyparsing.infix_notation()
operators working in a precedence hierarchy. Operators may be unary or binary, left- or right-associative. Parse actions can also be attached to operator expressions. The generated parser will also recognize the use of parentheses to override operator precedences (see example below).
Note: if you define a deep operator list, you may see performance issues when using infix_notation. See
pyparsing.infix_notation(base_expr: pyparsing.core.ParserElement, op_list: List[Union[Tuple[Union[pyparsing.core.ParserElement, str, Tuple[Union[pyparsing.core.ParserElement, str], Union[pyparsing.core.ParserElement, str]]], int, pyparsing.helpers.OpAssoc, Union[Callable[[], Any], Callable[[pyparsing.results.ParseResults], Any], Callable[[int, pyparsing.results.ParseResults], Any], Callable[[str, int, pyparsing.results.ParseResults], Any], NoneType]], Tuple[Union[pyparsing.core.ParserElement, str, Tuple[Union[pyparsing.core.ParserElement, str], Union[pyparsing.core.ParserElement, str]]], int, pyparsing.helpers.OpAssoc]]], lpar: Union[str, pyparsing.core.ParserElement] = Suppress:('('), rpar: Union[str, pyparsing.core.ParserElement] = Suppress:(')')) -> pyparsing.core.ParserElement
pyparsing.infix_notation(
base_expr,
op_list,
lpar,
rpar,
)
pyparsing.locatedExpr()
Helper to decorate a returned token with its starting and ending locations in the input string.
This helper adds the following results names:
locn_start
- location where matched expression beginslocn_end
- location where matched expression endsvalue
- the actual parsed results
Be careful if the input text contains \<TAB\>
characters, you
may want to call :class:ParserElement.parseWithTabs
Example::
wd = Word(alphas)
for match in locatedExpr(wd).searchString("ljsdf123lksdjjf123lkkjj1222"):
print(match)
prints::
[[0, 'ljsdf', 5]]
[[8, 'lksdjjf', 15]]
[[18, 'lkkjj', 23]]
pyparsing.locatedExpr(expr: pyparsing.core.ParserElement) -> pyparsing.core.ParserElement
pyparsing.locatedExpr(
expr,
)
pyparsing.lru_cache()
If maxsize is set to None, the LRU features are disabled and the cache can grow without bound.
If typed is True, arguments of different types will be cached separately. For example, f(3.0) and f(3) will be treated as distinct calls with distinct results.
Arguments to the cached function must be hashable.
View the cache statistics named tuple (hits, misses, maxsize, currsize) with f.cache_info(). Clear the cache and statistics with f.cache_clear(). Access the underlying function with f.wrapped.
See: http://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU)
pyparsing.lru_cache(maxsize=128, typed=False)
pyparsing.lru_cache(
maxsize,
typed,
)
pyparsing.makeHTMLTags()
given a tag name. Matches tags in either upper or lower case, attributes with namespaces and with quoted or unquoted values.
Example::
text = '\<td\>More info at the \<a href="https://github.com/pyparsing/pyparsing/wiki"\>pyparsing\</a\> wiki page\</td\>'
# make_html_tags returns pyparsing expressions for the opening and
# closing tags as a 2-tuple
a, a_end = make_html_tags("A")
link_expr = a + SkipTo(a_end)("link_text") + a_end
for link in link_expr.search_string(text):
# attributes in the \<A\> tag (like "href" shown here) are
# also accessible as named results
print(link.link_text, '-\>', link.href)
prints::
pyparsing -\> https://github.com/pyparsing/pyparsing/wiki
pyparsing.makeHTMLTags(tag_str: Union[str, pyparsing.core.ParserElement]) -> Tuple[pyparsing.core.ParserElement, pyparsing.core.ParserElement]
pyparsing.makeHTMLTags(
tag_str,
)
pyparsing.makeXMLTags()
given a tag name. Matches tags only in the given upper/lower case.
Example: similar to :class:make_html_tags
pyparsing.makeXMLTags(tag_str: Union[str, pyparsing.core.ParserElement]) -> Tuple[pyparsing.core.ParserElement, pyparsing.core.ParserElement]
pyparsing.makeXMLTags(
tag_str,
)
pyparsing.make_html_tags()
given a tag name. Matches tags in either upper or lower case, attributes with namespaces and with quoted or unquoted values.
Example::
text = '\<td\>More info at the \<a href="https://github.com/pyparsing/pyparsing/wiki"\>pyparsing\</a\> wiki page\</td\>'
# make_html_tags returns pyparsing expressions for the opening and
# closing tags as a 2-tuple
a, a_end = make_html_tags("A")
link_expr = a + SkipTo(a_end)("link_text") + a_end
for link in link_expr.search_string(text):
# attributes in the \<A\> tag (like "href" shown here) are
# also accessible as named results
print(link.link_text, '-\>', link.href)
prints::
pyparsing -\> https://github.com/pyparsing/pyparsing/wiki
pyparsing.make_html_tags(tag_str: Union[str, pyparsing.core.ParserElement]) -> Tuple[pyparsing.core.ParserElement, pyparsing.core.ParserElement]
pyparsing.make_html_tags(
tag_str,
)
pyparsing.make_xml_tags()
given a tag name. Matches tags only in the given upper/lower case.
Example: similar to :class:make_html_tags
pyparsing.make_xml_tags(tag_str: Union[str, pyparsing.core.ParserElement]) -> Tuple[pyparsing.core.ParserElement, pyparsing.core.ParserElement]
pyparsing.make_xml_tags(
tag_str,
)
pyparsing.matchOnlyAtCol()
a specific column in the input text.
pyparsing.matchOnlyAtCol(n)
pyparsing.matchOnlyAtCol(
n,
)
pyparsing.matchPreviousExpr()
the tokens matched in a previous expression, that is, it looks for a ‘repeat’ of a previous expression. For example::
first = Word(nums)
second = match_previous_expr(first)
match_expr = first + ":" + second
will match "1:1"
, but not "1:2"
. Because this
matches by expressions, will not match the leading "1:1"
in "1:10"
; the expressions are evaluated first, and then
compared, so "1"
is compared with "10"
. Do not use
with packrat parsing enabled.
pyparsing.matchPreviousExpr(expr: pyparsing.core.ParserElement) -> pyparsing.core.ParserElement
pyparsing.matchPreviousExpr(
expr,
)
pyparsing.matchPreviousLiteral()
the tokens matched in a previous expression, that is, it looks for a ‘repeat’ of a previous expression. For example::
first = Word(nums)
second = match_previous_literal(first)
match_expr = first + ":" + second
will match "1:1"
, but not "1:2"
. Because this
matches a previous literal, will also match the leading
"1:1"
in "1:10"
. If this is not desired, use
pyparsing.matchPreviousLiteral(expr: pyparsing.core.ParserElement) -> pyparsing.core.ParserElement
pyparsing.matchPreviousLiteral(
expr,
)
pyparsing.match_only_at_col()
a specific column in the input text.
pyparsing.match_only_at_col(n)
pyparsing.match_only_at_col(
n,
)
pyparsing.match_previous_expr()
the tokens matched in a previous expression, that is, it looks for a ‘repeat’ of a previous expression. For example::
first = Word(nums)
second = match_previous_expr(first)
match_expr = first + ":" + second
will match "1:1"
, but not "1:2"
. Because this
matches by expressions, will not match the leading "1:1"
in "1:10"
; the expressions are evaluated first, and then
compared, so "1"
is compared with "10"
. Do not use
with packrat parsing enabled.
pyparsing.match_previous_expr(expr: pyparsing.core.ParserElement) -> pyparsing.core.ParserElement
pyparsing.match_previous_expr(
expr,
)
pyparsing.match_previous_literal()
the tokens matched in a previous expression, that is, it looks for a ‘repeat’ of a previous expression. For example::
first = Word(nums)
second = match_previous_literal(first)
match_expr = first + ":" + second
will match "1:1"
, but not "1:2"
. Because this
matches a previous literal, will also match the leading
"1:1"
in "1:10"
. If this is not desired, use
pyparsing.match_previous_literal(expr: pyparsing.core.ParserElement) -> pyparsing.core.ParserElement
pyparsing.match_previous_literal(
expr,
)
pyparsing.nestedExpr()
closing delimiters ("("
and ")"
are the default).
Parameters:
opener
- opening character for a nested list (default="("
); can also be a pyparsing expressioncloser
- closing character for a nested list (default=")"
); can also be a pyparsing expressioncontent
- expression for items within the nested lists (default=None
)ignore_expr
- expression for ignoring opening and closing delimiters (default= :class:quoted_string
)ignoreExpr
- this pre-PEP8 argument is retained for compatibility but will be removed in a future release
If an expression is not provided for the content argument, the nested expression will capture all whitespace-delimited content between delimiters as a list of separate values.
Use the ignore_expr
argument to define expressions that may
contain opening or closing characters that should not be treated as
opening or closing characters for nesting, such as quoted_string or
a comment expression. Specify multiple expressions using an
pyparsing.nestedExpr(opener: Union[str, pyparsing.core.ParserElement] = '(', closer: Union[str, pyparsing.core.ParserElement] = ')', content: Optional[pyparsing.core.ParserElement] = None, ignore_expr: pyparsing.core.ParserElement = quotedString using single or double quotes, *, ignoreExpr: pyparsing.core.ParserElement = quotedString using single or double quotes) -> pyparsing.core.ParserElement
pyparsing.nestedExpr(
opener,
closer,
content,
ignore_expr,
ignoreExpr,
)
pyparsing.nested_expr()
closing delimiters ("("
and ")"
are the default).
Parameters:
opener
- opening character for a nested list (default="("
); can also be a pyparsing expressioncloser
- closing character for a nested list (default=")"
); can also be a pyparsing expressioncontent
- expression for items within the nested lists (default=None
)ignore_expr
- expression for ignoring opening and closing delimiters (default= :class:quoted_string
)ignoreExpr
- this pre-PEP8 argument is retained for compatibility but will be removed in a future release
If an expression is not provided for the content argument, the nested expression will capture all whitespace-delimited content between delimiters as a list of separate values.
Use the ignore_expr
argument to define expressions that may
contain opening or closing characters that should not be treated as
opening or closing characters for nesting, such as quoted_string or
a comment expression. Specify multiple expressions using an
pyparsing.nested_expr(opener: Union[str, pyparsing.core.ParserElement] = '(', closer: Union[str, pyparsing.core.ParserElement] = ')', content: Optional[pyparsing.core.ParserElement] = None, ignore_expr: pyparsing.core.ParserElement = quotedString using single or double quotes, *, ignoreExpr: pyparsing.core.ParserElement = quotedString using single or double quotes) -> pyparsing.core.ParserElement
pyparsing.nested_expr(
opener,
closer,
content,
ignore_expr,
ignoreExpr,
)
pyparsing.nullDebugAction()
pyparsing.nullDebugAction(*args)
pyparsing.nullDebugAction(
args,
)
pyparsing.null_debug_action()
pyparsing.null_debug_action(*args)
pyparsing.null_debug_action(
args,
)
pyparsing.oneOf()
and makes sure to do longest-first testing when there is a conflict,
regardless of the input order, but returns
a :class:MatchFirst
for best performance.
Parameters:
strs
- a string of space-delimited literals, or a collection of string literalscaseless
- treat all literals as caseless - (default=False
)use_regex
- as an optimization, will generate a :class:Regex
object; otherwise, will generate a :class:MatchFirst
object (ifcaseless=True
orasKeyword=True
, or if creating a :class:Regex
raises an exception) - (default=True
)as_keyword
- enforce :class:Keyword
-style matching on the generated expressions - (default=False
)asKeyword
anduseRegex
are retained for pre-PEP8 compatibility, but will be removed in a future release
Example::
comp_oper = one_of("\< = \> \<= \>= !=")
var = Word(alphas)
number = Word(nums)
term = var | number
comparison_expr = term + comp_oper + term
print(comparison_expr.search_string("B = 12 AA=23 B\<=AA AA\>12"))
prints::
[['B', '=', '12'], ['AA', '=', '23'], ['B', '\<=', 'AA'], ['AA', '\>', '12']]
pyparsing.oneOf(strs: Union[Iterable[str], str], caseless: bool = False, use_regex: bool = True, as_keyword: bool = False, *, useRegex: bool = True, asKeyword: bool = False) -> pyparsing.core.ParserElement
pyparsing.oneOf(
strs,
caseless,
use_regex,
as_keyword,
useRegex,
asKeyword,
)
pyparsing.one_of()
and makes sure to do longest-first testing when there is a conflict,
regardless of the input order, but returns
a :class:MatchFirst
for best performance.
Parameters:
strs
- a string of space-delimited literals, or a collection of string literalscaseless
- treat all literals as caseless - (default=False
)use_regex
- as an optimization, will generate a :class:Regex
object; otherwise, will generate a :class:MatchFirst
object (ifcaseless=True
orasKeyword=True
, or if creating a :class:Regex
raises an exception) - (default=True
)as_keyword
- enforce :class:Keyword
-style matching on the generated expressions - (default=False
)asKeyword
anduseRegex
are retained for pre-PEP8 compatibility, but will be removed in a future release
Example::
comp_oper = one_of("\< = \> \<= \>= !=")
var = Word(alphas)
number = Word(nums)
term = var | number
comparison_expr = term + comp_oper + term
print(comparison_expr.search_string("B = 12 AA=23 B\<=AA AA\>12"))
prints::
[['B', '=', '12'], ['AA', '=', '23'], ['B', '\<=', 'AA'], ['AA', '\>', '12']]
pyparsing.one_of(strs: Union[Iterable[str], str], caseless: bool = False, use_regex: bool = True, as_keyword: bool = False, *, useRegex: bool = True, asKeyword: bool = False) -> pyparsing.core.ParserElement
pyparsing.one_of(
strs,
caseless,
use_regex,
as_keyword,
useRegex,
asKeyword,
)
pyparsing.originalTextFor()
expression. Useful to restore the parsed fields of an HTML start tag into the raw tag text itself, or to revert separate tokens with intervening whitespace back to the original matching input text. By default, returns astring containing the original parsed text.
If the optional as_string
argument is passed as
False
, then the return value is
a :class:ParseResults
containing any results names that
were originally matched, and a single token containing the original
matched text from the input string. So if the expression passed to
pyparsing.originalTextFor(expr: pyparsing.core.ParserElement, as_string: bool = True, *, asString: bool = True) -> pyparsing.core.ParserElement
pyparsing.originalTextFor(
expr,
as_string,
asString,
)
pyparsing.original_text_for()
expression. Useful to restore the parsed fields of an HTML start tag into the raw tag text itself, or to revert separate tokens with intervening whitespace back to the original matching input text. By default, returns astring containing the original parsed text.
If the optional as_string
argument is passed as
False
, then the return value is
a :class:ParseResults
containing any results names that
were originally matched, and a single token containing the original
matched text from the input string. So if the expression passed to
pyparsing.original_text_for(expr: pyparsing.core.ParserElement, as_string: bool = True, *, asString: bool = True) -> pyparsing.core.ParserElement
pyparsing.original_text_for(
expr,
as_string,
asString,
)
pyparsing.removeQuotes()
quoted strings.
Example::
# by default, quotation marks are included in parsed results
quoted_string.parse_string("'Now is the Winter of our Discontent'") # -\> ["'Now is the Winter of our Discontent'"]
# use remove_quotes to strip quotation marks from parsed results
quoted_string.set_parse_action(remove_quotes)
quoted_string.parse_string("'Now is the Winter of our Discontent'") # -\> ["Now is the Winter of our Discontent"]
pyparsing.removeQuotes(s, l, t)
pyparsing.removeQuotes(
s,
l,
t,
)
pyparsing.remove_quotes()
quoted strings.
Example::
# by default, quotation marks are included in parsed results
quoted_string.parse_string("'Now is the Winter of our Discontent'") # -\> ["'Now is the Winter of our Discontent'"]
# use remove_quotes to strip quotation marks from parsed results
quoted_string.set_parse_action(remove_quotes)
quoted_string.parse_string("'Now is the Winter of our Discontent'") # -\> ["Now is the Winter of our Discontent"]
pyparsing.remove_quotes(s, l, t)
pyparsing.remove_quotes(
s,
l,
t,
)
pyparsing.replaceHTMLEntity()
pyparsing.replaceHTMLEntity(t)
pyparsing.replaceHTMLEntity(
t,
)
pyparsing.replaceWith()
a literal value. Especially useful when used with
pyparsing.replaceWith(repl_str)
pyparsing.replaceWith(
repl_str,
)
pyparsing.replace_html_entity()
pyparsing.replace_html_entity(t)
pyparsing.replace_html_entity(
t,
)
pyparsing.replace_with()
a literal value. Especially useful when used with
pyparsing.replace_with(repl_str)
pyparsing.replace_with(
repl_str,
)
pyparsing.srange()
construction. Borrows syntax from regexp '[]'
string range
definitions::
srange("[0-9]") -\> "0123456789"
srange("[a-z]") -\> "abcdefghijklmnopqrstuvwxyz"
srange("[a-z$_]") -\> "abcdefghijklmnopqrstuvwxyz$_"
The input string must be enclosed in []’s, and the returned string is the expanded character set joined into a single string. The values enclosed in the []’s may be:
- a single character
- an escaped character with a leading backslash (such as
\-
or\]
) - an escaped hex character with a leading
'\x'
(\x21
, which is a'!'
character) (\0x##
is also supported for backwards compatibility) - an escaped octal character with a leading
'\0'
(\041
, which is a'!'
character) - a range of any of the above, separated by a dash (
'a-z'
, etc.) - any combination of the above (
'aeiouy'
,'a-zA-Z0-9_$'
, etc.)
pyparsing.srange(s)
pyparsing.srange(
s,
)
pyparsing.tokenMap()
elements of a :class:ParseResults
list. If any additional args are passed,
they are forwarded to the given function as additional arguments
after the token, as in
hex_integer = Word(hexnums).set_parse_action(token_map(int, 16))
,
which will convert the parsed data to an integer using base 16.
Example (compare the last to example in :class:ParserElement.transform_string
::
hex_ints = OneOrMore(Word(hexnums)).set_parse_action(token_map(int, 16))
hex_ints.run_tests('''
00 11 22 aa FF 0a 0d 1a
''')
upperword = Word(alphas).set_parse_action(token_map(str.upper))
OneOrMore(upperword).run_tests('''
my kingdom for a horse
''')
wd = Word(alphas).set_parse_action(token_map(str.title))
OneOrMore(wd).set_parse_action(' '.join).run_tests('''
now is the winter of our discontent made glorious summer by this sun of york
''')
prints::
00 11 22 aa FF 0a 0d 1a
[0, 17, 34, 170, 255, 10, 13, 26]
my kingdom for a horse
['MY', 'KINGDOM', 'FOR', 'A', 'HORSE']
now is the winter of our discontent made glorious summer by this sun of york
['Now Is The Winter Of Our Discontent Made Glorious Summer By This Sun Of York']
pyparsing.tokenMap(func, *args)
pyparsing.tokenMap(
func,
args,
)
pyparsing.token_map()
elements of a :class:ParseResults
list. If any additional args are passed,
they are forwarded to the given function as additional arguments
after the token, as in
hex_integer = Word(hexnums).set_parse_action(token_map(int, 16))
,
which will convert the parsed data to an integer using base 16.
Example (compare the last to example in :class:ParserElement.transform_string
::
hex_ints = OneOrMore(Word(hexnums)).set_parse_action(token_map(int, 16))
hex_ints.run_tests('''
00 11 22 aa FF 0a 0d 1a
''')
upperword = Word(alphas).set_parse_action(token_map(str.upper))
OneOrMore(upperword).run_tests('''
my kingdom for a horse
''')
wd = Word(alphas).set_parse_action(token_map(str.title))
OneOrMore(wd).set_parse_action(' '.join).run_tests('''
now is the winter of our discontent made glorious summer by this sun of york
''')
prints::
00 11 22 aa FF 0a 0d 1a
[0, 17, 34, 170, 255, 10, 13, 26]
my kingdom for a horse
['MY', 'KINGDOM', 'FOR', 'A', 'HORSE']
now is the winter of our discontent made glorious summer by this sun of york
['Now Is The Winter Of Our Discontent Made Glorious Summer By This Sun Of York']
pyparsing.token_map(func, *args)
pyparsing.token_map(
func,
args,
)
pyparsing.traceParseAction()
When the parse action is called, this decorator will print
"\>\> entering method-name(line:\<current_source_line\>, \<parse_location\>, \<matched_tokens\>)"
.
When the parse action completes, the decorator will print
"\<\<"
followed by the returned value, or any exception that the parse action raised.
Example::
wd = Word(alphas)
@trace_parse_action
def remove_duplicate_chars(tokens):
return ''.join(sorted(set(''.join(tokens))))
wds = OneOrMore(wd).set_parse_action(remove_duplicate_chars)
print(wds.parse_string("slkdjs sld sldd sdlf sdljf"))
prints::
\>\>entering remove_duplicate_chars(line: 'slkdjs sld sldd sdlf sdljf', 0, (['slkdjs', 'sld', 'sldd', 'sdlf', 'sdljf'], {}))
\<\<leaving remove_duplicate_chars (ret: 'dfjkls')
['dfjkls']
pyparsing.traceParseAction(f: Union[Callable[[], Any], Callable[[pyparsing.results.ParseResults], Any], Callable[[int, pyparsing.results.ParseResults], Any], Callable[[str, int, pyparsing.results.ParseResults], Any]])
pyparsing.traceParseAction(
f,
)
pyparsing.trace_parse_action()
When the parse action is called, this decorator will print
"\>\> entering method-name(line:\<current_source_line\>, \<parse_location\>, \<matched_tokens\>)"
.
When the parse action completes, the decorator will print
"\<\<"
followed by the returned value, or any exception that the parse action raised.
Example::
wd = Word(alphas)
@trace_parse_action
def remove_duplicate_chars(tokens):
return ''.join(sorted(set(''.join(tokens))))
wds = OneOrMore(wd).set_parse_action(remove_duplicate_chars)
print(wds.parse_string("slkdjs sld sldd sdlf sdljf"))
prints::
\>\>entering remove_duplicate_chars(line: 'slkdjs sld sldd sdlf sdljf', 0, (['slkdjs', 'sld', 'sldd', 'sdlf', 'sdljf'], {}))
\<\<leaving remove_duplicate_chars (ret: 'dfjkls')
['dfjkls']
pyparsing.trace_parse_action(f: Union[Callable[[], Any], Callable[[pyparsing.results.ParseResults], Any], Callable[[int, pyparsing.results.ParseResults], Any], Callable[[str, int, pyparsing.results.ParseResults], Any]])
pyparsing.trace_parse_action(
f,
)
pyparsing.ungroup()
even if all but one are non-empty.
pyparsing.ungroup(expr: pyparsing.core.ParserElement) -> pyparsing.core.ParserElement
pyparsing.ungroup(
expr,
)
pyparsing.withAttribute()
tags created with :class:make_xml_tags
or
pyparsing.withAttribute(*args, **attr_dict)
pyparsing.withAttribute(
args,
attr_dict,
)
pyparsing.withClass()
matching on a div class - made difficult because class
is
a reserved word in Python.
Example::
html = '''
\<div\>
Some text
\<div class="grid"\>1 4 0 1 0\</div\>
\<div class="graph"\>1,3 2,3 1,1\</div\>
\<div\>this <div> has no class\</div\>
\</div\>
'''
div,div_end = make_html_tags("div")
div_grid = div().set_parse_action(with_class("grid"))
grid_expr = div_grid + SkipTo(div | div_end)("body")
for grid_header in grid_expr.search_string(html):
print(grid_header.body)
div_any_type = div().set_parse_action(with_class(withAttribute.ANY_VALUE))
div_expr = div_any_type + SkipTo(div | div_end)("body")
for div_header in div_expr.search_string(html):
print(div_header.body)
prints::
1 4 0 1 0
1 4 0 1 0
1,3 2,3 1,1
pyparsing.withClass(classname, namespace='')
pyparsing.withClass(
classname,
namespace,
)
pyparsing.with_attribute()
tags created with :class:make_xml_tags
or
pyparsing.with_attribute(*args, **attr_dict)
pyparsing.with_attribute(
args,
attr_dict,
)
pyparsing.with_class()
matching on a div class - made difficult because class
is
a reserved word in Python.
Example::
html = '''
\<div\>
Some text
\<div class="grid"\>1 4 0 1 0\</div\>
\<div class="graph"\>1,3 2,3 1,1\</div\>
\<div\>this <div> has no class\</div\>
\</div\>
'''
div,div_end = make_html_tags("div")
div_grid = div().set_parse_action(with_class("grid"))
grid_expr = div_grid + SkipTo(div | div_end)("body")
for grid_header in grid_expr.search_string(html):
print(grid_header.body)
div_any_type = div().set_parse_action(with_class(withAttribute.ANY_VALUE))
div_expr = div_any_type + SkipTo(div | div_end)("body")
for div_header in div_expr.search_string(html):
print(div_header.body)
prints::
1 4 0 1 0
1 4 0 1 0
1,3 2,3 1,1
pyparsing.with_class(classname, namespace='')
pyparsing.with_class(
classname,
namespace,
)
pyparsing.wraps()
Returns a decorator that invokes update_wrapper() with the decorated function as the wrapper argument and the arguments to wraps() as the remaining arguments. Default arguments are as for update_wrapper(). This is a convenience function to simplify applying partial() to update_wrapper().
pyparsing.wraps(wrapped, assigned=('__module__', '__name__', '__qualname__', '__doc__', '__annotations__'), updated=('__dict__',))
pyparsing.wraps(
wrapped,
assigned,
updated,
)