Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add found string as property#571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ptmcg merged 3 commits intopyparsing:masterfromOdysseasKr:exception-found
Sep 23, 2024
Merged

Add found string as property#571

ptmcg merged 3 commits intopyparsing:masterfromOdysseasKr:exception-found
Sep 23, 2024

Conversation

@OdysseasKr
Copy link
Contributor

A bit of a silly change, feel free to close if you don't accept these.

I have moved the logic of the "found" string out of the__str__ magic method. This allows the user to generate prettier messages from the exception. I also think it's a bit cleaner to avoid parsing logic in__str__

Example

From the following exception

In [2]:Keyword("KEY1").parse_string("KEY2",parseAll=True)---------------------------------------------------------------------------ParseExceptionTraceback (mostrecentcalllast)CellIn[2],line1---->1Keyword("KEY1").parse_string("KEY2",parseAll=True)File~/Documents/pyparsing/pyparsing/core.py:1197,inParserElement.parse_string(self,instring,parse_all,parseAll)1194raise1195else:1196# catch and re-raise exception from here, clearing out pyparsing internal stack trace->1197raiseexc.with_traceback(None)1198else:1199returntokensParseException:ExpectedKeyword'KEY1',found'KEY2'  (atchar0), (line:1,col:1)

I would like to generate a message like this

1:1 Expected Keyword 'KEY1', found 'KEY2'

With the existing implementation the user would have to call__str__ and then remove the character, line and column information.

Moved logic about the "found" string from __str__ to its own property in`ParseBaseException`.
@OdysseasKrOdysseasKr changed the titleAdd found str as propertyAdd found string as propertySep 22, 2024
@ptmcg
Copy link
Member

I'll give it a look. There are a few very popular projects that use pyparsing and rely on the specific format of exception messages, so I try to keep these messages from changing if I can help it.

Adding a new property should not be a problem, and with that you might be able to monkeypatchParseBaseException.__str__ to reformat it to your liking.

And those projects might also benefit from this new property, so there is that possibililty as well (though pyparsing elements of most projects are often in the category of "that one guy wrote this 10 years ago, so let's not ever change it").

@ptmcg
Copy link
Member

Ok, I think this would make for a nice change, and there are likely some (slight) performance improvements we can do, too. See my review comment.

@ptmcg
Copy link
Member

Or if you'd rather, I can accept your PR as-is, and then make these adjustments afterward (and also add tests and CHANGES note).

@OdysseasKr
Copy link
ContributorAuthor

OdysseasKr commentedSep 23, 2024
edited
Loading

Makes sense, should have used cached_property for this. I will make the changes today!

Use `cached_property` to prevent re-computing properties of `ParseBaseException`
@ptmcgptmcg self-assigned thisSep 23, 2024
Copy link
Member

@ptmcgptmcg left a comment

Choose a reason for hiding this comment

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

Looks good to me, merging

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@ptmcgptmcgptmcg approved these changes

Assignees

@ptmcgptmcg

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@OdysseasKr@ptmcg

[8]ページ先頭

©2009-2025 Movatter.jp