From 9220faf8ca819bc1e1861fba7e3bc199fab49eb7 Mon Sep 17 00:00:00 2001 From: Karim Bahgat Date: Wed, 24 Aug 2016 22:34:52 +0200 Subject: [PATCH 1/3] Fix of records() bug Previous records() speedup forgot that row values had to be parsed from string into their appropriate field types. So moved the record parsing into a separate method and call it where needed. Also forgot that deleteflag is a field when grouping the flat rowlist into rows. Changed to correct length. All doctests pass again after these fixes. --- shapefile.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/shapefile.py b/shapefile.py index f63d9c8f..4f696a65 100644 --- a/shapefile.py +++ b/shapefile.py @@ -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 @@ -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): From 0e881850ea07098b5542c3a72e567151fe74934a Mon Sep 17 00:00:00 2001 From: Karim Bahgat Date: Wed, 24 Aug 2016 22:37:04 +0200 Subject: [PATCH 2/3] Fixed wrong doc for the __recordFmt() method --- shapefile.py | 1 - 1 file changed, 1 deletion(-) diff --git a/shapefile.py b/shapefile.py index 4f696a65..a2387f55 100644 --- a/shapefile.py +++ b/shapefile.py @@ -477,7 +477,6 @@ def __dbfHeader(self): self.__recStruct = Struct(fmt) def __recordFmt(self): - """Calculates the size of a .shp geometry record.""" if not self.numRecords: self.__dbfHeader() fmt = ''.join(['%ds' % fieldinfo[2] for fieldinfo in self.fields]) From 2c8fd1985ec2f87cbe13d90835a7466338a2e77a Mon Sep 17 00:00:00 2001 From: Karim Bahgat Date: Wed, 24 Aug 2016 22:37:49 +0200 Subject: [PATCH 3/3] Fixed wrong doc for the __recordFmt() method --- shapefile.py | 1 + 1 file changed, 1 insertion(+) diff --git a/shapefile.py b/shapefile.py index a2387f55..05fd9523 100644 --- a/shapefile.py +++ b/shapefile.py @@ -477,6 +477,7 @@ def __dbfHeader(self): self.__recStruct = Struct(fmt) def __recordFmt(self): + """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])