Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a6c739a

Browse filesBrowse files
committed
Combine the methods, giving start and stop default values. Remove f.seek(0) before f.seek(..)
1 parent 811d329 commit a6c739a
Copy full SHA for a6c739a

File tree

Expand file treeCollapse file tree

1 file changed

+18
-25
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+18
-25
lines changed

‎shapefile.py

Copy file name to clipboardExpand all lines: shapefile.py
+18-25Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,7 +1325,9 @@ def __restrictIndex(self, i):
13251325
if self.numRecords:
13261326
rmax = self.numRecords - 1
13271327
if abs(i) > rmax:
1328-
raise IndexError("Shape or Record index out of range.")
1328+
raise IndexError(
1329+
"Shape or Record index: %s out of range. Max index: %s" % (i, rmax)
1330+
)
13291331
if i < 0:
13301332
i = range(self.numRecords)[i]
13311333
return i
@@ -1809,41 +1811,32 @@ def records(self, fields=None):
18091811
records.append(r)
18101812
return records
18111813

1812-
def iterRecords(self, fields=None):
1814+
def iterRecords(self, fields=None, start=0, stop=None):
18131815
"""Returns a generator of records in a dbf file.
18141816
Useful for large shapefiles or dbf files.
18151817
To only read some of the fields, specify the 'fields' arg as a
18161818
list of one or more fieldnames.
1817-
"""
1818-
if self.numRecords is None:
1819-
self.__dbfHeader()
1820-
f = self.__getFileObj(self.dbf)
1821-
f.seek(self.__dbfHdrLength)
1822-
fieldTuples, recLookup, recStruct = self.__recordFields(fields)
1823-
for i in xrange(self.numRecords):
1824-
r = self.__record(
1825-
oid=i, fieldTuples=fieldTuples, recLookup=recLookup, recStruct=recStruct
1826-
)
1827-
if r:
1828-
yield r
1829-
1830-
def iterRecords_range(self, start, stop, fields=None):
1831-
"""Returns a generator of records in a dbf file, for a range
1832-
of oid. Useful for large shapefiles or dbf files. To only
1833-
read some of the fields, specify the 'fields' arg as a list of
1834-
one or more fieldnames.
1835-
1819+
By default yields all records. Otherwise, specify start
1820+
(default: 0) or stop (default: number_of_records)
1821+
to only yield record numbers i, where
1822+
start <= i < stop, (or
1823+
start <= i < number_of_records + stop
1824+
if stop < 0).
18361825
"""
18371826
if self.numRecords is None:
18381827
self.__dbfHeader()
18391828
f = self.__getFileObj(self.dbf)
18401829
start = self.__restrictIndex(start)
1841-
if abs(stop) > self.numRecords:
1842-
raise IndexError("Record index out of range.")
1843-
if stop < 0:
1830+
if stop is None:
1831+
stop = self.numRecords
1832+
elif abs(stop) > self.numRecords:
1833+
raise IndexError(
1834+
"abs(stop): %s exceeds number of records: %s."
1835+
% (abs(stop), self.numRecords)
1836+
)
1837+
elif stop < 0:
18441838
stop = range(self.numRecords)[stop]
18451839
recSize = self.__recordLength
1846-
f.seek(0)
18471840
f.seek(self.__dbfHdrLength + (start * recSize))
18481841
fieldTuples, recLookup, recStruct = self.__recordFields(fields)
18491842
for i in xrange(start, stop):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.