Skip to content
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

shorter and faster version extraction from __init__.py #2518

Merged
merged 2 commits into from
Mar 27, 2015

Conversation

deronnax
Copy link
Contributor

@sigmavirus24 I though it could be improved a bit. Tell me what you think.

@deronnax
Copy link
Contributor Author

if you think readability has been tampered too much, I'll make a pull request with this less-pure-but-softer version:

-    for line in fd:
-        m = reg.match(line)
-        if m:
-            version = m.group(1)
-            break
+    version = reg.search(fd.read())

@Lukasa
Copy link
Member

Lukasa commented Mar 25, 2015

No objections from me. 👍

@sigmavirus24
Copy link
Contributor

Do you have benchmarks? Also, why are we optimizing something that is run at most once by a user (during pip install)?

@deronnax
Copy link
Contributor Author

It's not optimizing. It's making it simpler. And thus that it's simpler also makes faster.
What you did was opening the file, buffering it, crawling it line by line (triggering a search for line terminators whereas we don't care about) and applying a regex on each line.
What I do is opening and reading the whole file and applying the regex on the content.
This uninteresting microbenchmark shows us it's 3 time faster, but that's not my point. It's simpler, that's what matter :)

if m:
version = m.group(1)
break
version = re.search(r'__version__\s*=\s*[\'"]([^\'"]*)[\'"]',
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we're not splitting on line terminators anymore and lazily ejecting when we've found what we're looking for, the regular expression needs to be updated. We want to make sure we don't grab any cruft from a separate line that has version in it.

match will automatically add the equivalent of ^ to the beginning of the regular expression. search does not. We don't want anything where there's a line that looks like

# We want __version__ = "some string"
...
__version__ = "2.6.1"

Because we'll get the wrong result. Please fix the expression to check for line beginnings and endings.

@sigmavirus24
Copy link
Contributor

Either way, this change is incomplete in its current form. I've left feedback on what is necessary to fix it.

@deronnax
Copy link
Contributor Author

Yeah, sorry for the cardet, I thought about it and forgot.
I think using the dollar and thus requiring the "version" line ends with a newline is a bad idea : it might end with a whitespace or a comment, it's valid.
I think the cardet suffices. And it thus behaves the same as your version did ;)

@sigmavirus24
Copy link
Contributor

I think using the dollar and thus requiring the "version" line ends with a newline is a bad idea : it might end with a whitespace or a comment, it's valid.

No one asked for you to add anything other than the ^.

Thanks for fixing this up. Any further feedback @Lukasa?

@Lukasa
Copy link
Member

Lukasa commented Mar 27, 2015

Nope. Fine by me. =)

@deronnax
Copy link
Contributor Author

The

Please fix the expression to check for line beginnings and endings

misled me :)

sigmavirus24 added a commit that referenced this pull request Mar 27, 2015
shorter and faster version extraction from __init__.py
@sigmavirus24 sigmavirus24 merged commit 460caf7 into psf:master Mar 27, 2015
@sigmavirus24
Copy link
Contributor

I misspoke twice then! Sorry for the confusion @deronnax. Thanks for the contribution! 🍰

@deronnax
Copy link
Contributor Author

You're welcome :)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants