-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
highlight_regex in rich.text.Text Now Expects a Compiled Regular Expression (re.compile) Has Been Passed #3347
highlight_regex in rich.text.Text Now Expects a Compiled Regular Expression (re.compile) Has Been Passed #3347
Conversation
…t' is a compiled regular expression. If a str is passed, as was the previous assumption, the str will be compiled as a regular expression.
Changelog and tests please. |
@PyWoody Are you still working on this? |
Hi @willmcgugan , Sorry! This slipped through the cracks. Please find the updated CHANGELOG and requisite tests in the branch now. Please let me know if there is anything else you need done or modified and I'd be glad to do it. |
Sorry, in haste I pushed from a location that didn't have the pre-commit hooks installed. I've installed the hooks, updated the test file according to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks
Type of changes
Checklist
Description
highlight_regex
inrich.text.Text
now expects the passedre_highlight
to be a compiled regular expression. If astr
is passed, as was the previous expectation, thestr
will be compiled as a regular expression and the method will continue on as before.The proposed change is for a minor performance bump in the
rich.highlighter.RegexHighlighter
class. A user can now pass compiled regular expressions in the the list ofhighlights
, which will be guaranteed to not re-compiled byText
. Previously,highlight_regex
expected it was receiving astr
so it compiled the regular expression every time.There will be no breaking changes to the user as the user can continue to pass
str
in their respectiveRegexHighlighter.highlights
. The only performance degradation will be caused by theisinstance(re_highlight, str)
check, which will be minimal.Below is a silly speed test (hopefully) showing the new
highlight_regex
as performing faster. I used 600 "words" for the regular expressions because Python's currentre.
actions use a shared internal cache of a maximum of 512[0] regular expressions and I wanted demonstrate how relying on that can cause slowdowns.[0] https://github.com/python/cpython/blob/3.12/Lib/re/__init__.py#L277