diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2015-07-05 11:47:29 -0600 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2015-07-05 12:18:26 -0600 |
commit | 8b3e9ae5262c1da1118370cd6e83db9b2166952e (patch) | |
tree | ad552d92d8f806db622da102707571a38faec7c2 | |
parent | c2ae6aa0103aecdb5e2a71504583451cada1bfbc (diff) | |
download | numpy-8b3e9ae5262c1da1118370cd6e83db9b2166952e.tar.gz |
STY: PEP8 fixes for numpy/core/*.py
-rw-r--r-- | numpy/core/__init__.py | 5 | ||||
-rw-r--r-- | numpy/core/_internal.py | 41 | ||||
-rw-r--r-- | numpy/core/_methods.py | 1 | ||||
-rw-r--r-- | numpy/core/arrayprint.py | 41 | ||||
-rw-r--r-- | numpy/core/defchararray.py | 15 | ||||
-rw-r--r-- | numpy/core/getlimits.py | 48 | ||||
-rw-r--r-- | numpy/core/machar.py | 52 | ||||
-rw-r--r-- | numpy/core/memmap.py | 1 | ||||
-rw-r--r-- | numpy/core/numeric.py | 38 | ||||
-rw-r--r-- | numpy/core/numerictypes.py | 39 | ||||
-rw-r--r-- | numpy/core/records.py | 32 | ||||
-rw-r--r-- | numpy/core/setup.py | 68 | ||||
-rw-r--r-- | numpy/core/setup_common.py | 29 | ||||
-rw-r--r-- | numpy/core/shape_base.py | 10 |
14 files changed, 216 insertions, 204 deletions
diff --git a/numpy/core/__init__.py b/numpy/core/__init__.py index f6b9534eb..41314cee4 100644 --- a/numpy/core/__init__.py +++ b/numpy/core/__init__.py @@ -18,7 +18,7 @@ del envbak del os from . import umath -from . import _internal # for freeze programs +from . import _internal # for freeze programs from . import numerictypes as nt multiarray.set_typeDict(nt.sctypeDict) from . import numeric @@ -40,8 +40,7 @@ from . import shape_base from .shape_base import * del nt -from .fromnumeric import amax as max, amin as min, \ - round_ as round +from .fromnumeric import amax as max, amin as min, round_ as round from .numeric import absolute as abs __all__ = ['char', 'rec', 'memmap'] diff --git a/numpy/core/_internal.py b/numpy/core/_internal.py index a20bf10e4..b6af93d43 100644 --- a/numpy/core/_internal.py +++ b/numpy/core/_internal.py @@ -73,10 +73,10 @@ def _usefields(adict, align): else: titles.append(None) - return dtype({"names" : names, - "formats" : formats, - "offsets" : offsets, - "titles" : titles}, align) + return dtype({"names": names, + "formats": formats, + "offsets": offsets, + "titles": titles}, align) # construct an array_protocol descriptor list @@ -101,7 +101,6 @@ def _array_descr(descriptor): else: return (_array_descr(subdtype[0]), subdtype[1]) - names = descriptor.names ordered_fields = [fields[x] + (x,) for x in names] result = [] @@ -167,8 +166,8 @@ def _commastring(astr): mo = sep_re.match(astr, pos=startindex) if not mo: raise ValueError( - 'format number %d of "%s" is not recognized' % - (len(result)+1, astr)) + 'format number %d of "%s" is not recognized' % + (len(result)+1, astr)) startindex = mo.end() if order2 == asbytes(''): @@ -179,7 +178,9 @@ def _commastring(astr): order1 = _convorder.get(order1, order1) order2 = _convorder.get(order2, order2) if (order1 != order2): - raise ValueError('inconsistent byte-order specification %s and %s' % (order1, order2)) + raise ValueError( + 'inconsistent byte-order specification %s and %s' % + (order1, order2)) order = order1 if order in [asbytes('|'), asbytes('='), _nbo]: @@ -354,9 +355,9 @@ def _get_all_field_offsets(dtype, base_offset=0): fields = [] if dtype.fields is not None: for name in dtype.names: - sub_dtype = dtype.fields[name][0] - sub_offset = dtype.fields[name][1] + base_offset - fields.extend(_get_all_field_offsets(sub_dtype, sub_offset)) + sub_dtype = dtype.fields[name][0] + sub_offset = dtype.fields[name][1] + base_offset + fields.extend(_get_all_field_offsets(sub_dtype, sub_offset)) else: if dtype.shape: sub_offsets = _get_all_field_offsets(dtype.base, base_offset) @@ -484,7 +485,7 @@ def _view_is_safe(oldtype, newtype): # 'tiled positions' of the object match up. Here, we allow # for arbirary itemsizes (even those possibly disallowed # due to stride/data length issues). - if old_size == new_size: + if old_size == new_size: new_num = old_num = 1 else: gcd_new_old = _gcd(new_size, old_size) @@ -525,7 +526,7 @@ _pep3118_native_map = { 's': 'S', 'w': 'U', 'O': 'O', - 'x': 'V', # padding + 'x': 'V', # padding } _pep3118_native_typechars = ''.join(_pep3118_native_map.keys()) @@ -549,7 +550,7 @@ _pep3118_standard_map = { 's': 'S', 'w': 'U', 'O': 'O', - 'x': 'V', # padding + 'x': 'V', # padding } _pep3118_standard_typechars = ''.join(_pep3118_standard_map.keys()) @@ -563,8 +564,10 @@ def _dtype_from_pep3118(spec, byteorder='@', is_subdtype=False): last_offset = 0 dummy_name_index = [0] + def next_dummy_name(): dummy_name_index[0] += 1 + def get_dummy_name(): while True: name = 'f%d' % dummy_name_index[0] @@ -698,8 +701,8 @@ def _dtype_from_pep3118(spec, byteorder='@', is_subdtype=False): offset += extra_offset # Check if this was a simple 1-item type - if len(fields) == 1 and not explicit_name and fields['f0'][1] == 0 \ - and not is_subdtype: + if (len(fields) == 1 and not explicit_name and + fields['f0'][1] == 0 and not is_subdtype): ret = fields['f0'][0] else: ret = dtype(fields) @@ -724,8 +727,8 @@ def _add_trailing_padding(value, padding): else: vfields = dict(value.fields) - if value.names and value.names[-1] == '' and \ - value[''].char == 'V': + if (value.names and value.names[-1] == '' and + value[''].char == 'V'): # A trailing padding field is already present vfields[''] = ('V%d' % (vfields[''][0].itemsize + padding), vfields[''][1]) @@ -757,5 +760,5 @@ def _prod(a): def _gcd(a, b): """Calculate the greatest common divisor of a and b""" while b: - a, b = b, a%b + a, b = b, a % b return a diff --git a/numpy/core/_methods.py b/numpy/core/_methods.py index 00716e1b4..5fc2bc445 100644 --- a/numpy/core/_methods.py +++ b/numpy/core/_methods.py @@ -58,7 +58,6 @@ def _mean(a, axis=None, dtype=None, out=None, keepdims=False): if rcount == 0: warnings.warn("Mean of empty slice.", RuntimeWarning) - # Cast bool, unsigned int, and int to float64 by default if dtype is None and issubclass(arr.dtype.type, (nt.integer, nt.bool_)): dtype = mu.dtype('f8') diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index b8acaee97..a28b5a89e 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -31,10 +31,11 @@ else: _MAXINT = sys.maxint _MININT = -sys.maxint - 1 -def product(x, y): return x*y +def product(x, y): + return x*y _summaryEdgeItems = 3 # repr N leading and trailing items of each dimension -_summaryThreshold = 1000 # total items > triggers array summarization +_summaryThreshold = 1000 # total items > triggers array summarization _float_output_precision = 8 _float_output_suppress_small = False @@ -149,9 +150,10 @@ def set_printoptions(precision=None, threshold=None, edgeitems=None, ... suppress=False, threshold=1000, formatter=None) """ - global _summaryThreshold, _summaryEdgeItems, _float_output_precision, \ - _line_width, _float_output_suppress_small, _nan_str, _inf_str, \ - _formatter + global _summaryThreshold, _summaryEdgeItems, _float_output_precision + global _line_width, _float_output_suppress_small, _nan_str, _inf_str + global _formatter + if linewidth is not None: _line_width = linewidth if threshold is not None: @@ -254,17 +256,17 @@ def _array2string(a, max_line_width, precision, suppress_small, separator=' ', summary_insert = "" data = ravel(asarray(a)) - formatdict = {'bool' : _boolFormatter, - 'int' : IntegerFormat(data), - 'float' : FloatFormat(data, precision, suppress_small), - 'longfloat' : LongFloatFormat(precision), - 'complexfloat' : ComplexFormat(data, precision, + formatdict = {'bool': _boolFormatter, + 'int': IntegerFormat(data), + 'float': FloatFormat(data, precision, suppress_small), + 'longfloat': LongFloatFormat(precision), + 'complexfloat': ComplexFormat(data, precision, suppress_small), - 'longcomplexfloat' : LongComplexFormat(precision), - 'datetime' : DatetimeFormat(data), - 'timedelta' : TimedeltaFormat(data), - 'numpystr' : repr_format, - 'str' : str} + 'longcomplexfloat': LongComplexFormat(precision), + 'datetime': DatetimeFormat(data), + 'timedelta': TimedeltaFormat(data), + 'numpystr': repr_format, + 'str': str} if formatter is not None: 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, return str(obj) if summary_insert and 2*edge_items < len(a): - leading_items, trailing_items, summary_insert1 = \ - edge_items, edge_items, summary_insert + leading_items = edge_items + trailing_items = edge_items + summary_insert1 = summary_insert else: - leading_items, trailing_items, summary_insert1 = 0, len(a), "" + leading_items = 0 + trailing_items = len(a) + summary_insert1 = "" if rank == 1: s = "" diff --git a/numpy/core/defchararray.py b/numpy/core/defchararray.py index ead11e8d8..e273007c9 100644 --- a/numpy/core/defchararray.py +++ b/numpy/core/defchararray.py @@ -55,8 +55,8 @@ def _use_unicode(*args): result should be unicode. """ for x in args: - if (isinstance(x, _unicode) - or issubclass(numpy.asarray(x).dtype.type, unicode_)): + if (isinstance(x, _unicode) or + issubclass(numpy.asarray(x).dtype.type, unicode_)): return unicode_ return string_ @@ -1068,7 +1068,7 @@ def replace(a, old, new, count=None): """ return _to_string_or_unicode_array( _vec_string( - a, object_, 'replace', [old, new] +_clean_args(count))) + a, object_, 'replace', [old, new] + _clean_args(count))) def rfind(a, sub, start=0, end=None): @@ -2039,7 +2039,6 @@ class chararray(ndarray): """ return count(self, sub, start, end) - def decode(self, encoding=None, errors=None): """ Calls `str.decode` element-wise. @@ -2610,10 +2609,10 @@ def array(obj, itemsize=None, copy=True, unicode=None, order=None): if order is not None: obj = numpy.asarray(obj, order=order) - if (copy - or (itemsize != obj.itemsize) - or (not unicode and isinstance(obj, unicode_)) - or (unicode and isinstance(obj, string_))): + if (copy or + (itemsize != obj.itemsize) or + (not unicode and isinstance(obj, unicode_)) or + (unicode and isinstance(obj, string_))): obj = obj.astype((dtype, long(itemsize))) return obj diff --git a/numpy/core/getlimits.py b/numpy/core/getlimits.py index bd1c4571b..2ea9c0e11 100644 --- a/numpy/core/getlimits.py +++ b/numpy/core/getlimits.py @@ -12,7 +12,8 @@ from .numeric import array def _frz(a): """fix rank-0 --> rank-1""" - if a.ndim == 0: a.shape = (1,) + if a.ndim == 0: + a.shape = (1,) return a _convert_to_float = { @@ -170,25 +171,25 @@ class finfo(object): return self def __str__(self): - return '''\ -Machine parameters for %(dtype)s ---------------------------------------------------------------------- -precision=%(precision)3s resolution= %(_str_resolution)s -machep=%(machep)6s eps= %(_str_eps)s -negep =%(negep)6s epsneg= %(_str_epsneg)s -minexp=%(minexp)6s tiny= %(_str_tiny)s -maxexp=%(maxexp)6s max= %(_str_max)s -nexp =%(nexp)6s min= -max ---------------------------------------------------------------------- -''' % self.__dict__ + fmt = ( + 'Machine parameters for %(dtype)s\n' + '---------------------------------------------------------------\n' + 'precision=%(precision)3s resolution= %(_str_resolution)s\n' + 'machep=%(machep)6s eps= %(_str_eps)s\n' + 'negep =%(negep)6s epsneg= %(_str_epsneg)s\n' + 'minexp=%(minexp)6s tiny= %(_str_tiny)s\n' + 'maxexp=%(maxexp)6s max= %(_str_max)s\n' + 'nexp =%(nexp)6s min= -max\n' + '---------------------------------------------------------------\n' + ) + return fmt % self.__dict__ def __repr__(self): c = self.__class__.__name__ d = self.__dict__.copy() d['klass'] = c - return ("%(klass)s(resolution=%(resolution)s, min=-%(_str_max)s," \ - + " max=%(_str_max)s, dtype=%(dtype)s)") \ - % d + return (("%(klass)s(resolution=%(resolution)s, min=-%(_str_max)s," + " max=%(_str_max)s, dtype=%(dtype)s)") % d) class iinfo(object): @@ -249,7 +250,7 @@ class iinfo(object): self.kind = self.dtype.kind self.bits = self.dtype.itemsize * 8 self.key = "%s%d" % (self.kind, self.bits) - if not self.kind in 'iu': + if self.kind not in 'iu': raise ValueError("Invalid integer data type.") def min(self): @@ -282,13 +283,14 @@ class iinfo(object): def __str__(self): """String representation.""" - return '''\ -Machine parameters for %(dtype)s ---------------------------------------------------------------------- -min = %(min)s -max = %(max)s ---------------------------------------------------------------------- -''' % {'dtype': self.dtype, 'min': self.min, 'max': self.max} + fmt = ( + 'Machine parameters for %(dtype)s\n' + '---------------------------------------------------------------\n' + 'min = %(min)s\n' + 'max = %(max)s\n' + '---------------------------------------------------------------\n' + ) + return fmt % {'dtype': self.dtype, 'min': self.min, 'max': self.max} def __repr__(self): return "%s(min=%s, max=%s, dtype=%s)" % (self.__class__.__name__, diff --git a/numpy/core/machar.py b/numpy/core/machar.py index 9eb4430a6..6f2735d32 100644 --- a/numpy/core/machar.py +++ b/numpy/core/machar.py @@ -94,16 +94,19 @@ class MachAr(object): Cambridge University Press, 2002, p. 31. """ + def __init__(self, float_conv=float,int_conv=int, float_to_float=float, - float_to_str = lambda v:'%24.16e' % v, - title = 'Python floating point number'): + float_to_str=lambda v:'%24.16e' % v, + title='Python floating point number'): """ - float_conv - convert integer to float (array) - int_conv - convert float (array) to integer - float_to_float - convert float array to float - float_to_str - convert array float to str - title - description of used floating point numbers + + float_conv - convert integer to float (array) + int_conv - convert float (array) to integer + float_to_float - convert float array to float + float_to_str - convert array float to str + title - description of used floating point numbers + """ # We ignore all errors here because we are purposely triggering # underflow to detect the properties of the runninng arch. @@ -169,7 +172,7 @@ class MachAr(object): irnd = 1 tempa = a + beta temp = tempa + betah - if irnd==0 and any(temp-tempa != zero): + if irnd == 0 and any(temp-tempa != zero): irnd = 2 # Determine negep and epsneg @@ -188,7 +191,7 @@ class MachAr(object): # Prevent infinite loop on PPC with gcc 4.0: if negep < 0: raise RuntimeError("could not determine machine tolerance " - "for 'negep', locals() -> %s" % (locals())) + "for 'negep', locals() -> %s" % (locals())) else: raise RuntimeError(msg % (_, one.dtype)) negep = -negep @@ -211,7 +214,7 @@ class MachAr(object): # Determine ngrd ngrd = 0 temp = one + eps - if irnd==0 and any(temp*one - one != zero): + if irnd == 0 and any(temp*one - one != zero): ngrd = 1 # Determine iexp @@ -223,9 +226,9 @@ class MachAr(object): for _ in range(max_iterN): y = z z = y*y - a = z*one # Check here for underflow + a = z*one # Check here for underflow temp = z*t - if any(a+a == zero) or any(abs(z)>=y): + if any(a+a == zero) or any(abs(z) >= y): break temp1 = temp * betain if any(temp1*beta == z): @@ -251,7 +254,7 @@ class MachAr(object): y = y * betain a = y * one temp = y * t - if any(a+a != zero) and any(abs(y) < xmin): + if any((a + a) != zero) and any(abs(y) < xmin): k = k + 1 temp1 = temp * betain if any(temp1*beta == y) and any(temp != y): @@ -285,7 +288,7 @@ class MachAr(object): xmax = xmax / (xmin*beta*beta*beta) i = maxexp + minexp + 3 for j in range(i): - if ibeta==2: + if ibeta == 2: xmax = xmax + xmax else: xmax = xmax * beta @@ -322,16 +325,17 @@ class MachAr(object): self._str_resolution = float_to_str(resolution) def __str__(self): - return '''\ -Machine parameters for %(title)s ---------------------------------------------------------------------- -ibeta=%(ibeta)s it=%(it)s iexp=%(iexp)s ngrd=%(ngrd)s irnd=%(irnd)s -machep=%(machep)s eps=%(_str_eps)s (beta**machep == epsilon) -negep =%(negep)s epsneg=%(_str_epsneg)s (beta**epsneg) -minexp=%(minexp)s xmin=%(_str_xmin)s (beta**minexp == tiny) -maxexp=%(maxexp)s xmax=%(_str_xmax)s ((1-epsneg)*beta**maxexp == huge) ---------------------------------------------------------------------- -''' % self.__dict__ + fmt = ( + 'Machine parameters for %(title)s\n' + '---------------------------------------------------------------------\n' + 'ibeta=%(ibeta)s it=%(it)s iexp=%(iexp)s ngrd=%(ngrd)s irnd=%(irnd)s\n' + 'machep=%(machep)s eps=%(_str_eps)s (beta**machep == epsilon)\n' + 'negep =%(negep)s epsneg=%(_str_epsneg)s (beta**epsneg)\n' + 'minexp=%(minexp)s xmin=%(_str_xmin)s (beta**minexp == tiny)\n' + 'maxexp=%(maxexp)s xmax=%(_str_xmax)s ((1-epsneg)*beta**maxexp == huge)\n' + '---------------------------------------------------------------------\n' + ) + return fmt % self.__dict__ if __name__ == '__main__': diff --git a/numpy/core/memmap.py b/numpy/core/memmap.py index 6397e8939..2e8b305d3 100644 --- a/numpy/core/memmap.py +++ b/numpy/core/memmap.py @@ -200,6 +200,7 @@ class memmap(ndarray): """ __array_priority__ = -100.0 + def __new__(subtype, filename, dtype=uint8, mode='r+', offset=0, shape=None, order='C'): # Import here to minimize 'import numpy' overhead diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index f7b0f49fa..c31c5f75f 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -826,8 +826,8 @@ def flatnonzero(a): return a.ravel().nonzero()[0] _mode_from_name_dict = {'v': 0, - 's' : 1, - 'f' : 2} + 's': 1, + 'f': 2} def _mode_from_name(mode): if isinstance(mode, basestring): @@ -991,9 +991,9 @@ def convolve(a,v,mode='full'): a, v = array(a, copy=False, ndmin=1), array(v, copy=False, ndmin=1) if (len(v) > len(a)): a, v = v, a - if len(a) == 0 : + if len(a) == 0: raise ValueError('a cannot be empty') - if len(v) == 0 : + if len(v) == 0: raise ValueError('v cannot be empty') mode = _mode_from_name(mode) return multiarray.correlate(a, v[::-1], mode) @@ -1281,7 +1281,8 @@ def tensordot(a, b, axes=2): bs = b.shape ndb = len(b.shape) equal = True - if (na != nb): equal = False + if (na != nb): + equal = False else: for k in range(na): if as_[axes_a[k]] != bs[axes_b[k]]: @@ -1701,14 +1702,14 @@ def array_repr(arr, max_line_width=None, precision=None, suppress_small=None): 'array([ 0.000001, 0. , 2. , 3. ])' """ - if arr.size > 0 or arr.shape==(0,): + if arr.size > 0 or arr.shape == (0,): lst = array2string(arr, max_line_width, precision, suppress_small, ', ', "array(") - else: # show zero-length shape unless it is (0,) + else: # show zero-length shape unless it is (0,) lst = "[], shape=%s" % (repr(arr.shape),) if arr.__class__ is not ndarray: - cName= arr.__class__.__name__ + cName = arr.__class__.__name__ else: cName = "array" @@ -1900,7 +1901,7 @@ def indices(dimensions, dtype=int): for i, dim in enumerate(dimensions): tmp = arange(dim, dtype=dtype) tmp.shape = (1,)*i + (dim,)+(1,)*(N-i-1) - newdim = dimensions[:i] + (1,)+ dimensions[i+1:] + newdim = dimensions[:i] + (1,) + dimensions[i+1:] val = zeros(newdim, dtype) add(tmp, val, res[i]) return res @@ -2548,13 +2549,17 @@ def seterr(all=None, divide=None, over=None, under=None, invalid=None): pyvals = umath.geterrobj() old = geterr() - if divide is None: divide = all or old['divide'] - if over is None: over = all or old['over'] - if under is None: under = all or old['under'] - if invalid is None: invalid = all or old['invalid'] + if divide is None: + divide = all or old['divide'] + if over is None: + over = all or old['over'] + if under is None: + under = all or old['under'] + if invalid is None: + invalid = all or old['invalid'] maskvalue = ((_errdict[divide] << SHIFT_DIVIDEBYZERO) + - (_errdict[over] << SHIFT_OVERFLOW ) + + (_errdict[over] << SHIFT_OVERFLOW) + (_errdict[under] << SHIFT_UNDERFLOW) + (_errdict[invalid] << SHIFT_INVALID)) @@ -2626,9 +2631,9 @@ def setbufsize(size): if size > 10e6: raise ValueError("Buffer size, %s, is too big." % size) if size < 5: - raise ValueError("Buffer size, %s, is too small." %size) + raise ValueError("Buffer size, %s, is too small." % size) if size % 16 != 0: - raise ValueError("Buffer size, %s, is not a multiple of 16." %size) + raise ValueError("Buffer size, %s, is not a multiple of 16." % size) pyvals = umath.geterrobj() old = getbufsize() @@ -2849,6 +2854,7 @@ class errstate(object): """ # Note that we don't want to run the above doctests because they will fail # without a from __future__ import with_statement + def __init__(self, **kwargs): self.call = kwargs.pop('call', _Unspecified) self.kwargs = kwargs diff --git a/numpy/core/numerictypes.py b/numpy/core/numerictypes.py index 0c03cce89..d8277beea 100644 --- a/numpy/core/numerictypes.py +++ b/numpy/core/numerictypes.py @@ -117,16 +117,9 @@ else: _all_chars = [chr(_m) for _m in range(256)] _ascii_upper = _all_chars[65:65+26] _ascii_lower = _all_chars[97:97+26] -LOWER_TABLE="".join(_all_chars[:65] + _ascii_lower + _all_chars[65+26:]) -UPPER_TABLE="".join(_all_chars[:97] + _ascii_upper + _all_chars[97+26:]) +LOWER_TABLE = "".join(_all_chars[:65] + _ascii_lower + _all_chars[65+26:]) +UPPER_TABLE = "".join(_all_chars[:97] + _ascii_upper + _all_chars[97+26:]) -#import string -# assert (string.maketrans(string.ascii_uppercase, string.ascii_lowercase) == \ -# LOWER_TABLE) -# assert (string.maketrnas(string_ascii_lowercase, string.ascii_uppercase) == \ -# UPPER_TABLE) -#LOWER_TABLE = string.maketrans(string.ascii_uppercase, string.ascii_lowercase) -#UPPER_TABLE = string.maketrans(string.ascii_lowercase, string.ascii_uppercase) def english_lower(s): """ Apply English case rules to convert ASCII strings to all lower case. @@ -251,30 +244,30 @@ def bitname(obj): if name == 'bool_': char = 'b' base = 'bool' - elif name=='void': + elif name == 'void': char = 'V' base = 'void' - elif name=='object_': + elif name == 'object_': char = 'O' base = 'object' bits = 0 - elif name=='datetime64': + elif name == 'datetime64': char = 'M' - elif name=='timedelta64': + elif name == 'timedelta64': char = 'm' if sys.version_info[0] >= 3: - if name=='bytes_': + if name == 'bytes_': char = 'S' base = 'bytes' - elif name=='str_': + elif name == 'str_': char = 'U' base = 'str' else: - if name=='string_': + if name == 'string_': char = 'S' base = 'string' - elif name=='unicode_': + elif name == 'unicode_': char = 'U' base = 'unicode' @@ -310,11 +303,12 @@ def _add_aliases(): typeobj = typeinfo[a][-1] # insert bit-width version for this class (if relevant) base, bit, char = bitname(typeobj) - if base[-3:] == 'int' or char[0] in 'ui': continue + if base[-3:] == 'int' or char[0] in 'ui': + continue if base != '': myname = "%s%d" % (base, bit) - if (name != 'longdouble' and name != 'clongdouble') or \ - myname not in allTypes.keys(): + if ((name != 'longdouble' and name != 'clongdouble') or + myname not in allTypes.keys()): allTypes[myname] = typeobj sctypeDict[myname] = typeobj if base == 'complex': @@ -549,7 +543,7 @@ _python_types = {int: 'int_', bytes: 'bytes_', unicode: 'unicode_', buffer_type: 'void', - } + } if sys.version_info[0] >= 3: def _python_type(t): @@ -778,6 +772,7 @@ class _typedict(dict): first they have to be populated. """ + def __getitem__(self, obj): return dict.__getitem__(self, obj2sctype(obj)) @@ -864,7 +859,7 @@ except AttributeError: ScalarType.extend(_sctype2char_dict.keys()) ScalarType = tuple(ScalarType) for key in _sctype2char_dict.keys(): - cast[key] = lambda x, k=key : array(x, copy=False).astype(k) + cast[key] = lambda x, k=key: array(x, copy=False).astype(k) # Create the typestring lookup dictionary _typestr = _typedict() diff --git a/numpy/core/records.py b/numpy/core/records.py index 9c6d8347a..4a995533a 100644 --- a/numpy/core/records.py +++ b/numpy/core/records.py @@ -139,6 +139,7 @@ class format_parser: dtype([('f0', '<f8'), ('f1', '<i4'), ('f2', '|S5')]) """ + def __init__(self, formats, names, titles, aligned=False, byteorder=None): self._parseFormats(formats, aligned) self._setfieldnames(names, titles) @@ -434,7 +435,7 @@ class recarray(ndarray): # accessed by attribute). try: return object.__getattribute__(self, attr) - except AttributeError: # attr must be a fieldname + except AttributeError: # attr must be a fieldname pass # look for a field with this name @@ -478,9 +479,10 @@ class recarray(ndarray): fielddict = ndarray.__getattribute__(self, 'dtype').fields or {} if attr not in fielddict: return ret - if newattr: # We just added this one - try: # or this setattr worked on an internal - # attribute. + if newattr: + # We just added this one or this setattr worked on an + # internal attribute. + try: object.__delattr__(self, attr) except: return ret @@ -506,14 +508,14 @@ class recarray(ndarray): def __repr__(self): # get data/shape string. logic taken from numeric.array_repr - if self.size > 0 or self.shape==(0,): + if self.size > 0 or self.shape == (0,): lst = sb.array2string(self, separator=', ') else: # show zero-length shape unless it is (0,) lst = "[], shape=%s" % (repr(self.shape),) if (self.dtype.type is record - or (not issubclass(self.dtype.type, nt.void)) ): + or (not issubclass(self.dtype.type, nt.void))): # If this is a full record array (has numpy.record dtype), # or if it has a scalar (non-void) dtype with no records, # represent it using the rec.array function. Since rec.array @@ -683,7 +685,6 @@ def fromstring(datastring, dtype=None, shape=None, offset=0, formats=None, """ create a (read-only) record array from binary data contained in a string""" - if dtype is None and formats is None: raise ValueError("Must have dtype= or formats=") @@ -756,7 +757,7 @@ def fromfile(fd, dtype=None, shape=None, offset=0, formats=None, shapesize = shapeprod * itemsize if shapesize < 0: shape = list(shape) - shape[ shape.index(-1) ] = size / -shapesize + shape[shape.index(-1)] = size / -shapesize shape = tuple(shape) shapeprod = sb.array(shape).prod() @@ -781,10 +782,9 @@ def array(obj, dtype=None, shape=None, offset=0, strides=None, formats=None, """Construct a record array from a wide-variety of objects. """ - if (isinstance(obj, (type(None), str)) or isfileobj(obj)) \ - and (formats is None) \ - and (dtype is None): - raise ValueError("Must define formats (or dtype) if object is "\ + if ((isinstance(obj, (type(None), str)) or isfileobj(obj)) and + (formats is None) and (dtype is None)): + raise ValueError("Must define formats (or dtype) if object is " "None, string, or an open file") kwds = {} @@ -795,10 +795,10 @@ def array(obj, dtype=None, shape=None, offset=0, strides=None, formats=None, aligned, byteorder)._descr else: kwds = {'formats': formats, - 'names' : names, - 'titles' : titles, - 'aligned' : aligned, - 'byteorder' : byteorder + 'names': names, + 'titles': titles, + 'aligned': aligned, + 'byteorder': byteorder } if obj is None: diff --git a/numpy/core/setup.py b/numpy/core/setup.py index 7a82f1e35..ac797562d 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -247,7 +247,7 @@ def check_ieee_macros(config): # Note: including Python.h is necessary because it modifies some math.h # definitions for f in macros: - st = config.check_decl(f, headers = ["Python.h", "math.h"]) + st = config.check_decl(f, headers=["Python.h", "math.h"]) if st: _add_decl(f) @@ -276,7 +276,7 @@ def check_types(config_cmd, ext, build_dir): result = config_cmd.check_header('Python.h') if not result: raise SystemError( - "Cannot compile 'Python.h'. Perhaps you need to "\ + "Cannot compile 'Python.h'. Perhaps you need to " "install python-dev|python-devel.") res = config_cmd.check_header("endian.h") if res: @@ -285,7 +285,7 @@ def check_types(config_cmd, ext, build_dir): # Check basic types sizes for type in ('short', 'int', 'long'): - res = config_cmd.check_decl("SIZEOF_%s" % sym2def(type), headers = ["Python.h"]) + res = config_cmd.check_decl("SIZEOF_%s" % sym2def(type), headers=["Python.h"]) if res: public_defines.append(('NPY_SIZEOF_%s' % sym2def(type), "SIZEOF_%s" % sym2def(type))) else: @@ -297,7 +297,7 @@ def check_types(config_cmd, ext, build_dir): for type in ('float', 'double', 'long double'): already_declared = config_cmd.check_decl("SIZEOF_%s" % sym2def(type), - headers = ["Python.h"]) + headers=["Python.h"]) res = config_cmd.check_type_size(type, expected=expected[type]) if res >= 0: public_defines.append(('NPY_SIZEOF_%s' % sym2def(type), '%d' % res)) @@ -317,7 +317,6 @@ def check_types(config_cmd, ext, build_dir): else: raise SystemError("Checking sizeof (%s) failed !" % complex_def) - for type in ('Py_intptr_t', 'off_t'): res = config_cmd.check_type_size(type, headers=["Python.h"], library_dirs=[pythonlib_dir()], @@ -350,7 +349,7 @@ def check_types(config_cmd, ext, build_dir): if not config_cmd.check_decl('CHAR_BIT', headers=['Python.h']): raise RuntimeError( - "Config wo CHAR_BIT is not supported"\ + "Config wo CHAR_BIT is not supported" ", please contact the maintainers") return private_defines, public_defines @@ -401,7 +400,7 @@ def configuration(parent_package='',top_path=None): open(generate_umath_py, 'U'), generate_umath_py, ('.py', 'U', 1)) - header_dir = 'include/numpy' # this is relative to config.path_in_package + header_dir = 'include/numpy' # this is relative to config.path_in_package cocache = CallOnceOnly() @@ -431,7 +430,7 @@ def configuration(parent_package='',top_path=None): moredefs.append('__NPY_PRIVATE_NO_SIGNAL') # Windows checks - if sys.platform=='win32' or os.name=='nt': + if sys.platform == 'win32' or os.name == 'nt': win32_checks(moredefs) # C99 restrict keyword @@ -558,7 +557,7 @@ def configuration(parent_package='',top_path=None): moredefs.append(('NPY_RELAXED_STRIDES_CHECKING', 1)) # Check wether we can use inttypes (C99) formats - if config_cmd.check_decl('PRIdPTR', headers = ['inttypes.h']): + if config_cmd.check_decl('PRIdPTR', headers=['inttypes.h']): moredefs.append(('NPY_USE_C99_FORMATS', 1)) # visibility check @@ -645,7 +644,7 @@ def configuration(parent_package='',top_path=None): # generate_numpyconfig_h as sources *before* adding npymath. config.add_extension('_dummy', - sources = [join('src', 'dummymodule.c'), + sources=[join('src', 'dummymodule.c'), generate_config_h, generate_numpyconfig_h, generate_numpy_api] @@ -656,6 +655,7 @@ def configuration(parent_package='',top_path=None): ####################################################################### subst_dict = dict([("sep", os.path.sep), ("pkgname", "numpy.core")]) + def get_mathlib_info(*args): # Another ugly hack: the mathlib info is known once build_src is run, # but we cannot use add_installed_pkg_config here either, so we only @@ -678,7 +678,8 @@ def configuration(parent_package='',top_path=None): npymath_sources = [join('src', 'npymath', 'npy_math.c.src'), join('src', 'npymath', 'ieee754.c.src'), join('src', 'npymath', 'npy_math_complex.c.src'), - join('src', 'npymath', 'halffloat.c')] + join('src', 'npymath', 'halffloat.c') + ] config.add_installed_library('npymath', sources=npymath_sources + [get_mathlib_info], install_dir='lib') @@ -692,19 +693,18 @@ def configuration(parent_package='',top_path=None): ####################################################################### # This library is created for the build but it is not installed - npysort_sources=[join('src', 'npysort', 'quicksort.c.src'), - join('src', 'npysort', 'mergesort.c.src'), - join('src', 'npysort', 'heapsort.c.src'), - join('src', 'private', 'npy_partition.h.src'), - join('src', 'npysort', 'selection.c.src'), - join('src', 'private', 'npy_binsearch.h.src'), - join('src', 'npysort', 'binsearch.c.src'), - ] + npysort_sources = [join('src', 'npysort', 'quicksort.c.src'), + join('src', 'npysort', 'mergesort.c.src'), + join('src', 'npysort', 'heapsort.c.src'), + join('src', 'private', 'npy_partition.h.src'), + join('src', 'npysort', 'selection.c.src'), + join('src', 'private', 'npy_binsearch.h.src'), + join('src', 'npysort', 'binsearch.c.src'), + ] config.add_library('npysort', sources=npysort_sources, include_dirs=[]) - ####################################################################### # multiarray module # ####################################################################### @@ -721,7 +721,8 @@ def configuration(parent_package='',top_path=None): join(local_dir, subpath, 'nditer_templ.c.src'), join(local_dir, subpath, 'lowlevel_strided_loops.c.src'), join(local_dir, subpath, 'einsum.c.src'), - join(local_dir, 'src', 'private', 'templ_common.h.src')] + join(local_dir, 'src', 'private', 'templ_common.h.src') + ] # numpy.distutils generate .c from .c.src in weird directories, we have # to add them there as they depend on the build_dir @@ -834,11 +835,11 @@ def configuration(parent_package='',top_path=None): ] blas_info = get_info('blas_opt', 0) - if blas_info and ('HAVE_CBLAS', None) in blas_info.get('define_macros', []): + if blas_info and ('HAVE_CBLAS', None) in blas_info.get('define_macros', []): extra_info = blas_info multiarray_src.extend([join('src', 'multiarray', 'cblasfuncs.c'), join('src', 'multiarray', 'python_xerbla.c'), - ]) + ]) if uses_accelerate_framework(blas_info): multiarray_src.extend(get_sgemv_fix()) else: @@ -849,7 +850,6 @@ def configuration(parent_package='',top_path=None): multiarray_src = [join('src', 'multiarray', 'multiarraymodule_onefile.c')] multiarray_src.append(generate_multiarray_templated_sources) - config.add_extension('multiarray', sources=multiarray_src + [generate_config_h, @@ -885,7 +885,6 @@ def configuration(parent_package='',top_path=None): cmd.ensure_finalized() cmd.template_sources(sources, ext) - def generate_umath_c(ext, build_dir): target = join(build_dir, header_dir, '__umath_generated.c') dir = os.path.dirname(target) @@ -926,50 +925,49 @@ def configuration(parent_package='',top_path=None): umath_src.append(join('src', 'umath', 'simd.inc.src')) config.add_extension('umath', - sources = umath_src + + sources=umath_src + [generate_config_h, generate_numpyconfig_h, generate_umath_c, generate_ufunc_api], - depends = deps + umath_deps, - libraries = ['npymath'], + depends=deps + umath_deps, + libraries=['npymath'], ) - ####################################################################### # umath_tests module # ####################################################################### config.add_extension('umath_tests', - sources = [join('src', 'umath', 'umath_tests.c.src')]) + sources=[join('src', 'umath', 'umath_tests.c.src')]) ####################################################################### # custom rational dtype module # ####################################################################### config.add_extension('test_rational', - sources = [join('src', 'umath', 'test_rational.c.src')]) + sources=[join('src', 'umath', 'test_rational.c.src')]) ####################################################################### # struct_ufunc_test module # ####################################################################### config.add_extension('struct_ufunc_test', - sources = [join('src', 'umath', 'struct_ufunc_test.c.src')]) + sources=[join('src', 'umath', 'struct_ufunc_test.c.src')]) ####################################################################### # multiarray_tests module # ####################################################################### config.add_extension('multiarray_tests', - sources = [join('src', 'multiarray', 'multiarray_tests.c.src')]) + sources=[join('src', 'multiarray', 'multiarray_tests.c.src')]) ####################################################################### # operand_flag_tests module # ####################################################################### config.add_extension('operand_flag_tests', - sources = [join('src', 'umath', 'operand_flag_tests.c.src')]) + sources=[join('src', 'umath', 'operand_flag_tests.c.src')]) config.add_data_dir('tests') config.add_data_dir('tests/data') @@ -978,6 +976,6 @@ def configuration(parent_package='',top_path=None): return config -if __name__=='__main__': +if __name__ == '__main__': from numpy.distutils.core import setup setup(configuration=configuration) diff --git a/numpy/core/setup_common.py b/numpy/core/setup_common.py index 09e6b1595..c7bb8688c 100644 --- a/numpy/core/setup_common.py +++ b/numpy/core/setup_common.py @@ -83,11 +83,12 @@ def check_api_version(apiversion, codegen_dir): # To compute the checksum of the current API, use # code_generators/cversions.py script if not curapi_hash == api_hash: - msg = "API mismatch detected, the C API version " \ - "numbers have to be updated. Current C api version is %d, " \ - "with checksum %s, but recorded checksum for C API version %d in " \ - "codegen_dir/cversions.txt is %s. If functions were added in the " \ - "C API, you have to update C_API_VERSION in %s." + msg = ("API mismatch detected, the C API version " + "numbers have to be updated. Current C api version is %d, " + "with checksum %s, but recorded checksum for C API version %d in " + "codegen_dir/cversions.txt is %s. If functions were added in the " + "C API, you have to update C_API_VERSION in %s." + ) warnings.warn(msg % (apiversion, curapi_hash, apiversion, api_hash, __file__), MismatchCAPIWarning) @@ -106,8 +107,8 @@ OPTIONAL_STDFUNCS = ["expm1", "log1p", "acosh", "asinh", "atanh", OPTIONAL_HEADERS = [ # sse headers only enabled automatically on amd64/x32 builds - "xmmintrin.h", # SSE - "emmintrin.h", # SSE2 + "xmmintrin.h", # SSE + "emmintrin.h", # SSE2 "features.h", # for glibc version linux ] @@ -121,8 +122,8 @@ OPTIONAL_INTRINSICS = [("__builtin_isnan", '5.'), ("__builtin_bswap64", '5u'), ("__builtin_expect", '5, 0'), ("__builtin_mul_overflow", '5, 5, (int*)5'), - ("_mm_load_ps", '(float*)0', "xmmintrin.h"), # SSE - ("_mm_load_pd", '(double*)0', "emmintrin.h"), # SSE2 + ("_mm_load_ps", '(float*)0', "xmmintrin.h"), # SSE + ("_mm_load_pd", '(double*)0', "emmintrin.h"), # SSE2 ] # function attributes @@ -134,7 +135,7 @@ OPTIONAL_FUNCTION_ATTRIBUTES = [('__attribute__((optimize("unroll-loops")))', 'attribute_optimize_opt_3'), ('__attribute__((nonnull (1)))', 'attribute_nonnull'), - ] + ] # variable attributes tested via "int %s a" % attribute OPTIONAL_VARIABLE_ATTRIBUTES = ["__thread", "__declspec(thread)"] @@ -282,10 +283,10 @@ _MOTOROLA_EXTENDED_12B = ['300', '031', '000', '000', '353', '171', _IEEE_QUAD_PREC_BE = ['300', '031', '326', '363', '105', '100', '000', '000', '000', '000', '000', '000', '000', '000', '000', '000'] _IEEE_QUAD_PREC_LE = _IEEE_QUAD_PREC_BE[::-1] -_DOUBLE_DOUBLE_BE = ['301', '235', '157', '064', '124', '000', '000', '000'] + \ - ['000'] * 8 -_DOUBLE_DOUBLE_LE = ['000', '000', '000', '124', '064', '157', '235', '301'] + \ - ['000'] * 8 +_DOUBLE_DOUBLE_BE = (['301', '235', '157', '064', '124', '000', '000', '000'] + + ['000'] * 8) +_DOUBLE_DOUBLE_LE = (['000', '000', '000', '124', '064', '157', '235', '301'] + + ['000'] * 8) def long_double_representation(lines): """Given a binary dump as given by GNU od -b, look for long double diff --git a/numpy/core/shape_base.py b/numpy/core/shape_base.py index 3259f3b1d..50c46f38e 100644 --- a/numpy/core/shape_base.py +++ b/numpy/core/shape_base.py @@ -48,9 +48,9 @@ def atleast_1d(*arys): res = [] for ary in arys: ary = asanyarray(ary) - if len(ary.shape) == 0 : + if len(ary.shape) == 0: result = ary.reshape(1) - else : + else: result = ary res.append(result) if len(res) == 1: @@ -98,11 +98,11 @@ def atleast_2d(*arys): res = [] for ary in arys: ary = asanyarray(ary) - if len(ary.shape) == 0 : + if len(ary.shape) == 0: result = ary.reshape(1, 1) - elif len(ary.shape) == 1 : + elif len(ary.shape) == 1: result = ary[newaxis,:] - else : + else: result = ary res.append(result) if len(res) == 1: |