@@ -1325,7 +1325,9 @@ def __restrictIndex(self, i):
1325
1325
if self .numRecords :
1326
1326
rmax = self .numRecords - 1
1327
1327
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
+ )
1329
1331
if i < 0 :
1330
1332
i = range (self .numRecords )[i ]
1331
1333
return i
@@ -1809,41 +1811,32 @@ def records(self, fields=None):
1809
1811
records .append (r )
1810
1812
return records
1811
1813
1812
- def iterRecords (self , fields = None ):
1814
+ def iterRecords (self , fields = None , start = 0 , stop = None ):
1813
1815
"""Returns a generator of records in a dbf file.
1814
1816
Useful for large shapefiles or dbf files.
1815
1817
To only read some of the fields, specify the 'fields' arg as a
1816
1818
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).
1836
1825
"""
1837
1826
if self .numRecords is None :
1838
1827
self .__dbfHeader ()
1839
1828
f = self .__getFileObj (self .dbf )
1840
1829
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 :
1844
1838
stop = range (self .numRecords )[stop ]
1845
1839
recSize = self .__recordLength
1846
- f .seek (0 )
1847
1840
f .seek (self .__dbfHdrLength + (start * recSize ))
1848
1841
fieldTuples , recLookup , recStruct = self .__recordFields (fields )
1849
1842
for i in xrange (start , stop ):
0 commit comments