Skip to content

Commit 87126b9

Browse files
author
y-p
committed
BUG: make sure pd.options style access triggers warnings and callbacks as needed
1 parent cc2a22a commit 87126b9

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

pandas/core/config.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,33 @@ def _reset_option(pat):
141141
class DictWrapper(object):
142142
""" provide attribute-style access to a nested dict
143143
"""
144-
def __init__(self,d):
144+
def __init__(self,d,prefix=""):
145145
object.__setattr__(self,"d",d)
146+
object.__setattr__(self,"prefix",prefix)
146147

147148
def __setattr__(self,key,val):
149+
prefix = object.__getattribute__(self,"prefix")
150+
if prefix:
151+
prefix += "."
152+
prefix += key
148153
# you can't set new keys
149154
# can you can't overwrite subtrees
150155
if key in self.d and not isinstance(self.d[key],dict):
156+
_set_option(prefix,val)
151157
self.d[key]=val
152158
else:
153159
raise KeyError("You can only set the value of existing options")
154160

155161
def __getattr__(self,key):
162+
prefix = object.__getattribute__(self,"prefix")
163+
if prefix:
164+
prefix += "."
165+
prefix += key
156166
v=object.__getattribute__(self,"d")[key]
157167
if isinstance(v,dict):
158-
return DictWrapper(v)
168+
return DictWrapper(v,prefix)
159169
else:
160-
return v
170+
return _get_option(prefix)
161171

162172
def __dir__(self):
163173
return self.d.keys()
@@ -366,6 +376,7 @@ def register_option(key, defval, doc='', validator=None, cb=None):
366376
raise KeyError("Path prefix to option '%s' is already an option"
367377
% '.'.join(path[:-1]))
368378

379+
369380
cursor[path[-1]] = defval # initialize
370381

371382
# save the option metadata

pandas/tests/test_config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,16 @@ def eq(val):
325325
eq(17)
326326

327327
def test_attribute_access(self):
328+
holder = []
328329
def f():
329330
options.b=1
330331
def f2():
331332
options.display=1
333+
def f3(key):
334+
holder.append(True)
335+
332336
self.cf.register_option('a',0)
337+
self.cf.register_option('c',0,cb=f3)
333338
options=self.cf.options
334339

335340
self.assertEqual(options.a,0)
@@ -345,5 +350,9 @@ def f2():
345350
self.assertRaises(KeyError,f)
346351
self.assertRaises(KeyError,f2)
347352

353+
# make sure callback kicks when using this form of setting
354+
options.c = 1
355+
self.assertEqual(len(holder),1)
356+
348357
# fmt.reset_printoptions and fmt.set_printoptions were altered
349358
# to use core.config, test_format exercises those paths.

0 commit comments

Comments
 (0)