summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pyparsing/__init__.py44
-rw-r--r--pyparsing/common.py35
-rw-r--r--pyparsing/core.py5
-rw-r--r--pyparsing/helpers.py41
-rw-r--r--pyparsing/results.py3
-rw-r--r--pyparsing/util.py25
-rw-r--r--tests/test_unit.py4
7 files changed, 99 insertions, 58 deletions
diff --git a/pyparsing/__init__.py b/pyparsing/__init__.py
index d26557f..dcf4622 100644
--- a/pyparsing/__init__.py
+++ b/pyparsing/__init__.py
@@ -85,11 +85,11 @@ classes inherit from. Use the docstrings for examples of how to:
and :class:`'&'<Each>` operators to combine simple expressions into
more complex ones
- associate names with your parsed results using
- :class:`ParserElement.setResultsName`
+ :class:`ParserElement.set_results_name`
- access the parsed data, which is returned as a :class:`ParseResults`
object
- - find some helpful expression short-cuts like :class:`delimitedList`
- and :class:`oneOf`
+ - find some helpful expression short-cuts like :class:`delimited_list`
+ and :class:`one_of`
- find more useful common expressions in the :class:`pyparsing_common`
namespace class
"""
@@ -121,7 +121,7 @@ class version_info(NamedTuple):
__version_info__ = version_info(3, 0, 10, "final", 0)
-__version_time__ = "10 Jun 2022 05:40 UTC"
+__version_time__ = "16 Jun 2022 07:11 UTC"
__version__ = __version_info__.__version__
__versionTime__ = __version_time__
__author__ = "Paul McGuire <ptmcg.gm+pyparsing@gmail.com>"
@@ -166,6 +166,7 @@ __all__ = [
"CaselessKeyword",
"CaselessLiteral",
"CharsNotIn",
+ "CloseMatch",
"Combine",
"Dict",
"Each",
@@ -219,9 +220,11 @@ __all__ = [
"alphas8bit",
"any_close_tag",
"any_open_tag",
+ "autoname_elements",
"c_style_comment",
"col",
"common_html_entity",
+ "condition_as_parse_action",
"counted_array",
"cpp_style_comment",
"dbl_quoted_string",
@@ -233,6 +236,7 @@ __all__ = [
"html_comment",
"identchars",
"identbodychars",
+ "infix_notation",
"java_style_comment",
"line",
"line_end",
@@ -247,8 +251,12 @@ __all__ = [
"null_debug_action",
"nums",
"one_of",
+ "original_text_for",
"printables",
"punc8bit",
+ "pyparsing_common",
+ "pyparsing_test",
+ "pyparsing_unicode",
"python_style_comment",
"quoted_string",
"remove_quotes",
@@ -259,28 +267,20 @@ __all__ = [
"srange",
"string_end",
"string_start",
+ "token_map",
"trace_parse_action",
+ "ungroup",
+ "unicode_set",
"unicode_string",
"with_attribute",
- "indentedBlock",
- "original_text_for",
- "ungroup",
- "infix_notation",
- "locatedExpr",
"with_class",
- "CloseMatch",
- "token_map",
- "pyparsing_common",
- "pyparsing_unicode",
- "unicode_set",
- "condition_as_parse_action",
- "pyparsing_test",
# pre-PEP8 compatibility names
"__versionTime__",
"anyCloseTag",
"anyOpenTag",
"cStyleComment",
"commonHTMLEntity",
+ "conditionAsParseAction",
"countedArray",
"cppStyleComment",
"dblQuotedString",
@@ -288,9 +288,12 @@ __all__ = [
"delimitedList",
"dictOf",
"htmlComment",
+ "indentedBlock",
+ "infixNotation",
"javaStyleComment",
"lineEnd",
"lineStart",
+ "locatedExpr",
"makeHTMLTags",
"makeXMLTags",
"matchOnlyAtCol",
@@ -300,6 +303,7 @@ __all__ = [
"nullDebugAction",
"oneOf",
"opAssoc",
+ "originalTextFor",
"pythonStyleComment",
"quotedString",
"removeQuotes",
@@ -309,15 +313,9 @@ __all__ = [
"sglQuotedString",
"stringEnd",
"stringStart",
+ "tokenMap",
"traceParseAction",
"unicodeString",
"withAttribute",
- "indentedBlock",
- "originalTextFor",
- "infixNotation",
- "locatedExpr",
"withClass",
- "tokenMap",
- "conditionAsParseAction",
- "autoname_elements",
]
diff --git a/pyparsing/common.py b/pyparsing/common.py
index 1859fb7..9b52ed0 100644
--- a/pyparsing/common.py
+++ b/pyparsing/common.py
@@ -22,17 +22,17 @@ class pyparsing_common:
Parse actions:
- - :class:`convertToInteger`
- - :class:`convertToFloat`
- - :class:`convertToDate`
- - :class:`convertToDatetime`
- - :class:`stripHTMLTags`
- - :class:`upcaseTokens`
- - :class:`downcaseTokens`
+ - :class:`convert_to_integer`
+ - :class:`convert_to_float`
+ - :class:`convert_to_date`
+ - :class:`convert_to_datetime`
+ - :class:`strip_html_tags`
+ - :class:`upcase_tokens`
+ - :class:`downcase_tokens`
Example::
- pyparsing_common.number.runTests('''
+ pyparsing_common.number.run_tests('''
# any int or real number, returned as the appropriate type
100
-100
@@ -42,7 +42,7 @@ class pyparsing_common:
1e-12
''')
- pyparsing_common.fnumber.runTests('''
+ pyparsing_common.fnumber.run_tests('''
# any int or real number, returned as float
100
-100
@@ -52,19 +52,19 @@ class pyparsing_common:
1e-12
''')
- pyparsing_common.hex_integer.runTests('''
+ pyparsing_common.hex_integer.run_tests('''
# hex numbers
100
FF
''')
- pyparsing_common.fraction.runTests('''
+ pyparsing_common.fraction.run_tests('''
# fractions
1/2
-3/4
''')
- pyparsing_common.mixed_integer.runTests('''
+ pyparsing_common.mixed_integer.run_tests('''
# mixed fractions
1
1/2
@@ -73,8 +73,8 @@ class pyparsing_common:
''')
import uuid
- pyparsing_common.uuid.setParseAction(tokenMap(uuid.UUID))
- pyparsing_common.uuid.runTests('''
+ pyparsing_common.uuid.set_parse_action(token_map(uuid.UUID))
+ pyparsing_common.uuid.run_tests('''
# uuid
12345678-1234-5678-1234-567812345678
''')
@@ -411,12 +411,19 @@ class pyparsing_common:
# pre-PEP8 compatibility names
convertToInteger = convert_to_integer
+ """Deprecated - use :class:`convert_to_integer`"""
convertToFloat = convert_to_float
+ """Deprecated - use :class:`convert_to_float`"""
convertToDate = convert_to_date
+ """Deprecated - use :class:`convert_to_date`"""
convertToDatetime = convert_to_datetime
+ """Deprecated - use :class:`convert_to_datetime`"""
stripHTMLTags = strip_html_tags
+ """Deprecated - use :class:`strip_html_tags`"""
upcaseTokens = upcase_tokens
+ """Deprecated - use :class:`upcase_tokens`"""
downcaseTokens = downcase_tokens
+ """Deprecated - use :class:`downcase_tokens`"""
_builtin_exprs = [
diff --git a/pyparsing/core.py b/pyparsing/core.py
index 2506428..861548b 100644
--- a/pyparsing/core.py
+++ b/pyparsing/core.py
@@ -45,6 +45,7 @@ from .exceptions import *
from .actions import *
from .results import ParseResults, _ParseResultsWithOffset
from .unicode import pyparsing_unicode
+from .util import replaces_prePEP8_function
_MAX_INT = sys.maxsize
str_type: Tuple[type, ...] = (str, bytes)
@@ -322,6 +323,7 @@ def _trim_arity(func, max_limit=3):
return wrapper
+@replaces_prePEP8_function("conditionAsParseAction")
def condition_as_parse_action(
fn: ParseCondition, message: str = None, fatal: bool = False
) -> ParseAction:
@@ -5717,6 +5719,7 @@ def srange(s: str) -> str:
return ""
+@replaces_prePEP8_function("tokenMap")
def token_map(func, *args) -> ParseAction:
"""Helper to define a parse action by mapping a function to all
elements of a :class:`ParseResults` list. If any additional args are passed,
@@ -5799,8 +5802,6 @@ _builtin_exprs: List[ParserElement] = [
]
# backward compatibility names
-tokenMap = token_map
-conditionAsParseAction = condition_as_parse_action
nullDebugAction = null_debug_action
sglQuotedString = sgl_quoted_string
dblQuotedString = dbl_quoted_string
diff --git a/pyparsing/helpers.py b/pyparsing/helpers.py
index 9d2c9a2..4663177 100644
--- a/pyparsing/helpers.py
+++ b/pyparsing/helpers.py
@@ -5,12 +5,18 @@ import typing
from . import __diag__
from .core import *
-from .util import _bslash, _flatten, _escape_regex_range_chars
+from .util import (
+ _bslash,
+ _flatten,
+ _escape_regex_range_chars,
+ replaces_prePEP8_function,
+)
#
# global helpers
#
+@replaces_prePEP8_function("delimitedList")
def delimited_list(
expr: Union[str, ParserElement],
delim: Union[str, ParserElement] = ",",
@@ -66,6 +72,7 @@ def delimited_list(
return delimited_list_expr.set_name(dlName)
+@replaces_prePEP8_function("countedArray")
def counted_array(
expr: ParserElement,
int_expr: typing.Optional[ParserElement] = None,
@@ -126,6 +133,7 @@ def counted_array(
return (intExpr + array_expr).set_name("(len) " + str(expr) + "...")
+@replaces_prePEP8_function("matchPreviousLiteral")
def match_previous_literal(expr: ParserElement) -> ParserElement:
"""Helper to define an expression that is indirectly defined from
the tokens matched in a previous expression, that is, it looks for
@@ -159,6 +167,7 @@ def match_previous_literal(expr: ParserElement) -> ParserElement:
return rep
+@replaces_prePEP8_function("matchPreviousExpr")
def match_previous_expr(expr: ParserElement) -> ParserElement:
"""Helper to define an expression that is indirectly defined from
the tokens matched in a previous expression, that is, it looks for
@@ -195,6 +204,7 @@ def match_previous_expr(expr: ParserElement) -> ParserElement:
return rep
+@replaces_prePEP8_function("oneOf")
def one_of(
strs: Union[typing.Iterable[str], str],
caseless: bool = False,
@@ -320,6 +330,7 @@ def one_of(
)
+@replaces_prePEP8_function("dictOf")
def dict_of(key: ParserElement, value: ParserElement) -> ParserElement:
"""Helper to easily and clearly define a dictionary by specifying
the respective patterns for the key and value. Takes care of
@@ -360,6 +371,7 @@ def dict_of(key: ParserElement, value: ParserElement) -> ParserElement:
return Dict(OneOrMore(Group(key + value)))
+@replaces_prePEP8_function("originalTextFor")
def original_text_for(
expr: ParserElement, as_string: bool = True, *, asString: bool = True
) -> ParserElement:
@@ -455,6 +467,7 @@ def locatedExpr(expr: ParserElement) -> ParserElement:
)
+@replaces_prePEP8_function("nestedExpr")
def nested_expr(
opener: Union[str, ParserElement] = "(",
closer: Union[str, ParserElement] = ")",
@@ -642,6 +655,7 @@ def _makeTags(tagStr, xml, suppress_LT=Suppress("<"), suppress_GT=Suppress(">"))
return openTag, closeTag
+@replaces_prePEP8_function("makeHTMLTags")
def make_html_tags(
tag_str: Union[str, ParserElement]
) -> Tuple[ParserElement, ParserElement]:
@@ -669,6 +683,7 @@ def make_html_tags(
return _makeTags(tag_str, False)
+@replaces_prePEP8_function("makeXMLTags")
def make_xml_tags(
tag_str: Union[str, ParserElement]
) -> Tuple[ParserElement, ParserElement]:
@@ -692,12 +707,16 @@ common_html_entity = Regex("&(?P<entity>" + "|".join(_htmlEntityMap) + ");").set
)
-def replace_html_entity(t):
+@replaces_prePEP8_function("replaceHTMLEntity")
+def replace_html_entity(s, l, t):
"""Helper parser action to replace common HTML entities with their special characters"""
return _htmlEntityMap.get(t.entity)
class OpAssoc(Enum):
+ """Enumeration of operator associativity
+ - used in constructing InfixNotationOperatorSpec for :class:`infix_notation`"""
+
LEFT = 1
RIGHT = 2
@@ -720,6 +739,7 @@ InfixNotationOperatorSpec = Union[
]
+@replaces_prePEP8_function("infixNotation")
def infix_notation(
base_expr: ParserElement,
op_list: List[InfixNotationOperatorSpec],
@@ -1069,21 +1089,10 @@ _builtin_exprs: List[ParserElement] = [
# pre-PEP8 compatible names
-delimitedList = delimited_list
-countedArray = counted_array
-matchPreviousLiteral = match_previous_literal
-matchPreviousExpr = match_previous_expr
-oneOf = one_of
-dictOf = dict_of
-originalTextFor = original_text_for
-nestedExpr = nested_expr
-makeHTMLTags = make_html_tags
-makeXMLTags = make_xml_tags
-anyOpenTag, anyCloseTag = any_open_tag, any_close_tag
-commonHTMLEntity = common_html_entity
-replaceHTMLEntity = replace_html_entity
opAssoc = OpAssoc
-infixNotation = infix_notation
+anyOpenTag = any_open_tag
+anyCloseTag = any_close_tag
+commonHTMLEntity = common_html_entity
cStyleComment = c_style_comment
htmlComment = html_comment
restOfLine = rest_of_line
diff --git a/pyparsing/results.py b/pyparsing/results.py
index 3790090..892cbd9 100644
--- a/pyparsing/results.py
+++ b/pyparsing/results.py
@@ -749,8 +749,11 @@ class ParseResults:
return ret
asList = as_list
+ """Deprecated - use :class:`as_list`"""
asDict = as_dict
+ """Deprecated - use :class:`as_dict`"""
getName = get_name
+ """Deprecated - use :class:`get_name`"""
MutableMapping.register(ParseResults)
diff --git a/pyparsing/util.py b/pyparsing/util.py
index 47dbf30..b217844 100644
--- a/pyparsing/util.py
+++ b/pyparsing/util.py
@@ -1,9 +1,10 @@
# util.py
+import inspect
import warnings
import types
import collections
import itertools
-from functools import lru_cache
+from functools import lru_cache, wraps
from typing import List, Union, Iterable
_bslash = chr(92)
@@ -227,3 +228,25 @@ def _flatten(ll: list) -> list:
else:
ret.append(i)
return ret
+
+
+def _deprecated_prePEP8(fn):
+ @wraps(fn)
+ def _inner(*args, **kwargs):
+ # warnings.warn(
+ # f"Deprecated - use {fn.__name__}", DeprecationWarning, stacklevel=3
+ # )
+ return fn(*args, **kwargs)
+
+ _inner.__doc__ = f"""Deprecated - use :class:`{fn.__name__}`"""
+ return _inner
+
+
+def replaces_prePEP8_function(old_name):
+ ns = inspect.currentframe().f_back.f_locals
+
+ def _inner(fn):
+ ns[old_name] = _deprecated_prePEP8(fn)
+ return fn
+
+ return _inner
diff --git a/tests/test_unit.py b/tests/test_unit.py
index 1548959..4733029 100644
--- a/tests/test_unit.py
+++ b/tests/test_unit.py
@@ -7984,7 +7984,7 @@ class Test02_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase):
msg=f"raised {pp.Diagnostics.warn_ungrouped_named_tokens_in_collection}"
f" warning when warn on ungrouped named tokens was suppressed (original_text_for)"
):
- pp.originalTextFor(pp.Word("ABC")[...])("words")
+ pp.original_text_for(pp.Word("ABC")[...])("words")
def testWarnNameSetOnEmptyForward(self):
"""
@@ -8102,7 +8102,7 @@ class Test02_WithoutPackrat(ppt.TestParseResultsAsserts, TestCase):
with self.assertDoesNotWarn(
msg=f"raised {pp.Diagnostics.warn_on_multiple_string_args_to_oneof} warning when not enabled"
):
- a = pp.oneOf("A", "B")
+ a = pp.one_of("A", "B")
with ppt.reset_pyparsing_context():
pp.enable_diag(pp.Diagnostics.warn_on_multiple_string_args_to_oneof)