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
ENH add --negate flag to pygrep #1643
Conversation
d1913e7
to
ee3af6f
Compare
Marking as ready for review as I'd like to think this is enough to tell whether it's the right direction / whether the feature would be welcome. Thanks for the 'don't put logic in tests' link! |
pre_commit/languages/pygrep.py
Outdated
for line in f: | ||
if pattern.search(line): | ||
retv = 0 | ||
break |
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.
this can be return 0
and then below you can
else:
output.write_line(filename)
return 1
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.
Yes, much simpler, thanks!
Though here there wouldn't be an else
, would there? It's necessary to check through all the lines before deciding to return 1
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.
I mean for: ... else: ...
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.
Ah, I see, thanks! I see you have a video about this too, will watch now https://youtu.be/8P7lXLXR_3c
abac169
to
8c1a551
Compare
pre_commit/languages/pygrep.py
Outdated
retv |= _process_filename_at_once(pattern, filename) | ||
else: | ||
retv |= _process_filename_by_line(pattern, filename) | ||
process_fn = FNS[Choice(multiline=args.multiline, negate=args.negate)] |
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.
this should be outside the loop, otherwise you're doing this work every iteration instead of just once
8c1a551
to
a0658c0
Compare
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.
Thank you so much for the mentorship you provided me here, I really appreciate it, it's very kind of you! |
xref this comment on StackOverflow: https://stackoverflow.com/a/64344519/9988333
A use-case comes from
PyMC3
, where there is a pre-commit hook to check that all notebooks have a watermark. Currently, this is done by definingin https://github.com/pymc-devs/pymc3/blob/master/.pre-commit-config.yaml, and then
in https://github.com/pymc-devs/pymc3/blob/master/scripts/check_watermark.py .
I think the output should be a file which does not contain the pattern...I'll work on this tomorrow