FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix error with pyparsing 3 for 3.5.x by timhoffm · Pull Request #21454 · matplotlib/matplotlib · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (3) .txt  (2) All 2 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
4 changes: 2 additions & 2 deletions lib/matplotlib/_mathtext.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import numpy as np
from pyparsing import (
Combine, Empty, FollowedBy, Forward, Group, Literal, oneOf, OneOrMore,
Combine, Empty, Forward, Group, Literal, oneOf, OneOrMore,
Optional, ParseBaseException, ParseFatalException, ParserElement,
ParseResults, QuotedString, Regex, StringEnd, Suppress, ZeroOrMore)

Expand Down Expand Up @@ -2044,7 +2044,7 @@ def __init__(self):
p.accentprefixed <<= Suppress(p.bslash) + oneOf(self._accentprefixed)
p.symbol_name <<= (
Combine(p.bslash + oneOf(list(tex2uni)))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Could this also have been fixed by changing oneOf(list(tex2uni)) to oneOf(list(tex2uni), asKeyword=True) and removing the FollowedBy altogether? This has the benefit of reducing the number of pyparsing terms to be matched, which should translate to faster parsing.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I think the problem of asKeyword is that "word boundary" needs a custom definition here (e.g. underscores and digits also separate words). I this this is configurable on pyparsing's side, but I'd rather just write our own regex (something like (?![a-zA-Z])) and be done with it.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This sounds like a better approach than trying to do custom keywords.

If you really want to collapse this down to a single Regex, then you could use oneOf to build a Regex for you of just the word choices, but then extract the generated regex pattern into a new Regex, something like Regex(r"\\(" + oneOf(text2uni).pattern + ")(?![a-zA-Z])"). (untested, ymmv, etc.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

tex symbols should not have any metacharacters, so the middle part is probably even just "|".join(tex2uni).

ptmcg Oct 29, 2021 •
edited
Loading

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Beware of this - oneOf also takes care of reordering longer entries before shorter in the event the shorter entry is a leading subset, and also deduping. You could just reverse sort the keys in tex2uni by length, but if there are known frequencies to some symbols vs others, and more frequent entries were placed first, oneOf would only reorder them to avoid masking entries, whereas sorting by longest-to-shortest would lose some of this priority ordering.

You can test this for yourself:

re.match(r"ab|abb", "abb")

will return "ab", not the longer and more desirable match "abb". And there are several cases in tex2uni of these kind of masking pairs.

Maybe keep it simple first, just do "|".join(sorted(set(tex2uni), key=len, reverse=True)) and then get clever with the frequency-based ordering as a later experiment.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Ah, good point, thanks for mentioning that.

+ FollowedBy(Regex("[^A-Za-z]").leaveWhitespace() | StringEnd())
+ Suppress(Regex("(?=[^A-Za-z]|$)").leaveWhitespace())
)
p.symbol <<= (p.single_symbol | p.symbol_name).leaveWhitespace()

Expand Down
8 changes: 5 additions & 3 deletions lib/matplotlib/tests/test_mathtext.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ def test_fontinfo():
(r'$\leftF$', r'Expected a delimiter'),
(r'$\rightF$', r'Unknown symbol: \rightF'),
(r'$\left(\right$', r'Expected a delimiter'),
(r'$\left($', r'Expected "\right"'),
# PyParsing 2 uses double quotes, PyParsing 3 uses single quotes and an
# extra backslash.
(r'$\left($', re.compile(r'Expected ("|\'\\)\\right["\']')),
(r'$\dfrac$', r'Expected \dfrac{num}{den}'),
(r'$\dfrac{}{}$', r'Expected \dfrac{num}{den}'),
(r'$\overset$', r'Expected \overset{body}{annotation}'),
Expand Down Expand Up @@ -281,8 +283,8 @@ def test_fontinfo():
)
def test_mathtext_exceptions(math, msg):
parser = mathtext.MathTextParser('agg')

with pytest.raises(ValueError, match=re.escape(msg)):
match = re.escape(msg) if isinstance(msg, str) else msg
with pytest.raises(ValueError, match=match):
parser.parse(math)


Expand Down
1 change: 0 additions & 1 deletion requirements/doc/doc-requirements.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ ipython
ipywidgets
numpydoc>=0.8
packaging>=20
pyparsing<3.0.0
pydata-sphinx-theme>=0.6.0
sphinxcontrib-svg2pdfconverter>=1.1.0
sphinx-gallery>=0.10
Expand Down
1 change: 0 additions & 1 deletion requirements/testing/all.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

certifi
coverage
pyparsing<3.0.0
pytest!=4.6.0,!=5.4.0
pytest-cov
pytest-rerunfailures
Expand Down
2 changes: 1 addition & 1 deletion setup.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def make_release_tree(self, base_dir, files):
"numpy>=1.17",
"packaging>=20.0",
"pillow>=6.2.0",
"pyparsing>=2.2.1,<3.0.0",
"pyparsing>=2.2.1",
"python-dateutil>=2.7",
] + (
# Installing from a git checkout.
Expand Down

Back | FazBrowse Home | New Git URL