diff --git a/doc/source/basics.rst b/doc/source/basics.rst index 46745d94b5f78..adff5a3c74f90 100644 --- a/doc/source/basics.rst +++ b/doc/source/basics.rst @@ -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 ------------------------- diff --git a/doc/source/release.rst b/doc/source/release.rst index 7109b87f5352b..d4c9fa07e546f 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -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 ~~~~~~~~~ diff --git a/pandas/core/api.py b/pandas/core/api.py index d75c075d22d7c..b36c9f7499df6 100644 --- a/pandas/core/api.py +++ b/pandas/core/api.py @@ -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) diff --git a/pandas/core/config.py b/pandas/core/config.py index 4bec029851092..f2f932e39759a 100644 --- a/pandas/core/config.py +++ b/pandas/core/config.py @@ -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): @@ -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), ...)).' ) diff --git a/pandas/tests/test_config.py b/pandas/tests/test_config.py index 80a3fe9be7003..6d4486525f4eb 100644 --- a/pandas/tests/test_config.py +++ b/pandas/tests/test_config.py @@ -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')