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

Make "copy" work better with numpy arrays #2577

Merged
merged 3 commits into from
Oct 7, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions spyderlib/widgets/dicteditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
from spyderlib.widgets.texteditor import TextEditor
from spyderlib.widgets.importwizard import ImportWizard
from spyderlib.py3compat import (to_text_string, to_binary_string,
is_text_string, is_binary_string, getcwd, u)
is_text_string, is_binary_string, getcwd, u,
PY3, io)


LARGE_NROWS = 5000
Expand Down Expand Up @@ -1075,7 +1076,23 @@ def copy(self):
for idx in self.selectedIndexes():
if not idx.isValid():
continue
clipl.append(to_text_string(self.delegate.get_value(idx)))
obj = self.delegate.get_value(idx)
# check if it's a numpy array, and if so make sure to copy the whole
# thing in a tab separated format
try:
import numpy as np
except ImportError:
pass # skip this part
Copy link
Member

Choose a reason for hiding this comment

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

I think this try/except is not needed. If Numpy arrays are shown in the Variable Explorer, that means numpy is already present, so we don't need to verify for its presence.

Copy link
Contributor

Choose a reason for hiding this comment

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

You need it to check if the object is a ndarray. You you need it in order to know if you need it !

else:
if isinstance(obj, np.ndarray):
if PY3:
output = io.BytesIO()
else:
output = io.StringIO()
np.savetxt(output, obj, delimiter='\t')
obj = output.getvalue().decode('utf-8')
Copy link
Member

Choose a reason for hiding this comment

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

This line is not needed. That's why you're using to_text_string below.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, forget my comment, this is really necessary :-)


clipl.append(to_text_string(obj))
Copy link
Member

Choose a reason for hiding this comment

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

This is use of to_text_string is what is not needed, i.e. this line needs to be

clipl.append(obj)

because obj is already in utf-8

clipboard.setText(u('\n').join(clipl))

def import_from_string(self, text, title=None):
Expand Down