Skip to content

Commit 69f794f

Browse files
rockgwesm
authored andcommitted
PEP: pandas/core round 6 (config*, convert, datetools, strings, style)
Author: rockg <grant.roch@gmail.com> Closes #12095 from rockg/pep8-round6 and squashes the following commits: b03a39f [rockg] PEP: pandas/core round 6 (config*, convert, datetools, strings, style)
1 parent 06053e7 commit 69f794f

File tree

6 files changed

+178
-174
lines changed

6 files changed

+178
-174
lines changed

pandas/core/config.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@
5757
import pandas.compat as compat
5858

5959
DeprecatedOption = namedtuple('DeprecatedOption', 'key msg rkey removal_ver')
60-
RegisteredOption = namedtuple(
61-
'RegisteredOption', 'key defval doc validator cb')
60+
RegisteredOption = namedtuple('RegisteredOption',
61+
'key defval doc validator cb')
6262

6363
_deprecated_options = {} # holds deprecated option metdata
6464
_registered_options = {} # holds registered option metdata
@@ -67,14 +67,14 @@
6767

6868

6969
class OptionError(AttributeError, KeyError):
70-
7170
"""Exception for pandas.options, backwards compatible with KeyError
72-
checks"""
73-
71+
checks
72+
"""
7473

7574
#
7675
# User API
7776

77+
7878
def _get_single_key(pat, silent):
7979
keys = _select_options(pat)
8080
if len(keys) == 0:
@@ -106,14 +106,14 @@ def _set_option(*args, **kwargs):
106106
nargs = len(args)
107107
if not nargs or nargs % 2 != 0:
108108
raise ValueError("Must provide an even number of non-keyword "
109-
"arguments")
109+
"arguments")
110110

111111
# default to false
112112
silent = kwargs.pop('silent', False)
113113

114114
if kwargs:
115115
raise TypeError('_set_option() got an unexpected keyword '
116-
'argument "{0}"'.format(list(kwargs.keys())[0]))
116+
'argument "{0}"'.format(list(kwargs.keys())[0]))
117117

118118
for k, v in zip(args[::2], args[1::2]):
119119
key = _get_single_key(k, silent)
@@ -129,6 +129,7 @@ def _set_option(*args, **kwargs):
129129
if o.cb:
130130
o.cb(key)
131131

132+
132133
def _describe_option(pat='', _print_desc=True):
133134

134135
keys = _select_options(pat)
@@ -168,9 +169,7 @@ def get_default_val(pat):
168169

169170

170171
class DictWrapper(object):
171-
172-
""" provide attribute-style access to a nested dict
173-
"""
172+
""" provide attribute-style access to a nested dict"""
174173

175174
def __init__(self, d, prefix=""):
176175
object.__setattr__(self, "d", d)
@@ -202,7 +201,6 @@ def __getattr__(self, key):
202201
def __dir__(self):
203202
return list(self.d.keys())
204203

205-
206204
# For user convenience, we'd like to have the available options described
207205
# in the docstring. For dev convenience we'd like to generate the docstrings
208206
# dynamically instead of maintaining them by hand. To this, we use the
@@ -213,7 +211,6 @@ def __dir__(self):
213211

214212

215213
class CallableDynamicDoc(object):
216-
217214
def __init__(self, func, doc_tmpl):
218215
self.__doc_tmpl__ = doc_tmpl
219216
self.__func__ = func
@@ -228,6 +225,7 @@ def __doc__(self):
228225
return self.__doc_tmpl__.format(opts_desc=opts_desc,
229226
opts_list=opts_list)
230227

228+
231229
_get_option_tmpl = """
232230
get_option(pat)
233231
@@ -384,10 +382,8 @@ class option_context(object):
384382

385383
def __init__(self, *args):
386384
if not (len(args) % 2 == 0 and len(args) >= 2):
387-
raise ValueError(
388-
'Need to invoke as'
389-
'option_context(pat, val, [(pat, val), ...)).'
390-
)
385+
raise ValueError('Need to invoke as'
386+
'option_context(pat, val, [(pat, val), ...)).')
391387

392388
self.ops = list(zip(args[::2], args[1::2]))
393389

@@ -462,8 +458,8 @@ def register_option(key, defval, doc='', validator=None, cb=None):
462458
cursor = cursor[p]
463459

464460
if not isinstance(cursor, dict):
465-
raise OptionError("Path prefix to option '%s' is already an option"
466-
% '.'.join(path[:-1]))
461+
raise OptionError("Path prefix to option '%s' is already an option" %
462+
'.'.join(path[:-1]))
467463

