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

ENH: expose option_context as a top-level API GH5618 #5752

Merged
3 commits merged into from Dec 31, 2013
Merged
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions doc/source/basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,21 @@ It's also possible to reset multiple options at once (using a regex):
reset_option("^display")


.. versionadded:: 0.14.0

Beginning with v0.14.0 the `option_context` context manager has been exposed through
the top-level API, allowing you to execute code with given option values. Option values
are restored automatically when you exit the `with` block:

.. ipython:: python

with option_context("display.max_rows",10,"display.max_columns", 5):
print get_option("display.max_rows")
print get_option("display.max_columns")

print get_option("display.max_rows")
print get_option("display.max_columns")


Console Output Formatting
-------------------------
Expand Down
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Improvements to existing features
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- perf improvements in Series datetime/timedelta binary operations (:issue:`5801`)
- `option_context` context manager now available as top-level API (:issue:`5752`)

Bug Fixes
~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
import pandas.core.datetools as datetools

from pandas.core.config import (get_option, set_option, reset_option,
describe_option, options)
describe_option, option_context, options)
53 changes: 14 additions & 39 deletions pandas/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,54 +100,29 @@ def _get_option(pat, silent=False):
return root[k]


def _set_single_option(pat, value, silent):
key = _get_single_key(pat, silent)

o = _get_registered_option(key)
if o and o.validator:
o.validator(value)

# walk the nested dict
root, k = _get_root(key)
root[k] = value

if o.cb:
o.cb(key)


def _set_multiple_options(args, silent):
for k, v in zip(args[::2], args[1::2]):
_set_single_option(k, v, silent)


def _set_option(*args, **kwargs):
# must at least 1 arg deal with constraints later
nargs = len(args)
if not nargs or nargs % 2 != 0:
raise AssertionError("Must provide an even number of non-keyword "
raise ValueError("Must provide an even number of non-keyword "
"arguments")

# must be 0 or 1 kwargs
nkwargs = len(kwargs)
if nkwargs not in (0, 1):
raise AssertionError("The can only be 0 or 1 keyword arguments")
# default to false
silent = kwargs.get('silent', False)

# if 1 kwarg then it must be silent=True or silent=False
if nkwargs:
k, = list(kwargs.keys())
v, = list(kwargs.values())
for k, v in zip(args[::2], args[1::2]):
key = _get_single_key(k, silent)

if k != 'silent':
raise ValueError("the only allowed keyword argument is 'silent', "
"you passed '{0}'".format(k))
if not isinstance(v, bool):
raise TypeError("the type of the keyword argument passed must be "
"bool, you passed a {0}".format(v.__class__))
o = _get_registered_option(key)
if o and o.validator:
o.validator(v)

# default to false
silent = kwargs.get('silent', False)
_set_multiple_options(args, silent)
# walk the nested dict
root, k = _get_root(key)
root[k] = v

if o.cb:
o.cb(key)

def _describe_option(pat='', _print_desc=True):

Expand Down Expand Up @@ -365,7 +340,7 @@ class option_context(object):

def __init__(self, *args):
if not (len(args) % 2 == 0 and len(args) >= 2):
raise AssertionError(
raise ValueError(
'Need to invoke as'
'option_context(pat, val, [(pat, val), ...)).'
)
Expand Down
19 changes: 3 additions & 16 deletions pandas/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,26 +170,13 @@ def test_set_option(self):


def test_set_option_empty_args(self):
self.assertRaises(AssertionError, self.cf.set_option)
self.assertRaises(ValueError, self.cf.set_option)

def test_set_option_uneven_args(self):
self.assertRaises(AssertionError, self.cf.set_option, 'a.b', 2, 'b.c')


def test_set_option_2_kwargs(self):
self.assertRaises(AssertionError, self.cf.set_option, 'a.b', 2,
silenadf=2, asdf=2)

def test_set_option_invalid_kwargs_key(self):
self.assertRaises(ValueError, self.cf.set_option, 'a.b', 2,
silenadf=2)

def test_set_option_invalid_kwargs_value_type(self):
self.assertRaises(TypeError, self.cf.set_option, 'a.b', 2,
silent=2)
self.assertRaises(ValueError, self.cf.set_option, 'a.b', 2, 'b.c')

def test_set_option_invalid_single_argument_type(self):
self.assertRaises(AssertionError, self.cf.set_option, 2)
self.assertRaises(ValueError, self.cf.set_option, 2)

def test_set_option_multiple(self):
self.cf.register_option('a', 1, 'doc')
Expand Down