Skip to content

Commit a3766f4

Browse files
author
Jeff Whitaker
authored
Merge pull request #1321 from Unidata/fixindex
clean up for loop syntax
2 parents 9f74549 + d006b5f commit a3766f4

1 file changed

Lines changed: 25 additions & 24 deletions

File tree

src/netCDF4/_netCDF4.pyx

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,7 +1593,7 @@ cdef _get_att_names(int grpid, int varid):
15931593
ierr = nc_inq_varnatts(grpid, varid, &numatts)
15941594
_ensure_nc_success(ierr, err_cls=AttributeError)
15951595
attslist = []
1596-
for n from 0 <= n < numatts:
1596+
for n in range(numatts):
15971597
with nogil:
15981598
ierr = nc_inq_attname(grpid, varid, n, namstring)
15991599
_ensure_nc_success(ierr, err_cls=AttributeError)
@@ -1868,7 +1868,7 @@ cdef _get_types(group):
18681868
enumtypes = dict()
18691869

18701870
if ntypes > 0:
1871-
for n from 0 <= n < ntypes:
1871+
for n in range(ntypes):
18721872
xtype = typeids[n]
18731873
with nogil:
18741874
ierr = nc_inq_user_type(_grpid, xtype, namstring,
@@ -1930,9 +1930,9 @@ cdef _get_dims(group):
19301930
ierr = nc_inq_dimids(_grpid, &numdims, dimids, 0)
19311931
_ensure_nc_success(ierr)
19321932
else:
1933-
for n from 0 <= n < numdims:
1933+
for n in range(numdims):
19341934
dimids[n] = n
1935-
for n from 0 <= n < numdims:
1935+
for n in range(numdims):
19361936
with nogil:
19371937
ierr = nc_inq_dimname(_grpid, dimids[n], namstring)
19381938
_ensure_nc_success(ierr)
@@ -1959,7 +1959,7 @@ cdef _get_grps(group):
19591959
with nogil:
19601960
ierr = nc_inq_grps(_grpid, NULL, grpids)
19611961
_ensure_nc_success(ierr)
1962-
for n from 0 <= n < numgrps:
1962+
for n in range(numgrps):
19631963
with nogil:
19641964
ierr = nc_inq_grpname(grpids[n], namstring)
19651965
_ensure_nc_success(ierr)
@@ -1994,10 +1994,10 @@ cdef _get_vars(group, bint auto_complex=False):
19941994
ierr = nc_inq_varids(_grpid, &numvars, varids)
19951995
_ensure_nc_success(ierr)
19961996
else:
1997-
for n from 0 <= n < numvars:
1997+
for n in range(numvars):
19981998
varids[n] = n
19991999
# loop over variables.
2000-
for n from 0 <= n < numvars:
2000+
for n in range(numvars):
20012001
varid = varids[n]
20022002
# get variable name.
20032003
with nogil:
@@ -3761,7 +3761,7 @@ returns `True` if the `Dimension` instance is unlimited, `False` otherwise."""
37613761
ierr = nc_inq_unlimdims(self._grpid, &numunlimdims, unlimdimids)
37623762
_ensure_nc_success(ierr)
37633763
unlimdim_ids = []
3764-
for n from 0 <= n < numunlimdims:
3764+
for n in range(numunlimdims):
37653765
unlimdim_ids.append(unlimdimids[n])
37663766
free(unlimdimids)
37673767
if dimid in unlimdim_ids:
@@ -4155,7 +4155,7 @@ behavior is similar to Fortran or Matlab, but different than numpy.
41554155
# find dimension ids.
41564156
if ndims:
41574157
dimids = <int *>malloc(sizeof(int) * ndims)
4158-
for n from 0 <= n < ndims:
4158+
for n in range(ndims):
41594159
dimids[n] = dimensions[n]._dimid
41604160
# go into define mode if it's a netCDF 3 compatible
41614161
# file format. Be careful to exit define mode before
@@ -4289,7 +4289,7 @@ behavior is similar to Fortran or Matlab, but different than numpy.
42894289
if grp.data_model != 'NETCDF4': grp._enddef()
42904290
raise ValueError('chunksizes must be a sequence with the same length as dimensions')
42914291
chunksizesp = <size_t *>malloc(sizeof(size_t) * ndims)
4292-
for n from 0 <= n < ndims:
4292+
for n in range(ndims):
42934293
if not dimensions[n].isunlimited() and \
42944294
chunksizes[n] > dimensions[n].size:
42954295
msg = 'chunksize cannot exceed dimension size'
@@ -4813,7 +4813,7 @@ each dimension is returned."""
48134813
ierr = nc_inq_var_chunking(self._grpid, self._varid, &icontiguous, chunksizesp)
48144814
_ensure_nc_success(ierr)
48154815
chunksizes=[]
4816-
for n from 0 <= n < ndims:
4816+
for n in range(ndims):
48174817
chunksizes.append(chunksizesp[n])
48184818
free(chunksizesp)
48194819
if icontiguous:
@@ -5343,7 +5343,7 @@ rename a `Variable` attribute named `oldname` to `newname`."""
53435343
count = [1]*ndims
53445344
startp = <size_t *>malloc(sizeof(size_t) * ndims)
53455345
countp = <size_t *>malloc(sizeof(size_t) * ndims)
5346-
for n from 0 <= n < ndims:
5346+
for n in range(ndims):
53475347
startp[n] = start[n]
53485348
countp[n] = count[n]
53495349
if self.dtype == str: # VLEN string
@@ -5747,7 +5747,7 @@ NC_CHAR).
57475747
def _put(self,ndarray data,start,count,stride):
57485748
"""Private method to put data into a netCDF variable"""
57495749
cdef int ierr, ndims
5750-
cdef npy_intp totelem
5750+
cdef npy_intp totelem, dataelem, i
57515751
cdef size_t *startp
57525752
cdef size_t *countp
57535753
cdef ptrdiff_t *stridep
@@ -5769,7 +5769,7 @@ NC_CHAR).
57695769
startp = <size_t *>malloc(sizeof(size_t) * ndims)
57705770
countp = <size_t *>malloc(sizeof(size_t) * ndims)
57715771
stridep = <ptrdiff_t *>malloc(sizeof(ptrdiff_t) * ndims)
5772-
for n from 0 <= n < ndims:
5772+
for n in range(ndims):
57735773
count[n] = abs(count[n]) # make -1 into +1
57745774
countp[n] = count[n]
57755775
# for neg strides, reverse order (then flip that axis after data read in)
@@ -5834,7 +5834,7 @@ NC_CHAR).
58345834
# each element in struct.
58355835
# allocate struct array to hold vlen data.
58365836
strdata = <char **>malloc(sizeof(char *)*totelem)
5837-
for i from 0<=i<totelem:
5837+
for i in range(totelem):
58385838
strdata[i] = data[i]
58395839
# strides all 1 or scalar variable, use put_vara (faster)
58405840
if sum(stride) == ndims or ndims == 0:
@@ -5855,7 +5855,7 @@ NC_CHAR).
58555855
databuff = PyArray_BYTES(<ndarray>data)
58565856
# allocate struct array to hold vlen data.
58575857
vldata = <nc_vlen_t *>malloc(<size_t>totelem*sizeof(nc_vlen_t))
5858-
for i from 0<=i<totelem:
5858+
for i in range(totelem):
58595859
elptr = (<void**>databuff)[0]
58605860
dataarr = <ndarray>elptr
58615861
if self.dtype != dataarr.dtype.str[1:]:
@@ -5884,7 +5884,8 @@ NC_CHAR).
58845884

58855885
def _get(self,start,count,stride):
58865886
"""Private method to retrieve data from a netCDF variable"""
5887-
cdef int ierr, ndims, totelem
5887+
cdef int ierr, ndims
5888+
cdef npy_intp totelem, i
58885889
cdef size_t *startp
58895890
cdef size_t *countp
58905891
cdef ptrdiff_t *stridep
@@ -5911,7 +5912,7 @@ NC_CHAR).
59115912
startp = <size_t *>malloc(sizeof(size_t) * ndims)
59125913
countp = <size_t *>malloc(sizeof(size_t) * ndims)
59135914
stridep = <ptrdiff_t *>malloc(sizeof(ptrdiff_t) * ndims)
5914-
for n from 0 <= n < ndims:
5915+
for n in range(ndims):
59155916
count[n] = abs(count[n]) # make -1 into +1
59165917
countp[n] = count[n]
59175918
# for neg strides, reverse order (then flip that axis after data read in)
@@ -5980,7 +5981,7 @@ NC_CHAR).
59805981
# use _Encoding attribute to decode string to bytes - if
59815982
# not given, use 'utf-8'.
59825983
encoding = getattr(self,'_Encoding','utf-8')
5983-
for i from 0<=i<totelem:
5984+
for i in range(totelem):
59845985
if strdata[i]:
59855986
data[i] = strdata[i].decode(encoding)
59865987
else:
@@ -6015,7 +6016,7 @@ NC_CHAR).
60156016
_ensure_nc_success(ierr)
60166017
# loop over elements of object array, fill array with
60176018
# contents of vlarray struct, put array in object array.
6018-
for i from 0<=i<totelem:
6019+
for i in range(totelem):
60196020
arrlen = vldata[i].len
60206021
dataarr = numpy.empty(arrlen, self.dtype)
60216022
#dataarr.data = <char *>vldata[i].p
@@ -6253,7 +6254,7 @@ cdef _def_compound(grp, object dt, object dtype_name):
62536254
else: # nested array compound element
62546255
ndims = len(format.shape)
62556256
dim_sizes = <int *>malloc(sizeof(int) * ndims)
6256-
for n from 0 <= n < ndims:
6257+
for n in range(ndims):
62576258
dim_sizes[n] = format.shape[n]
62586259
if format.subdtype[0].kind != 'V': # primitive type.
62596260
try:
@@ -6331,7 +6332,7 @@ cdef _read_compound(group, nc_type xtype, endian=None):
63316332
names = []
63326333
formats = []
63336334
offsets = []
6334-
for nf from 0 <= nf < nfields:
6335+
for nf in range(nfields):
63356336
with nogil:
63366337
ierr = nc_inq_compound_field(_grpid,
63376338
xtype,
@@ -6359,7 +6360,7 @@ cdef _read_compound(group, nc_type xtype, endian=None):
63596360
# if numdims=0, not an array.
63606361
field_shape = ()
63616362
if numdims != 0:
6362-
for ndim from 0 <= ndim < numdims:
6363+
for ndim in range(numdims):
63636364
field_shape = field_shape + (dim_sizes[ndim],)
63646365
free(dim_sizes)
63656366
# check to see if this field is a nested compound type.
@@ -6613,7 +6614,7 @@ cdef _read_enum(group, nc_type xtype, endian=None):
66136614
# loop over members, build dict.
66146615
enum_dict = {}
66156616
enum_val = numpy.empty(1,dt)
6616-
for nmem from 0 <= nmem < nmembers:
6617+
for nmem in range(nmembers):
66176618
with nogil:
66186619
ierr = nc_inq_enum_member(grpid, xtype, nmem, \
66196620
enum_namstring,PyArray_DATA(enum_val))

0 commit comments

Comments
 (0)