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

Copying when nothing is selected no longer affects the clipboard. #3043

Merged
merged 2 commits into from
Mar 9, 2016
Merged

Copying when nothing is selected no longer affects the clipboard. #3043

merged 2 commits into from
Mar 9, 2016

Conversation

stephenshank
Copy link
Contributor

Fixes #2835


My first attempt at contributing to open source... constructive criticism welcome!

@@ -492,7 +492,8 @@ def copy(self):
Reimplement Qt method
Copy text to clipboard with correct EOL chars
"""
QApplication.clipboard().setText(self.get_selected_text())
if len(self.get_selected_text()) > 0:
QApplication.clipboard().setText(self.get_selected_text())
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the contribution

In python saying

if len(self.get_selected_text()) > 0 is the same as if len(self.get_selected_text()): cause if len is 0 then it is the same as saying False.

So we use the second version (as it is common practice in Python)

You can try doing:

bool(0)
bool(1)
bool(100)
bool(-450)

@ccordoba12
Copy link
Member

@stephenshank, thanks a lot for your contribution! What you did is exactly what it needed to be done, so well job :-)

What @goanpeca meant is that removing > 0 in the code you added is all we require from you to merge this because in Python there's no need of that further check.

I think even if self.get_selected_text() is a right solution because if there's no selected text, then self.get_selected_text() will return an empty string, and in Python if '' (like if [] for empty lists) returns False.

@stephenshank
Copy link
Contributor Author

Thanks to both of you for your feedback! After experimenting with %timeit, I ended up going with
if self.get_selected_text():
since this gave the most favorable performance among the three variations (and still resolved the issue with the clipboard, of course).

@ccordoba12
Copy link
Member

Ok, your first contribution to open source is going in ;-)

ccordoba12 added a commit that referenced this pull request Mar 9, 2016
Copying when nothing is selected no longer affects the clipboard.
@ccordoba12 ccordoba12 merged commit 225a609 into spyder-ide:master Mar 9, 2016
@ccordoba12
Copy link
Member

Please notice that this fix is going to be available since Spyder 3.0 beta3, and not in the 2.3.x series (2.3.9 is the next planned release :-)

@Nodd
Copy link
Contributor

Nodd commented Mar 9, 2016

Welcome to a whole new world ! 😄

I jusst have a remark about your reasoning. In non critical parts, the correct code isn't usually determined by %timeit (even if this result is interesting in itself). In that case performance matters less that maintainability and coherency. Having a better performance is of course a plus, but the user will never notice if Ctrl+C is 0.1ms slower or faster.

if self.get_selected_text(): is more pythonic, which means that it's the less surprising form to someone used to python, so it will be easier in the future to understand, debug or modify.

@stephenshank
Copy link
Contributor Author

Great news! Thanks so much! I can see that I clearly have a lot to learn from this community... you can expect to see more pull requests from me in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants