Skip to content

Commit b15c61b

Browse files
author
y-p
committed
ENH: Support callback functions to be called on config.option set/reset
1 parent 860c56d commit b15c61b

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

pandas/core/config.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
- all options in a certain sub - namespace can be reset at once.
2626
- the user can set / get / reset or ask for the description of an option.
2727
- a developer can register and mark an option as deprecated.
28-
28+
- you can register a callback to be invoked when the the option value
29+
is set or reset. Changing the stored value is considered misuse, but
30+
is not verboten.
2931
3032
Implementation
3133
==============
@@ -54,7 +56,7 @@
5456
import warnings
5557

5658
DeprecatedOption = namedtuple('DeprecatedOption', 'key msg rkey removal_ver')
57-
RegisteredOption = namedtuple('RegisteredOption', 'key defval doc validator')
59+
RegisteredOption = namedtuple('RegisteredOption', 'key defval doc validator cb')
5860

5961
_deprecated_options = {} # holds deprecated option metdata
6062
_registered_options = {} # holds registered option metdata
@@ -105,6 +107,9 @@ def _set_option(pat, value):
105107
root, k = _get_root(key)
106108
root[k] = value
107109

110+
if o and o.cb:
111+
o.cb(key)
112+
108113

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

@@ -270,7 +275,7 @@ def __doc__(self):
270275
######################################################
271276
# Functions for use by pandas developers, in addition to User - api
272277

273-
def register_option(key, defval, doc='', validator=None):
278+
def register_option(key, defval, doc='', validator=None, cb=None):
274279
"""Register an option in the package-wide pandas config object
275280
276281
Parameters
@@ -280,6 +285,9 @@ def register_option(key, defval, doc='', validator=None):
280285
doc - a string description of the option
281286
validator - a function of a single argument, should raise `ValueError` if
282287
called with a value which is not a legal value for the option.
288+
cb - a function of a single argument "key", which is called
289+
immediately after an option value is set/reset. key is
290+
the full name of the option.
283291
284292
Returns
285293
-------
@@ -321,7 +329,7 @@ def register_option(key, defval, doc='', validator=None):
321329

322330
# save the option metadata
323331
_registered_options[key] = RegisteredOption(key=key, defval=defval,
324-
doc=doc, validator=validator)
332+
doc=doc, validator=validator,cb=cb)
325333

326334

327335
def deprecate_option(key, msg=None, rkey=None, removal_ver=None):

pandas/tests/test_config.py

+25
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,31 @@ def test_config_prefix(self):
282282
self.assertEqual(self.cf.get_option('a'), 1)
283283
self.assertEqual(self.cf.get_option('b'), 2)
284284

285+
def test_callback(self):
286+
k=[None]
287+
v=[None]
288+
def callback(key):
289+
k.append(key)
290+
v.append(self.cf.get_option(key))
291+
292+
self.cf.register_option('d.a', 'foo',cb=callback)
293+
self.cf.register_option('d.b', 'foo',cb=callback)
294+
295+
del k[-1],v[-1]
296+
self.cf.set_option("d.a","fooz")
297+
self.assertEqual(k[-1],"d.a")
298+
self.assertEqual(v[-1],"fooz")
299+
300+
del k[-1],v[-1]
301+
self.cf.set_option("d.b","boo")
302+
self.assertEqual(k[-1],"d.b")
303+
self.assertEqual(v[-1],"boo")
304+
305+
del k[-1],v[-1]
306+
self.cf.reset_option("d.b")
307+
self.assertEqual(k[-1],"d.b")
308+
309+
285310

286311
# fmt.reset_printoptions and fmt.set_printoptions were altered
287312
# to use core.config, test_format exercises those paths.

0 commit comments

Comments
 (0)