8000 STY: PEP8 fixes for numpy/core/*.py · githubmlai/numpy@8b3e9ae · GitHub
[go: up one dir, main page]

Skip to content

Commit 8b3e9ae

Browse files
committed
STY: PEP8 fixes for numpy/core/*.py
1 parent c2ae6aa commit 8b3e9ae

14 files changed

+216
-204
lines changed

numpy/core/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
del os
1919

2020
from . import umath
21-
from . import _internal # for freeze programs
21+
from . import _internal # for freeze programs
2222
from . import numerictypes as nt
2323
multiarray.set_typeDict(nt.sctypeDict)
2424
from . import numeric
@@ -40,8 +40,7 @@
4040
from .shape_base import *
4141
del nt
4242

43-
from .fromnumeric import amax as max, amin as min, \
44-
round_ as round
43+
from .fromnumeric import amax as max, amin as min, round_ as round
4544
from .numeric import absolute as abs
4645

4746
__all__ = ['char', 'rec', 'memmap']

numpy/core/_internal.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ def _usefields(adict, align):
7373
else:
7474
titles.append(None)
7575

76-
return dtype({"names" : names,
77-
"formats" : formats,
78-
"offsets" : offsets,
79-
"titles" : titles}, align)
76+
return dtype({"names": names,
77+
"formats": formats,
78+
"offsets": offsets,
79+
"titles": titles}, align)
8080

8181

8282
# construct an array_protocol descriptor list
@@ -101,7 +101,6 @@ def _array_descr(descriptor):
101101
else:
102102
return (_array_descr(subdtype[0]), subdtype[1])
103103

104-
105104
names = descriptor.names
106105
ordered_fields = [fields[x] + (x,) for x in names]
107106
result = []
@@ -167,8 +166,8 @@ def _commastring(astr):
167166
mo = sep_re.match(astr, pos=startindex)
168167
if not mo:
169168
raise ValueError(
170-
'format number %d of "%s" is not recognized' %
171-
(len(result)+1, astr))
169+
'format number %d of "%s" is not recognized' %
170+
(len(result)+1, astr))
172171
startindex = mo.end()
173172

174173
if order2 == asbytes(''):
@@ -179,7 +178,9 @@ def _commastring(astr):
179178
order1 = _convorder.get(order1, order1)
180179
order2 = _convorder.get(order2, order2)
181180
if (order1 != order2):
182-
raise ValueError('inconsistent byte-order specification %s and %s' % (order1, order2))
181+
raise ValueError(
182+
'inconsistent byte-order specification %s and %s' %
183+
(order1, order2))
183184
order = order1
184185

185186
if order in [asbytes('|'), asbytes('='), _nbo]:
@@ -354,9 +355,9 @@ def _get_all_field_offsets(dtype, base_offset=0):
354355
fields = []
355356
if dtype.fields is not None:
356357
for name in dtype.names:
357-
sub_dtype = dtype.fields[name][0]
358-
sub_offset = dtype.fields[name][1] + base_offset
359-
fields.extend(_get_all_field_offsets(sub_dtype, sub_offset))
358+
sub_dtype = dtype.fields[name][0]
359+
sub_offset = dtype.fields[name][1] + base_offset
360+
fields.extend(_get_all_field_offsets(sub_dtype, sub_offset))
360361
else:
361362
if dtype.shape:
362363
sub_offsets = _get_all_field_offsets(dtype.base, base_offset)
@@ -484,7 +485,7 @@ def _view_is_safe(oldtype, newtype):
484485
# 'tiled positions' of the object match up. Here, we allow
485486
# for arbirary itemsizes (even those possibly disallowed
486487
# due to stride/data length issues).
487-
if old_size == new_size:
488+
if old_size == new_size:
488489
new_num = old_num = 1
489490
else:
490491
gcd_new_old = _gcd(new_size, old_size)
@@ -525,7 +526,7 @@ def _view_is_safe(oldtype, newtype):
525526
's': 'S',
526527
'w': 'U',
527528
'O': 'O',
528-
'x': 'V', # padding
529+
'x': 'V', # padding
529530
}
530531
_pep3118_native_typechars = ''.join(_pep3118_native_map.keys())
531532

@@ -549,7 +550,7 @@ def _view_is_safe(oldtype, newtype):
549550
's': 'S',
550551
'w': 'U',
551552
'O': 'O',
552-
'x': 'V', # padding
553+
'x': 'V', # padding
553554
}
554555
_pep3118_standard_typechars = ''.join(_pep3118_standard_map.keys())
555556

@@ -563,8 +564,10 @@ def _dtype_from_pep3118(spec, byteorder='@', is_subdtype=False):
563564
last_offset = 0
564565

565566
dummy_name_index = [0]
567+
566568
def next_dummy_name():
567569
dummy_name_index[0] += 1
570+
568571
def get_dummy_name():
569572
while True:
570573
name = 'f%d' % dummy_name_index[0]
@@ -698,8 +701,8 @@ def get_dummy_name():
698701
offset += extra_offset
699702

700703
# Check if this was a simple 1-item type
701-
if len(fields) == 1 and not explicit_name and fields['f0'][1] == 0 \
702-
and not is_subdtype:
704+
if (len(fields) == 1 and not explicit_name and
705+
fields['f0'][1] == 0 and not is_subdtype):
703706
ret = fields['f0'][0]
704707
else:
705708
ret = dtype(fields)
@@ -724,8 +727,8 @@ def _add_trailing_padding(value, padding):
724727
else:
725728
vfields = dict(value.fields)
726729

727-
if value.names and value.names[-1] == '' and \
728-
value[''].char == 'V':
730+
if (value.names and value.names[-1] == '' and
731+
value[''].char == 'V'):
729732
# A trailing padding field is already present
730733
vfields[''] = ('V%d' % (vfields[''][0].itemsize + padding),
731734
vfields[''][1])
@@ -757,5 +760,5 @@ def _prod(a):
757760
def _gcd(a, b):
758761
"""Calculate the greatest common divisor of a and b"""
759762
while b:
760-
a, b = b, a%b
763+
a, b = b, a % b
761764
return a

numpy/core/_methods.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ def _mean(a, axis=None, dtype=None, out=None, keepdims=False):
5858
if rcount == 0:
5959
warnings.warn("Mean of empty slice.", RuntimeWarning)
6060

61-
6261
# Cast bool, unsigned int, and int to float64 by default
6362
if dtype is None and issubclass(arr.dtype.type, (nt.integer, nt.bool_)):
6463
dtype = mu.dtype('f8')

numpy/core/arrayprint.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@
3131
_MAXINT = sys.maxint
3232
_MININT = -sys.maxint - 1
3333

34-
def product(x, y): return x*y
34+
def product(x, y):
35+
return x*y
3536

3637
_summaryEdgeItems = 3 # repr N leading and trailing items of each dimension
37-
_summaryThreshold = 1000 # total items > triggers array summarization
38+
_summaryThreshold = 1000 # total items > triggers array summarization
3839

3940
_float_output_precision = 8
4041
_float_output_suppress_small = False
@@ -149,9 +150,10 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None,
149150
... suppress=False, threshold=1000, formatter=None)
150151
"""
151152

152-
global _summaryThreshold, _summaryEdgeItems, _float_output_precision, \
153-
_line_width, _float_output_suppress_small, _nan_str, _inf_str, \
154-
_formatter
153+
global _summaryThreshold, _summaryEdgeItems, _float_output_precision
154+
global _line_width, _float_output_suppress_small, _nan_str, _inf_str
155+
global _formatter
156+
155157
if linewidth is not None:
156158
_line_width = linewidth
157159
if threshold is not None:
@@ -254,17 +256,17 @@ def _array2string(a, max_line_width, precision, suppress_small, separator=' ',
254256
summary_insert = ""
255257
data = ravel(asarray(a))
256258

257-
formatdict = {'bool' : _boolFormatter,
258-
'int' : IntegerFormat(data),
259-
'float' : FloatFormat(data, precision, suppress_small),
260-
'longfloat' : LongFloatFormat(precision),
261-
'complexfloat' : ComplexFormat(data, precision,
259+
formatdict = {'bool': _boolFormatter,
260+
'int': IntegerFormat(data),
261+
'float': FloatFormat(data, precision, suppress_small),
262+
'longfloat': LongFloatFormat(precision),
263+
'complexfloat': ComplexFormat(data, precision,
262264
suppress_small),
263-
'longcomplexfloat' : LongComplexFormat(precision),
264-
'datetime' : DatetimeFormat(data),
265-
'timedelta' : TimedeltaFormat(data),
266-
'numpystr' : repr_format,
267-
'str' : str}
265+
'longcomplexfloat': LongComplexFormat(precision),
266+
'datetime': DatetimeFormat(data),
267+
'timedelta': TimedeltaFormat(data),
268+
'numpystr': repr_format,
269+
'str': str}
268270

269271
if formatter is not None:
270272
fkeys = [k for k in formatter.keys() if formatter[k] is not None]
@@ -469,10 +471,13 @@ def _formatArray(a, format_function, rank, max_line_len,
469471
return str(obj)
470472

471473
if summary_insert and 2*edge_items < len(a):
472-
leading_items, trailing_items, summary_insert1 = \
473-
edge_items, edge_items, summary_insert
474+
leading_items = edge_items
475+
trailing_items = edge_items
476+
summary_insert1 = summary_insert
474477
else:
475-
leading_items, trailing_items, summary_insert1 = 0, len(a), ""
478+
leading_items = 0
479+
trailing_items = len(a)
480+
summary_insert1 = ""
476481

477482
if rank == 1:
478483
s = ""

numpy/core/defchararray.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ def _use_unicode(*args):
5555
result should be unicode.
5656
"""
5757
for x in args:
58-
if (isinstance(x, _unicode)
59-
or issubclass(numpy.asarray(x).dtype.type, unicode_)):
58+
if (isinstance(x, _unicode) or
59+
issubclass(numpy.asarray(x).dtype.type, unicode_)):
6060
return unicode_
6161
return string_
6262

@@ -1068,7 +1068,7 @@ def replace(a, old, new, count=None):
10681068
"""
10691069
return _to_string_or_unicode_array(
10701070
_vec_string(
1071-
a, object_, 'replace', [old, new] +_clean_args(count)))
1071+
a, object_, 'replace', [old, new] + _clean_args(count)))
10721072

10731073

10741074
def rfind(a, sub, start=0, end=None):
@@ -2039,7 +2039,6 @@ def count(self, sub, start=0, end=None):
20392039
"""
20402040
return count(self, sub, start, end)
20412041

2042-
20432042
def decode(self, encoding=None, errors=None):
20442043
"""
20452044
Calls `str.decode` element-wise.
@@ -2610,10 +2609,10 @@ class adds the following functionality:
26102609

26112610
if order is not None:
26122611
obj = numpy.asarray(obj, order=order)
2613-
if (copy
2614-
or (itemsize != obj.itemsize)
2615-
or (not unicode and isinstance(obj, unicode_))
2616-
or (unicode and isinstance(obj, string_))):
2612+
if (copy or
2613+
(itemsize != obj.itemsize) or
2614+
(not unicode and isinstance(obj, unicode_)) or
2615+
(unicode and isinstance(obj, string_))):
26172616
obj = obj.astype((dtype, long(itemsize)))
26182617
return obj
26192618

numpy/core/getlimits.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
def _frz(a):
1414
"""fix rank-0 --> rank-1"""
15-
if a.ndim == 0: a.shape = (1,)
15+
if a.ndim == 0:
16+
a.shape = (1,)
1617
return a
1718

1819
_convert_to_float = {
@@ -170,25 +171,25 @@ def _init(self, dtype):
170171
return self
171172

172173
def __str__(self):
173-
return '''\
174-
Machine parameters for %(dtype)s
175-
---------------------------------------------------------------------
176-
precision=%(precision)3s resolution= %(_str_resolution)s
177-
machep=%(machep)6s eps= %(_str_eps)s
178-
negep =%(negep)6s epsneg= %(_str_epsneg)s
179-
minexp=%(minexp)6s tiny= %(_str_tiny)s
180-
maxexp=%(maxexp)6s max= %(_str_max)s
181-
nexp =%(nexp)6s min= -max
182-
------------------------------------ 57E6 ---------------------------------
183-
''' % self.__dict__
174+
fmt = (
175+
'Machine parameters for %(dtype)s\n'
176+
'---------------------------------------------------------------\n'
177+
'precision=%(precision)3s resolution= %(_str_resolution)s\n'
178+
'machep=%(machep)6s eps= %(_str_eps)s\n'
179+
'negep =%(negep)6s epsneg= %(_str_epsneg)s\n'
180+
'minexp=%(minexp)6s tiny= %(_str_tiny)s\n'
181+
'maxexp=%(maxexp)6s max= %(_str_max)s\n'
182+
'nexp =%(nexp)6s min= -max\n'
183+
'---------------------------------------------------------------\n'
184+
)
185+
return fmt % self.__dict__
184186

185187
def __repr__(self):
186188
c = self.__class__.__name__
187189
d = self.__dict__.copy()
188190
d['klass'] = c
189-
return ("%(klass)s(resolution=%(resolution)s, min=-%(_str_max)s," \
190-
+ " max=%(_str_max)s, dtype=%(dtype)s)") \
191-
% d
191+
return (("%(klass)s(resolution=%(resolution)s, min=-%(_str_max)s,"
192+
" max=%(_str_max)s, dtype=%(dtype)s)") % d)
192193

193194

194195
class iinfo(object):
@@ -249,7 +250,7 @@ def __init__(self, int_type):
249250
self.kind = self.dtype.kind
250251
self.bits = self.dtype.itemsize * 8
251252
self.key = "%s%d" % (self.kind, self.bits)
252-
if not self.kind in 'iu':
253+
if self.kind not in 'iu':
253254
raise ValueError("Invalid integer data type.")
254255

255256
def min(self):
@@ -282,13 +283,14 @@ def max(self):
282283

283284
def __str__(self):
284285
"""String representation."""
285-
return '''\
286-
Machine parameters for %(dtype)s
287-
---------------------------------------------------------------------
288-
min = %(min)s
289-
max = %(max)s
290-
---------------------------------------------------------------------
291-
''' % {'dtype': self.dtype, 'min': self.min, 'max': self.max}
286+
fmt = (
287+
'Machine parameters for %(dtype)s\n'
288+
'---------------------------------------------------------------\n'
289+
'min = %(min)s\n'
290+
'max = %(max)s\n'
291+
'---------------------------------------------------------------\n'
292+
)
293+
return fmt % {'dtype': self.dtype, 'min': self.min, 'max': self.max}
292294

293295
def __repr__(self):
294296
return "%s(min=%s, max=%s, dtype=%s)" % (self.__class__.__name__,

0 commit comments

Comments
 (0)
0