25
25
- all options in a certain sub - namespace can be reset at once.
26
26
- the user can set / get / reset or ask for the description of an option.
27
27
- 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.
29
31
30
32
Implementation
31
33
==============
54
56
import warnings
55
57
56
58
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 ' )
58
60
59
61
_deprecated_options = {} # holds deprecated option metdata
60
62
_registered_options = {} # holds registered option metdata
@@ -105,6 +107,9 @@ def _set_option(pat, value):
105
107
root , k = _get_root (key )
106
108
root [k ] = value
107
109
110
+ if o and o .cb :
111
+ o .cb (key )
112
+
108
113
109
114
def _describe_option (pat = '' , _print_desc = True ):
110
115
@@ -270,7 +275,7 @@ def __doc__(self):
270
275
######################################################
271
276
# Functions for use by pandas developers, in addition to User - api
272
277
273
- def register_option (key , defval , doc = '' , validator = None ):
278
+ def register_option (key , defval , doc = '' , validator = None , cb = None ):
274
279
"""Register an option in the package-wide pandas config object
275
280
276
281
Parameters
@@ -280,6 +285,9 @@ def register_option(key, defval, doc='', validator=None):
280
285
doc - a string description of the option
281
286
validator - a function of a single argument, should raise `ValueError` if
282
287
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.
283
291
284
292
Returns
285
293
-------
@@ -321,7 +329,7 @@ def register_option(key, defval, doc='', validator=None):
321
329
322
330
# save the option metadata
323
331
_registered_options [key ] = RegisteredOption (key = key , defval = defval ,
324
- doc = doc , validator = validator )
332
+ doc = doc , validator = validator , cb = cb )
325
333
326
334
327
335
def deprecate_option (key , msg = None , rkey = None , removal_ver = None ):
0 commit comments