Skip to content

Commit 1ebb31e

Browse files
author
y-p
committed
Merge pull request #5752 from y-p/PR_expose_option_context
Make set_option into context manager
2 parents b3f6972 + 1628bfb commit 1ebb31e

File tree

5 files changed

+34
-56
lines changed

5 files changed

+34
-56
lines changed

doc/source/basics.rst

+15
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,21 @@ It's also possible to reset multiple options at once (using a regex):
14571457
reset_option("^display")
14581458
14591459
1460+
.. versionadded:: 0.14.0
1461+
1462+
Beginning with v0.14.0 the `option_context` context manager has been exposed through
1463+
the top-level API, allowing you to execute code with given option values. Option values
1464+
are restored automatically when you exit the `with` block:
1465+
1466+
.. ipython:: python
1467+
1468+
with option_context("display.max_rows",10,"display.max_columns", 5):
1469+
print get_option("display.max_rows")
1470+
print get_option("display.max_columns")
1471+
1472+
print get_option("display.max_rows")
1473+
print get_option("display.max_columns")
1474+
14601475
14611476
Console Output Formatting
14621477
-------------------------

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Improvements to existing features
6565
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6666

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

6970
Bug Fixes
7071
~~~~~~~~~

pandas/core/api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
import pandas.core.datetools as datetools
3232

3333
from pandas.core.config import (get_option, set_option, reset_option,
34-
describe_option, options)
34+
describe_option, option_context, options)

pandas/core/config.py

+14-39
Original file line numberDiff line numberDiff line change
@@ -100,54 +100,29 @@ def _get_option(pat, silent=False):
100100
return root[k]
101101

102102

103-
def _set_single_option(pat, value, silent):
104-
key = _get_single_key(pat, silent)
105-
106-
o = _get_registered_option(key)
107-
if o and o.validator:
108-
o.validator(value)
109-
110-
# walk the nested dict
111-
root, k = _get_root(key)
112-
root[k] = value
113-
114-
if o.cb:
115-
o.cb(key)
116-
117-
118-
def _set_multiple_options(args, silent):
119-
for k, v in zip(args[::2], args[1::2]):
120-
_set_single_option(k, v, silent)
121-
122-
123103
def _set_option(*args, **kwargs):
124104
# must at least 1 arg deal with constraints later
125105
nargs = len(args)
126106
if not nargs or nargs % 2 != 0:
127-
raise AssertionError("Must provide an even number of non-keyword "
107+
raise ValueError("Must provide an even number of non-keyword "
128108
"arguments")
129109

130-
# must be 0 or 1 kwargs
131-
nkwargs = len(kwargs)
132-
if nkwargs not in (0, 1):
133-
raise AssertionError("The can only be 0 or 1 keyword arguments")
110+
# default to false
111+
silent = kwargs.get('silent', False)
134112

135-
# if 1 kwarg then it must be silent=True or silent=False
136-
if nkwargs:
137-
k, = list(kwargs.keys())
138-
v, = list(kwargs.values())
113+
for k, v in zip(args[::2], args[1::2]):
114+
key = _get_single_key(k, silent)
139115

140-
if k != 'silent':
141-
raise ValueError("the only allowed keyword argument is 'silent', "
142-
"you passed '{0}'".format(k))
143-
if not isinstance(v, bool):
144-
raise TypeError("the type of the keyword argument passed must be "
145-
"bool, you passed a {0}".format(v.__class__))
116+
o = _get_registered_option(key)
117+
if o and o.validator:
118+
o.validator(v)
146119

147-
# default to false
148-
silent = kwargs.get('silent', False)
149-
_set_multiple_options(args, silent)
120+
# walk the nested dict
121+
root, k = _get_root(key)
122+
root[k] = v
150123

124+
if o.cb:
125+
o.cb(key)
151126

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

@@ -365,7 +340,7 @@ class option_context(object):
365340

366341
def __init__(self, *args):
367342
if not (len(args) % 2 == 0 and len(args) >= 2):
368-
raise AssertionError(
343+
raise ValueError(
369344
'Need to invoke as'
370345
'option_context(pat, val, [(pat, val), ...)).'
371346
)

pandas/tests/test_config.py

+3-16
Original file line numberDiff line numberDiff line change
@@ -170,26 +170,13 @@ def test_set_option(self):
170170

171171

172172
def test_set_option_empty_args(self):
173-
self.assertRaises(AssertionError, self.cf.set_option)
173+
self.assertRaises(ValueError, self.cf.set_option)
174174

175175
def test_set_option_uneven_args(self):
176-
self.assertRaises(AssertionError, self.cf.set_option, 'a.b', 2, 'b.c')
177-
178-
179-
def test_set_option_2_kwargs(self):
180-
self.assertRaises(AssertionError, self.cf.set_option, 'a.b', 2,
181-
silenadf=2, asdf=2)
182-
183-
def test_set_option_invalid_kwargs_key(self):
184-
self.assertRaises(ValueError, self.cf.set_option, 'a.b', 2,
185-
silenadf=2)
186-
187-
def test_set_option_invalid_kwargs_value_type(self):
188-
self.assertRaises(TypeError, self.cf.set_option, 'a.b', 2,
189-
silent=2)
176+
self.assertRaises(ValueError, self.cf.set_option, 'a.b', 2, 'b.c')
190177

191178
def test_set_option_invalid_single_argument_type(self):
192-
self.assertRaises(AssertionError, self.cf.set_option, 2)
179+
self.assertRaises(ValueError, self.cf.set_option, 2)
193180

194181
def test_set_option_multiple(self):
195182
self.cf.register_option('a', 1, 'doc')

0 commit comments

Comments
 (0)