Skip to content

Commit 36130c0

Browse files
author
y-p
committed
CLN: simplify code in config.py
1 parent 011e2c5 commit 36130c0

File tree

2 files changed

+12
-50
lines changed

2 files changed

+12
-50
lines changed

pandas/core/config.py

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -100,54 +100,29 @@ def _get_option(pat, silent=False):
100100
return root[k]
101101

102102

103-
def _set_single_option(pat, value, silent):
104-
key = _get_single_key(pat, silent)
105-
106-
o = _get_registered_option(key)
107-
if o and o.validator:
108-
o.validator(value)
109-
110-
# walk the nested dict
111-
root, k = _get_root(key)
112-
root[k] = value
113-
114-
if o.cb:
115-
o.cb(key)
116-
117-
118-
def _set_multiple_options(args, silent):
119-
for k, v in zip(args[::2], args[1::2]):
120-
_set_single_option(k, v, silent)
121-
122-
123103
def _set_option(*args, **kwargs):
124104
# must at least 1 arg deal with constraints later
125105
nargs = len(args)
126106
if not nargs or nargs % 2 != 0:
127107
raise ValueError("Must provide an even number of non-keyword "
128108
"arguments")
129109

130-
# must be 0 or 1 kwargs
131-
nkwargs = len(kwargs)
132-
if nkwargs not in (0, 1):
133-
raise ValueError("The can only be 0 or 1 keyword arguments")
110+
# default to false
111+
silent = kwargs.get('silent', False)
134112

135-
# if 1 kwarg then it must be silent=True or silent=False
136-
if nkwargs:
137-
k, = list(kwargs.keys())
138-
v, = list(kwargs.values())
113+
for k, v in zip(args[::2], args[1::2]):
114+
key = _get_single_key(k, silent)
139115

140-
if k != 'silent':
141-
raise ValueError("the only allowed keyword argument is 'silent', "
142-
"you passed '{0}'".format(k))
143-
if not isinstance(v, bool):
144-
raise TypeError("the type of the keyword argument passed must be "
145-
"bool, you passed a {0}".format(v.__class__))
116+
o = _get_registered_option(key)
117+
if o and o.validator:
118+
o.validator(v)
146119

147-
# default to false
148-
silent = kwargs.get('silent', False)
149-
_set_multiple_options(args, silent)
120+
# walk the nested dict
121+
root, k = _get_root(key)
122+
root[k] = v
150123

124+
if o.cb:
125+
o.cb(key)
151126

152127
def _describe_option(pat='', _print_desc=True):
153128

pandas/tests/test_config.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -175,19 +175,6 @@ def test_set_option_empty_args(self):
175175
def test_set_option_uneven_args(self):
176176
self.assertRaises(ValueError, self.cf.set_option, 'a.b', 2, 'b.c')
177177

178-
179-
def test_set_option_2_kwargs(self):
180-
self.assertRaises(ValueError, self.cf.set_option, 'a.b', 2,
181-
silenadf=2, asdf=2)
182-
183-
def test_set_option_invalid_kwargs_key(self):
184-
self.assertRaises(ValueError, self.cf.set_option, 'a.b', 2,
185-
silenadf=2)
186-
187-
def test_set_option_invalid_kwargs_value_type(self):
188-
self.assertRaises(TypeError, self.cf.set_option, 'a.b', 2,
189-
silent=2)
190-
191178
def test_set_option_invalid_single_argument_type(self):
192179
self.assertRaises(ValueError, self.cf.set_option, 2)
193180

0 commit comments

Comments
 (0)