65
65
_reserved_keys = ['all' ] # keys which have a special meaning
66
66
67
67
68
+ class OptionError (AttributeError , KeyError ):
69
+ """Exception for pandas.options, backwards compatible with KeyError
70
+ checks"""
71
+
72
+
68
73
##########################################
69
74
# User API
70
75
@@ -73,9 +78,9 @@ def _get_single_key(pat, silent):
73
78
if len (keys ) == 0 :
74
79
if not silent :
75
80
_warn_if_deprecated (pat )
76
- raise KeyError ('No such keys(s): %r' % pat )
81
+ raise OptionError ('No such keys(s): %r' % pat )
77
82
if len (keys ) > 1 :
78
- raise KeyError ('Pattern matched multiple keys' )
83
+ raise OptionError ('Pattern matched multiple keys' )
79
84
key = keys [0 ]
80
85
81
86
if not silent :
@@ -147,7 +152,7 @@ def _describe_option(pat='', _print_desc=True):
147
152
148
153
keys = _select_options (pat )
149
154
if len (keys ) == 0 :
150
- raise KeyError ('No such keys(s)' )
155
+ raise OptionError ('No such keys(s)' )
151
156
152
157
s = u ('' )
153
158
for k in keys : # filter by pat
@@ -164,7 +169,7 @@ def _reset_option(pat):
164
169
keys = _select_options (pat )
165
170
166
171
if len (keys ) == 0 :
167
- raise KeyError ('No such keys(s)' )
172
+ raise OptionError ('No such keys(s)' )
168
173
169
174
if len (keys ) > 1 and len (pat ) < 4 and pat != 'all' :
170
175
raise ValueError ('You must specify at least 4 characters when '
@@ -195,7 +200,7 @@ def __setattr__(self, key, val):
195
200
if key in self .d and not isinstance (self .d [key ], dict ):
196
201
_set_option (prefix , val )
197
202
else :
198
- raise KeyError ("You can only set the value of existing options" )
203
+ raise OptionError ("You can only set the value of existing options" )
199
204
200
205
def __getattr__ (self , key ):
201
206
prefix = object .__getattribute__ (self , "prefix" )
@@ -211,6 +216,7 @@ def __getattr__(self, key):
211
216
def __dir__ (self ):
212
217
return list (self .d .keys ())
213
218
219
+
214
220
# For user convenience, we'd like to have the available options described
215
221
# in the docstring. For dev convenience we'd like to generate the docstrings
216
222
# dynamically instead of maintaining them by hand. To this, we use the
@@ -255,7 +261,7 @@ def __doc__(self):
255
261
256
262
Raises
257
263
------
258
- KeyError if no such option exists
264
+ OptionError if no such option exists
259
265
260
266
{opts_desc}
261
267
"""
@@ -281,7 +287,7 @@ def __doc__(self):
281
287
282
288
Raises
283
289
------
284
- KeyError if no such option exists
290
+ OptionError if no such option exists
285
291
286
292
{opts_desc}
287
293
"""
@@ -398,9 +404,9 @@ def register_option(key, defval, doc='', validator=None, cb=None):
398
404
key = key .lower ()
399
405
400
406
if key in _registered_options :
401
- raise KeyError ("Option '%s' has already been registered" % key )
407
+ raise OptionError ("Option '%s' has already been registered" % key )
402
408
if key in _reserved_keys :
403
- raise KeyError ("Option '%s' is a reserved key" % key )
409
+ raise OptionError ("Option '%s' is a reserved key" % key )
404
410
405
411
# the default value should be legal
406
412
if validator :
@@ -418,14 +424,14 @@ def register_option(key, defval, doc='', validator=None, cb=None):
418
424
cursor = _global_config
419
425
for i , p in enumerate (path [:- 1 ]):
420
426
if not isinstance (cursor , dict ):
421
- raise KeyError ("Path prefix to option '%s' is already an option"
427
+ raise OptionError ("Path prefix to option '%s' is already an option"
422
428
% '.' .join (path [:i ]))
423
429
if p not in cursor :
424
430
cursor [p ] = {}
425
431
cursor = cursor [p ]
426
432
427
433
if not isinstance (cursor , dict ):
428
- raise KeyError ("Path prefix to option '%s' is already an option"
434
+ raise OptionError ("Path prefix to option '%s' is already an option"
429
435
% '.' .join (path [:- 1 ]))
430
436
431
437
cursor [path [- 1 ]] = defval # initialize
@@ -470,14 +476,14 @@ def deprecate_option(key, msg=None, rkey=None, removal_ver=None):
470
476
471
477
Raises
472
478
------
473
- KeyError - if key has already been deprecated.
479
+ OptionError - if key has already been deprecated.
474
480
475
481
"""
476
482
477
483
key = key .lower ()
478
484
479
485
if key in _deprecated_options :
480
- raise KeyError ("Option '%s' has already been defined as deprecated."
486
+ raise OptionError ("Option '%s' has already been defined as deprecated."
481
487
% key )
482
488
483
489
_deprecated_options [key ] = DeprecatedOption (key , msg , rkey , removal_ver )
0 commit comments