Skip to content

Commit 0452566

Browse files
committed
fix flake8 warnings or ignore them locally (per line) instead of ignoring them per file
simplified code in a few FileHandler.list_items() methods in the process
1 parent 0e466d4 commit 0452566

File tree

12 files changed

+177
-188
lines changed

12 files changed

+177
-188
lines changed

larray/__init__.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@
3131
from larray.inout.xw_excel import open_excel, Workbook
3232
from larray.inout.xw_reporting import ExcelReport, ReportSheet
3333

34-
# just make sure handlers for .pkl and .pickle are initialized
34+
# module is imported only to initialize handlers for .pkl and .pickle (we del _pkl at the end of this module)
3535
import larray.inout.pickle as _pkl
36-
del _pkl
3736

3837
from larray.util.options import get_options, set_options
3938

@@ -43,8 +42,6 @@
4342

4443
from larray.example import get_example_filepath, load_example_data, EXAMPLE_EXCEL_TEMPLATES_DIR
4544

46-
import larray.random
47-
4845

4946
__all__ = [
5047
# axis
@@ -92,13 +89,13 @@
9289

9390
# ==== DEPRECATED API ====
9491

95-
from larray.core.axis import x
96-
from larray.core.group import PGroup
97-
from larray.core.array import (LArray, aslarray, create_sequential, ndrange, larray_equal,
92+
# F401: imported item not used
93+
from larray.core.axis import x # noqa: F401
94+
from larray.core.group import PGroup # noqa: F401
95+
from larray.core.array import (LArray, aslarray, create_sequential, ndrange, larray_equal, # noqa: F401
9896
larray_nan_equal, nan_equal, element_equal)
9997

100-
101-
_deprecated = [
98+
_deprecated_names = [
10299
# axis
103100
'x',
104101
# group
@@ -109,4 +106,7 @@
109106
'larray_equal', 'larray_nan_equal', 'nan_equal', 'element_equal',
110107
]
111108

112-
__all__ += _deprecated
109+
__all__ += _deprecated_names
110+
111+
# cleanup namespace (module was imported only to initialize handlers for .pkl and .pickle)
112+
del _pkl

larray/core/array.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,8 @@ def _translate_key(self, key):
375375
def __getitem__(self, key):
376376
ndim = self.array.ndim
377377
full_scalar_key = (
378-
(isinstance(key, (int, np.integer)) and ndim == 1) or
379-
(isinstance(key, tuple) and len(key) == ndim and all(isinstance(k, (int, np.integer)) for k in key))
378+
(isinstance(key, (int, np.integer)) and ndim == 1)
379+
or (isinstance(key, tuple) and len(key) == ndim and all(isinstance(k, (int, np.integer)) for k in key))
380380
)
381381
# fast path when the result is a scalar
382382
if full_scalar_key:
@@ -388,8 +388,8 @@ def __setitem__(self, key, value):
388388
array = self.array
389389
ndim = array.ndim
390390
full_scalar_key = (
391-
(isinstance(key, (int, np.integer)) and ndim == 1) or
392-
(isinstance(key, tuple) and len(key) == ndim and all(isinstance(k, (int, np.integer)) for k in key))
391+
(isinstance(key, (int, np.integer)) and ndim == 1)
392+
or (isinstance(key, tuple) and len(key) == ndim and all(isinstance(k, (int, np.integer)) for k in key))
393393
)
394394
# fast path when setting a single cell
395395
if full_scalar_key:
@@ -652,7 +652,7 @@ def get_axis(obj, i):
652652
}
653653

654654

655-
def _doc_agg_method(func, by=False, long_name='', action_verb='perform', extra_args=[], kwargs=[]):
655+
def _doc_agg_method(func, by=False, long_name='', action_verb='perform', extra_args=(), kwargs=()):
656656
if not long_name:
657657
long_name = func.__name__
658658

@@ -800,10 +800,8 @@ class Array(ABCArray):
800800
11 F 0.0 0.0 0.0
801801
12 M 0.0 0.0 0.0
802802
12 F 0.0 0.0 0.0
803-
>>> # with metadata (Python <= 3.5)
804-
>>> arr = Array(data, axes, meta=[('title', 'my title'), ('author', 'John Smith')])
805-
>>> # with metadata (Python 3.6+)
806-
>>> arr = Array(data, axes, meta=Metadata(title='my title', author='John Smith')) # doctest: +SKIP
803+
>>> # with metadata
804+
>>> arr = Array(data, axes, meta=Metadata(title='my title', author='John Smith'))
807805
808806
Array creation functions
809807
@@ -1291,8 +1289,9 @@ def describe(self, *args, **kwargs):
12911289
# when *args is not empty (see https://github.com/larray-project/larray/issues/192)
12921290
# return stack([(~np.isnan(self)).sum(*args), self.mean(*args), self.std(*args),
12931291
# *self.percentile(percentiles, *args)], Axis(labels, 'stats'))
1294-
return stack([(~np.isnan(self)).sum(*args), self.mean(*args), self.std(*args)] +
1295-
[self.percentile(p, *args) for p in percentiles], Axis(labels, 'statistic'))
1292+
return stack([(~np.isnan(self)).sum(*args), self.mean(*args), self.std(*args)]
1293+
+ [self.percentile(p, *args) for p in percentiles],
1294+
Axis(labels, 'statistic'))
12961295

12971296
def describe_by(self, *args, **kwargs):
12981297
r"""
@@ -8504,9 +8503,11 @@ def sequence(axis, initial=0, inc=None, mult=None, func=None, axes=None, title=N
85048503

85058504
# fast path for the most common case
85068505
integer_types = (int, np.integer)
8507-
if (isinstance(mult, integer_types) and mult == 1 and
8508-
isinstance(inc, integer_types) and isinstance(initial, integer_types) and
8509-
func is None and axes is None):
8506+
if (isinstance(mult, integer_types) and mult == 1
8507+
and isinstance(inc, integer_types)
8508+
and isinstance(initial, integer_types)
8509+
and func is None
8510+
and axes is None):
85108511
axis = _make_axis(axis)
85118512
# stop is not included
85128513
stop = initial + inc * len(axis)

larray/core/axis.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -854,11 +854,13 @@ def isscalar(k):
854854
list_res = [self[k] for k in key]
855855
return list_res if isinstance(key, list) else tuple(list_res)
856856
# allow targeting a label from an aggregated axis with the group which created it
857-
elif (not isinstance(self, AxisReference) and
858-
isinstance(key, Group) and
859-
isinstance(key.axis, Axis) and
860-
key.axis.name == self.name and
861-
key.name in self):
857+
elif (
858+
not isinstance(self, AxisReference)
859+
and isinstance(key, Group)
860+
and isinstance(key.axis, Axis)
861+
and key.axis.name == self.name
862+
and key.name in self
863+
):
862864
return LGroup(key.name, None, self)
863865
# elif isinstance(key, basestring) and key in self:
864866
# TODO: this is an awful workaround to avoid the "processing" of string keys which exist as is in the axis

larray/inout/csv.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,7 @@ def list_items(self):
298298
# strip extension from files
299299
# XXX: unsure we should use sorted here
300300
fnames = sorted([os.path.splitext(fname)[0] for fname in fnames])
301-
items = []
302-
try:
303-
fnames.remove('__metadata__')
304-
except:
305-
pass
306-
items += [(name, 'Array') for name in fnames]
307-
return items
301+
return [(name, 'Array') for name in fnames if name != '__metadata__']
308302

309303
def _read_item(self, key, type, *args, **kwargs):
310304
if type == 'Array':

larray/inout/excel.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -246,14 +246,7 @@ def _open_for_write(self):
246246
self.handle = pd.ExcelWriter(self.fname, engine=engine)
247247

248248
def list_items(self):
249-
sheet_names = self.handle.sheet_names
250-
items = []
251-
try:
252-
sheet_names.remove('__metadata__')
253-
except:
254-
pass
255-
items += [(name, 'Array') for name in sheet_names]
256-
return items
249+
return [(name, 'Array') for name in self.handle.sheet_names if name != '__metadata__']
257250

258251
def _read_item(self, key, type, *args, **kwargs):
259252
if type == 'Array':
@@ -308,14 +301,7 @@ def _open_for_write(self):
308301
self.handle = open_excel(self.fname, overwrite_file=self.overwrite_file)
309302

310303
def list_items(self):
311-
sheet_names = self.handle.sheet_names()
312-
items = []
313-
try:
314-
sheet_names.remove('__metadata__')
315-
except:
316-
pass
317-
items += [(name, 'Array') for name in sheet_names]
318-
return items
304+
return [(name, 'Array') for name in self.handle.sheet_names() if name != '__metadata__']
319305

320306
def _read_item(self, key, type, *args, **kwargs):
321307
if type == 'Array':

larray/inout/pandas.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ def df_asarray(df, sort_rows=False, sort_columns=False, raw=False, parse_header=
337337
return from_series(series, sort_rows=sort_columns, **kwargs)
338338

339339
# handle 1D arrays
340-
if len(df) == 1 and (pd.isnull(df.index.values[0]) or
341-
(isinstance(df.index.values[0], str) and df.index.values[0].strip() == '')):
340+
if len(df) == 1 and (pd.isnull(df.index.values[0])
341+
or (isinstance(df.index.values[0], str) and df.index.values[0].strip() == '')):
342342
if parse_header:
343343
df.columns = pd.Index([parse(cell) for cell in df.columns.values], name=df.columns.name)
344344
series = df.iloc[0]
@@ -363,6 +363,6 @@ def parse_axis_name(name):
363363
# make them roundtrip correctly, based on the assumption that in an in-memory LArray an anonymouse axis is more
364364
# likely and useful than an Axis with an empty name.
365365
# TODO : find a more robust and elegant solution
366-
res = res.rename({axis: None for axis in res.axes if isinstance(axis.name, str) and
367-
(axis.name == '' or 'Unnamed:' in axis.name)})
366+
res = res.rename({axis: None for axis in res.axes if (isinstance(axis.name, str)
367+
and (axis.name == '' or 'Unnamed:' in axis.name))})
368368
return res

larray/inout/xw_excel.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@
2525
from xlwings.constants import FileFormat
2626
global_app = None
2727

28-
2928
def is_app_alive(app):
3029
try:
3130
app.books
3231
return True
3332
except Exception:
3433
return False
3534

36-
3735
def kill_global_app():
3836
global global_app
3937

@@ -46,7 +44,6 @@ def kill_global_app():
4644
del global_app
4745
global_app = None
4846

49-
5047
class ArrayConverter(PandasDataFrameConverter):
5148
writes_types = Array
5249

@@ -71,9 +68,8 @@ def _disable_screen_updates(app):
7168
# unsure we can safely do this
7269
# xl_app.EnableEvents = False
7370

74-
75-
# TODO: replace overwrite_file by mode='r'|'w'|'a' the day xlwings will support a read-only mode
7671
class Workbook(object):
72+
# TODO: replace overwrite_file by mode='r'|'w'|'a' the day xlwings will support a read-only mode
7773
def __init__(self, filepath=None, overwrite_file=False, visible=None, silent=None, app=None, load_addins=None):
7874
global global_app
7975

@@ -322,7 +318,6 @@ def __repr__(self):
322318
cls = self.__class__
323319
return f'<{cls.__module__}.{cls.__name__} [{self.name}]>'
324320

325-
326321
def _fill_slice(s, length):
327322
r"""
328323
replaces a slice None bounds by actual bounds.
@@ -340,7 +335,6 @@ def _fill_slice(s, length):
340335
"""
341336
return slice(s.start if s.start is not None else 0, s.stop if s.stop is not None else length, s.step)
342337

343-
344338
def _concrete_key(key, obj, ndim=2):
345339
r"""Expand key to ndim and replace None in slices start/stop bounds by 0 or obj.shape[corresponding_dim]
346340
respectively.
@@ -372,7 +366,6 @@ def _concrete_key(key, obj, ndim=2):
372366
return [_fill_slice(k, length) if isinstance(k, slice) else k
373367
for k, length in zip(key, shape)]
374368

375-
376369
class Sheet(object):
377370
def __init__(self, workbook, key, xw_sheet=None):
378371
if xw_sheet is None:
@@ -515,7 +508,6 @@ def __repr__(self):
515508
xw_sheet = self.xw_sheet
516509
return f'<{cls.__module__}.{cls.__name__} [{xw_sheet.book.name}]{xw_sheet.name}>'
517510

518-
519511
class Range(object):
520512
def __init__(self, sheet, *args):
521513
xw_range = sheet.xw_sheet.range(*args)
@@ -618,7 +610,6 @@ def load(self, header=True, convert_float=True, nb_axes=None, index_col=None, fi
618610
else:
619611
return Array(list_data)
620612

621-
622613
# XXX: deprecate this function?
623614
def open_excel(filepath=None, overwrite_file=False, visible=None, silent=None, app=None, load_addins=None):
624615
return Workbook(filepath, overwrite_file=overwrite_file, visible=visible, silent=silent, app=app,

0 commit comments

Comments
 (0)