Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for records() speedup #67

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions shapefile.py
Expand Up @@ -477,7 +477,7 @@ def __dbfHeader(self):
self.__recStruct = Struct(fmt)

def __recordFmt(self):
"""Calculates the size of a .shp geometry record."""
"""Calculates the format and size of a .dbf record."""
if not self.numRecords:
self.__dbfHeader()
fmt = ''.join(['%ds' % fieldinfo[2] for fieldinfo in self.fields])
Expand All @@ -488,6 +488,9 @@ def __record(self):
"""Reads and returns a dbf record row as a list of values."""
f = self.__getFileObj(self.dbf)
recordContents = self.__recStruct.unpack(f.read(self.__recStruct.size))
return self.__recordTypes(recordContents)

def __recordTypes(self, recordContents):
if recordContents[0] != b(' '):
# deleted record
return None
Expand Down Expand Up @@ -552,8 +555,9 @@ def records(self):
f = self.__getFileObj(self.dbf)
f.seek(self.__dbfHeaderLength())
flat = unpack(self.__recStruct.format * self.numRecords, f.read(self.__recStruct.size * self.numRecords))
rowlen = len(self.fields) - 1
records = list(izip(*(iter(flat),) * rowlen))
rowlen = len(self.fields)
records = (self.__recordTypes(row) for row in izip(*(iter(flat),) * rowlen))
records = [rec for rec in records if rec]
return records

def iterRecords(self):
Expand Down