From 8a7a2c372351a9e1da5a907b65887552136a3f53 Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Thu, 13 Oct 2011 16:56:48 +0100 Subject: [PATCH 1/2] Add columns to IPython tab completions of DataFrame attributes. --- pandas/core/frame.py | 19 +++++++++++++++++-- pandas/util/py3compat.py | 11 +++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 0efdcd838bf63..934dfb04b8ec1 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -15,6 +15,7 @@ from StringIO import StringIO import csv import operator +import sys import warnings from numpy import nan @@ -550,7 +551,6 @@ def to_string(self, buf=None, columns=None, colSpace=None, nanRep='NaN', formatters=None, float_format=None, sparsify=True): from pandas.core.common import _format, adjoin - import sys if buf is None: # pragma: no cover buf = sys.stdout @@ -629,7 +629,6 @@ def info(self, verbose=True, buf=None): If False, don't print column count summary buf : writable buffer, defaults to sys.stdout """ - import sys if buf is None: # pragma: no cover buf = sys.stdout @@ -3304,6 +3303,22 @@ def _homogenize(data, index, columns, dtype=None): def _put_str(s, space): return ('%s' % s)[:space].ljust(space) +def install_ipython_completers(): + """Register the DataFrame type with IPython's tab completion machinery, so + that it knows about accessing column names as attributes.""" + from IPython.utils.generics import complete_object + + @complete_object.when_type(DataFrame) + def complete_dataframe(obj, prev_completions): + return prev_completions + [c for c in obj.columns \ + if py3compat.isidentifier(c)] + +# Importing IPython brings in about 200 modules, so we want to avoid it unless +# we're in IPython (when those modules are loaded anyway). +if "IPython" in sys.modules: + install_ipython_completers() + + if __name__ == '__main__': import nose nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'], diff --git a/pandas/util/py3compat.py b/pandas/util/py3compat.py index e8bb212e215f2..afb48ef41cc95 100644 --- a/pandas/util/py3compat.py +++ b/pandas/util/py3compat.py @@ -1,3 +1,14 @@ import sys PY3 = (sys.version_info[0] >= 3) + +if PY3: + def isidentifier(s): + return s.isidentifier() + +else: + # Python 2 + import re + _name_re = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*$") + def isidentifier(s, dotted=False): + return bool(_name_re.match(s)) From 07f5324191db789abf8e376726efdc03368619d5 Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Thu, 13 Oct 2011 21:22:44 +0100 Subject: [PATCH 2/2] Catch and silence errors in installing IPython completers. Avoid problems with older versions of IPython (pre 0.11) Closes gh-230 --- pandas/core/frame.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 934dfb04b8ec1..90ae7feb15d83 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -3316,7 +3316,10 @@ def complete_dataframe(obj, prev_completions): # Importing IPython brings in about 200 modules, so we want to avoid it unless # we're in IPython (when those modules are loaded anyway). if "IPython" in sys.modules: - install_ipython_completers() + try: + install_ipython_completers() + except Exception: + pass if __name__ == '__main__':