From 011e2c5ef62c9880ce99ec4e141b8c8dab57b1cc Mon Sep 17 00:00:00 2001 From: y-p Date: Thu, 19 Dec 2013 23:01:12 +0200 Subject: [PATCH 1/3] CLN: Use correct exception types in config.py machinery --- pandas/core/config.py | 6 +++--- pandas/tests/test_config.py | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pandas/core/config.py b/pandas/core/config.py index 4bec029851092..96d89e9603fd4 100644 --- a/pandas/core/config.py +++ b/pandas/core/config.py @@ -124,13 +124,13 @@ 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") + raise ValueError("The can only be 0 or 1 keyword arguments") # if 1 kwarg then it must be silent=True or silent=False if nkwargs: @@ -365,7 +365,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..2c00a0928731a 100644 --- a/pandas/tests/test_config.py +++ b/pandas/tests/test_config.py @@ -170,14 +170,14 @@ 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') + self.assertRaises(ValueError, 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, + self.assertRaises(ValueError, self.cf.set_option, 'a.b', 2, silenadf=2, asdf=2) def test_set_option_invalid_kwargs_key(self): @@ -189,7 +189,7 @@ def test_set_option_invalid_kwargs_value_type(self): silent=2) 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') From 36130c0c5375d827c7a0a45397d14b72d798d307 Mon Sep 17 00:00:00 2001 From: y-p Date: Thu, 19 Dec 2013 23:38:41 +0200 Subject: [PATCH 2/3] CLN: simplify code in config.py --- pandas/core/config.py | 49 +++++++++---------------------------- pandas/tests/test_config.py | 13 ---------- 2 files changed, 12 insertions(+), 50 deletions(-) diff --git a/pandas/core/config.py b/pandas/core/config.py index 96d89e9603fd4..f2f932e39759a 100644 --- a/pandas/core/config.py +++ b/pandas/core/config.py @@ -100,26 +100,6 @@ 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) @@ -127,27 +107,22 @@ def _set_option(*args, **kwargs): 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 ValueError("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): diff --git a/pandas/tests/test_config.py b/pandas/tests/test_config.py index 2c00a0928731a..6d4486525f4eb 100644 --- a/pandas/tests/test_config.py +++ b/pandas/tests/test_config.py @@ -175,19 +175,6 @@ def test_set_option_empty_args(self): def test_set_option_uneven_args(self): self.assertRaises(ValueError, self.cf.set_option, 'a.b', 2, 'b.c') - - def test_set_option_2_kwargs(self): - self.assertRaises(ValueError, 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) - def test_set_option_invalid_single_argument_type(self): self.assertRaises(ValueError, self.cf.set_option, 2) From 1628bfb1d56cea69d5f8a902f0ec1c3d91405b46 Mon Sep 17 00:00:00 2001 From: y-p Date: Thu, 19 Dec 2013 23:49:12 +0200 Subject: [PATCH 3/3] ENH: expose option_context as a top-level API GH5618 --- doc/source/basics.rst | 15 +++++++++++++++ doc/source/release.rst | 1 + pandas/core/api.py | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) 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)