Skip to content

Commit

Permalink
ENH: read_clipboard function using code from IPython, GH #300
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Nov 14, 2011
1 parent 90de456 commit fa10e29
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
2 changes: 2 additions & 0 deletions RELEASE.rst
Expand Up @@ -61,6 +61,8 @@ pandas 0.5.1
- Add bar plot option to `DataFrame.plot` (PR #348)
- Add `idxmin` and `idxmax` functions to Series and DataFrame for computing
index labels achieving maximum and minimum values (PR #286)
- Add `read_clipboard` function for parsing DataFrame from OS clipboard,
should work across platforms (GH #300)
**Improvements to existing features**

Expand Down
2 changes: 1 addition & 1 deletion pandas/__init__.py
Expand Up @@ -22,7 +22,7 @@
from pandas.core.api import *
from pandas.core.common import set_printoptions
from pandas.core.common import set_eng_float_format
from pandas.io.parsers import read_csv, read_table, ExcelFile
from pandas.io.parsers import read_csv, read_table, read_clipboard, ExcelFile
from pandas.io.pytables import HDFStore
from pandas.stats.api import *
from pandas.util.testing import debug
Expand Down
13 changes: 13 additions & 0 deletions pandas/io/parsers.py
Expand Up @@ -75,6 +75,19 @@ def read_table(filepath_or_buffer, sep='\t', header=0, index_col=None,
nrows=nrows, iterator=iterator, chunksize=chunksize,
skip_footer=skip_footer, converters=converters)

def read_clipboard(**kwargs): # pragma: no cover
"""
Read text from clipboard and pass to read_table. See read_table for the full
argument list
Returns
-------
parsed : DataFrame
"""
from pandas.util.clipboard import clipboard_get
text = clipboard_get()
return read_table(StringIO(text), **kwargs)

_parser_params = """Also supports optionally iterating or breaking of the file
into chunks.
Expand Down
69 changes: 69 additions & 0 deletions pandas/util/clipboard.py
@@ -0,0 +1,69 @@
"""
Taken from the IPython project http://ipython.org
Used under the terms of the BSD license
"""

import subprocess
import sys

def clipboard_get():
""" Get text from the clipboard.
"""
if sys.platform == 'win32':
try:
return win32_clipboard_get()
except Exception:
pass
elif sys.platform == 'darwin':
try:
return osx_clipboard_get()
except Exception:
pass
return tkinter_clipboard_get()

def win32_clipboard_get():
""" Get the current clipboard's text on Windows.
Requires Mark Hammond's pywin32 extensions.
"""
try:
import win32clipboard
except ImportError:
message = ("Getting text from the clipboard requires the pywin32 "
"extensions: http://sourceforge.net/projects/pywin32/")
raise Exception(message)
win32clipboard.OpenClipboard()
text = win32clipboard.GetClipboardData(win32clipboard.CF_TEXT)
# FIXME: convert \r\n to \n?
win32clipboard.CloseClipboard()
return text

def osx_clipboard_get():
""" Get the clipboard's text on OS X.
"""
p = subprocess.Popen(['pbpaste', '-Prefer', 'ascii'],
stdout=subprocess.PIPE)
text, stderr = p.communicate()
# Text comes in with old Mac \r line endings. Change them to \n.
text = text.replace('\r', '\n')
return text

def tkinter_clipboard_get():
""" Get the clipboard's text using Tkinter.
This is the default on systems that are not Windows or OS X. It may
interfere with other UI toolkits and should be replaced with an
implementation that uses that toolkit.
"""
try:
import Tkinter
except ImportError:
message = ("Getting text from the clipboard on this platform "
"requires Tkinter.")
raise Exception(message)
root = Tkinter.Tk()
root.withdraw()
text = root.clipboard_get()
root.destroy()
return text
14 changes: 14 additions & 0 deletions scripts/boxplot_test.py
@@ -0,0 +1,14 @@
import matplotlib.pyplot as plt

import random
import pandas.util.testing as tm
tm.N = 1000
df = tm.makeTimeDataFrame()
import string
foo = list(string.letters[:5]) * 200
df['indic'] = list(string.letters[:5]) * 200
random.shuffle(foo)
df['indic2'] = foo
df.boxplot(by=['indic', 'indic2'], fontsize=8, rot=90)

plt.show()

0 comments on commit fa10e29

Please sign in to comment.