Skip to content

Commit cc2a22a

Browse files
author
y-p
committed
ENH: provide attribute style access to options via pd.options
1 parent c010904 commit cc2a22a

File tree

5 files changed

+64
-6
lines changed

5 files changed

+64
-6
lines changed

RELEASE.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ pandas 0.10.0
7373
backfilling time series data (GH2284_)
7474
- New option configuration system and functions `set_option`, `get_option`,
7575
`describe_option`, and `reset_option`. Deprecate `set_printoptions` and
76-
`reset_printoptions` (GH2393_)
76+
`reset_printoptions` (GH2393_).
77+
You can also access options as attributes via ``pandas.options.X``
7778
- Wide DataFrames can be viewed more easily in the console with new
7879
`expand_frame_repr` and `line_width` configuration options. This is on by
7980
default now (GH2436_)

doc/source/basics.rst

+15-3
Original file line numberDiff line numberDiff line change
@@ -1049,16 +1049,28 @@ Working with package options
10491049
.. _basics.working_with_options:
10501050

10511051
Introduced in 0.10.0, pandas supports a new system for working with options.
1052-
The 4 relavent functions are available directly from the ``pandas`` namespace,
1053-
and they are:
1052+
Options have a full "dotted-style", case-insensitive name (e.g. ``display.max_rows``),
1053+
1054+
You can get/set options directly as attributes of the top-level ``options`` attribute:
1055+
1056+
.. ipython:: python
1057+
1058+
import pandas as pd
1059+
pd.options.display.max_rows
1060+
pd.options.display.max_rows = 999
1061+
pd.options.display.max_rows
1062+
1063+
1064+
There is also an API composed of 4 relavent functions, available directly from the ``pandas``
1065+
namespace, and they are:
10541066

10551067
- ``get_option`` / ``set_option`` - get/set the value of a single option.
10561068
- ``reset_option`` - reset one or more options to their default value.
10571069
- ``describe_option`` - print the descriptions of one or more options.
10581070

10591071
**Note:** developers can check out pandas/core/config.py for more info.
10601072

1061-
Options have a full "dotted-style", case-insensitive name (e.g. ``display.max_rows``),
1073+
10621074
but all of the functions above accept a regexp pattern (``re.search`` style) as argument,
10631075
so passing in a substring will work - as long as it is unambiguous :
10641076

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
34+
describe_option, options

pandas/core/config.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,29 @@ def _reset_option(pat):
138138
for k in keys:
139139
_set_option(k, _registered_options[k].defval)
140140

141+
class DictWrapper(object):
142+
""" provide attribute-style access to a nested dict
143+
"""
144+
def __init__(self,d):
145+
object.__setattr__(self,"d",d)
146+
147+
def __setattr__(self,key,val):
148+
# you can't set new keys
149+
# can you can't overwrite subtrees
150+
if key in self.d and not isinstance(self.d[key],dict):
151+
self.d[key]=val
152+
else:
153+
raise KeyError("You can only set the value of existing options")
154+
155+
def __getattr__(self,key):
156+
v=object.__getattribute__(self,"d")[key]
157+
if isinstance(v,dict):
158+
return DictWrapper(v)
159+
else:
160+
return v
161+
162+
def __dir__(self):
163+
return self.d.keys()
141164

142165
# For user convenience, we'd like to have the available options described
143166
# in the docstring. For dev convenience we'd like to generate the docstrings
@@ -266,7 +289,7 @@ def __doc__(self):
266289
set_option = CallableDyanmicDoc(_set_option, _set_option_tmpl)
267290
reset_option = CallableDyanmicDoc(_reset_option, _reset_option_tmpl)
268291
describe_option = CallableDyanmicDoc(_describe_option, _describe_option_tmpl)
269-
292+
options = DictWrapper(_global_config)
270293

271294
######################################################
272295
# Functions for use by pandas developers, in addition to User - api

pandas/tests/test_config.py

+22
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def __init__(self,*args):
1919

2020
def setUp(self):
2121
setattr(self.cf, '_global_config', {})
22+
setattr(self.cf, 'options', self.cf.DictWrapper(self.cf._global_config))
2223
setattr(self.cf, '_deprecated_options', {})
2324
setattr(self.cf, '_registered_options', {})
2425

@@ -323,5 +324,26 @@ def eq(val):
323324
self.cf.set_option("a",17)
324325
eq(17)
325326

327+
def test_attribute_access(self):
328+
def f():
329+
options.b=1
330+
def f2():
331+
options.display=1
332+
self.cf.register_option('a',0)
333+
options=self.cf.options
334+
335+
self.assertEqual(options.a,0)
336+
with self.cf.option_context("a",15):
337+
self.assertEqual(options.a,15)
338+
339+
options.a=500
340+
self.assertEqual(self.cf.get_option("a"),500)
341+
342+
self.cf.reset_option("a")
343+
self.assertEqual(options.a, self.cf.get_option("a",0))
344+
345+
self.assertRaises(KeyError,f)
346+
self.assertRaises(KeyError,f2)
347+
326348
# fmt.reset_printoptions and fmt.set_printoptions were altered
327349
# to use core.config, test_format exercises those paths.

0 commit comments

Comments
 (0)