468464
cursor[path[-1]] = defval # initialize
469465

@@ -520,10 +516,10 @@ def deprecate_option(key, msg=None, rkey=None, removal_ver=None):
520516

521517
_deprecated_options[key] = DeprecatedOption(key, msg, rkey, removal_ver)
522518

523-
524519
#
525520
# functions internal to the module
526521

522+
527523
def _select_options(pat):
528524
"""returns a list of keys matching `pat`
529525
@@ -681,7 +677,6 @@ def pp(name, ks):
681677
else:
682678
return s
683679

684-
685680
#
686681
# helpers
687682

@@ -717,7 +712,6 @@ def config_prefix(prefix):
717712
global register_option, get_option, set_option, reset_option
718713

719714
def wrap(func):
720-
721715
def inner(key, *args, **kwds):
722716
pkey = '%s.%s' % (prefix, key)
723717
return func(pkey, *args, **kwds)
@@ -735,10 +729,10 @@ def inner(key, *args, **kwds):
735729
get_option = _get_option
736730
register_option = _register_option
737731

738-
739732
# These factories and methods are handy for use as the validator
740733
# arg in register_option
741734

735+
742736
def is_type_factory(_type):
743737
"""
744738
@@ -790,10 +784,10 @@ def inner(x):
790784
def is_one_of_factory(legal_values):
791785
def inner(x):
792786
from pandas.core.common import pprint_thing as pp
793-
if not x in legal_values:
787+
if x not in legal_values:
794788
pp_values = lmap(pp, legal_values)
795-
raise ValueError("Value must be one of %s"
796-
% pp("|".join(pp_values)))
789+
raise ValueError("Value must be one of %s" %
790+
pp("|".join(pp_values)))
797791

798792
return inner
799793

pandas/core/config_init.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@
1111
"""
1212

1313
import pandas.core.config as cf
14-
from pandas.core.config import (is_int, is_bool, is_text, is_float,
15-
is_instance_factory, is_one_of_factory,
16-
get_default_val)
14+
from pandas.core.config import (is_int, is_bool, is_text, is_instance_factory,
15+
is_one_of_factory, get_default_val)
1716
from pandas.core.format import detect_console_encoding
1817

19-
2018
#
2119
# options from the "display" namespace
2220

@@ -61,8 +59,8 @@
6159

6260
pc_max_categories_doc = """
6361
: int
64-
This sets the maximum number of categories pandas should output when printing
65-
out a `Categorical` or a Series of dtype "category".
62+
This sets the maximum number of categories pandas should output when
63+
printing out a `Categorical` or a Series of dtype "category".
6664
"""
6765

6866
pc_max_info_cols_doc = """
@@ -146,9 +144,11 @@
146144

147145
pc_east_asian_width_doc = """
148146
: boolean
149-
Whether to use the Unicode East Asian Width to calculate the display text width
147+
Whether to use the Unicode East Asian Width to calculate the display text
148+
width.
150149
Enabling this may affect to the performance (default: False)
151150
"""
151+
152152
pc_ambiguous_as_wide_doc = """
153153
: boolean
154154
Whether to handle Unicode characters belong to Ambiguous as Wide (width=2)
@@ -197,7 +197,8 @@
197197
: int or None
198198
df.info() will usually show null-counts for each column.
199199
For large frames this can be quite slow. max_info_rows and max_info_cols
200-
limit this null check only to frames with smaller dimensions then specified.
200+
limit this null check only to frames with smaller dimensions than
201+
specified.
201202
"""
202203

203204
pc_large_repr_doc = """
@@ -222,15 +223,16 @@
222223

223224
pc_latex_escape = """
224225
: bool
225-
This specifies if the to_latex method of a Dataframe uses escapes special
226+
This specifies if the to_latex method of a Dataframe uses escapes special
226227
characters.
227-
method. Valid values: False,True
228+
method. Valid values: False,True
228229
"""
229230

230231
pc_latex_longtable = """
231232
:bool
232-
This specifies if the to_latex method of a Dataframe uses the longtable format.
233-
method. Valid values: False,True
233+
This specifies if the to_latex method of a Dataframe uses the longtable
234+
format.
235+
method. Valid values: False,True
234236
"""
235237

236238
style_backup = dict()
@@ -244,7 +246,7 @@ def mpl_style_cb(key):
244246
val = cf.get_option(key)
245247

246248
if 'matplotlib' not in sys.modules.keys():
247-
if not(val): # starting up, we get reset to None
249+
if not val: # starting up, we get reset to None
248250
return val
249251
raise Exception("matplotlib has not been imported. aborting")
250252

@@ -267,7 +269,8 @@ def mpl_style_cb(key):
267269
validator=is_instance_factory((int, type(None))))
268270
cf.register_option('max_rows', 60, pc_max_rows_doc,
269271
validator=is_instance_factory([type(None), int]))
270-
cf.register_option('max_categories', 8, pc_max_categories_doc, validator=is_int)
272+
cf.register_option('max_categories', 8, pc_max_categories_doc,
273+
validator=is_int)
271274
cf.register_option('max_colwidth', 50, max_colwidth_doc, validator=is_int)
272275
cf.register_option('max_columns', 20, pc_max_cols_doc,
273276
validator=is_instance_factory([type(None), int]))
@@ -305,28 +308,29 @@ def mpl_style_cb(key):
305308
cf.register_option('line_width', get_default_val('display.width'),
306309
pc_line_width_doc)
307310
cf.register_option('memory_usage', True, pc_memory_usage_doc,
308-
validator=is_one_of_factory([None, True, False, 'deep']))
311+
validator=is_one_of_factory([None, True,
312+
False, 'deep']))
309313
cf.register_option('unicode.east_asian_width', False,
310314
pc_east_asian_width_doc, validator=is_bool)
311315
cf.register_option('unicode.ambiguous_as_wide', False,
312316
pc_east_asian_width_doc, validator=is_bool)
313-
cf.register_option('latex.escape',True, pc_latex_escape,
314-
validator=is_bool)
315-
cf.register_option('latex.longtable',False,pc_latex_longtable,
316-
validator=is_bool)
317+
cf.register_option('latex.escape', True, pc_latex_escape,
318+
validator=is_bool)
319+
cf.register_option('latex.longtable', False, pc_latex_longtable,
320+
validator=is_bool)
317321

318322
cf.deprecate_option('display.line_width',
319323
msg=pc_line_width_deprecation_warning,
320324
rkey='display.width')
321325

322-
cf.deprecate_option('display.height',
323-
msg=pc_height_deprecation_warning,
326+
cf.deprecate_option('display.height', msg=pc_height_deprecation_warning,
324327
rkey='display.max_rows')
325328

326329
tc_sim_interactive_doc = """
327330
: boolean
328331
Whether to simulate interactive mode for purposes of testing
329332
"""
333+
330334
with cf.config_prefix('mode'):
331335
cf.register_option('sim_interactive', False, tc_sim_interactive_doc)
332336

@@ -349,7 +353,6 @@ def use_inf_as_null_cb(key):
349353
cf.register_option('use_inf_as_null', False, use_inf_as_null_doc,
350354
cb=use_inf_as_null_cb)
351355

352-
353356
# user warnings
354357
chained_assignment = """
355358
: string
@@ -361,7 +364,6 @@ def use_inf_as_null_cb(key):
361364
cf.register_option('chained_assignment', 'warn', chained_assignment,
362365
validator=is_one_of_factory([None, 'warn', 'raise']))
363366

364-
365367
# Set up the io.excel specific configuration.
366368
writer_engine_doc = """
367369
: string
@@ -371,8 +373,7 @@ def use_inf_as_null_cb(key):
371373

372374
with cf.config_prefix('io.excel'):
373375
# going forward, will be additional writers
374-
for ext, options in [('xls', ['xlwt']),
375-
('xlsm', ['openpyxl'])]:
376+
for ext, options in [('xls', ['xlwt']), ('xlsm', ['openpyxl'])]:
376377
default = options.pop(0)
377378
if options:
378379
options = " " + ", ".join(options)
@@ -384,14 +385,13 @@ def use_inf_as_null_cb(key):
384385

385386
def _register_xlsx(engine, other):
386387
cf.register_option('xlsx.writer', engine,
387-
writer_engine_doc.format(ext='xlsx',
388-
default=engine,
388+
writer_engine_doc.format(ext='xlsx', default=engine,
389389
others=", '%s'" % other),
390390
validator=str)
391391

392392
try:
393393
# better memory footprint
394-
import xlsxwriter
394+
import xlsxwriter # noqa
395395
_register_xlsx('xlsxwriter', 'openpyxl')
396396
except ImportError:
397397
# fallback

0 commit comments

Comments
 (0)