Do iteration with standard coding idioms
authorKarl O. Pinc <kop@karlpinc.com>
Tue, 29 Dec 2020 18:51:09 +0000 (12:51 -0600)
committerKarl O. Pinc <kop@karlpinc.com>
Sat, 2 Jan 2021 19:56:58 +0000 (13:56 -0600)
src/pgwui_core/core.py

index 99d8cce652e80cf57b291c46dbd80b8cc68d1b2a..0243deaca31ff9b4a01846e2362131bb81da903b 100644 (file)
@@ -201,7 +201,8 @@ class LoadedForm(collections.abc.MutableMapping):
         self._fc = fc
 
     def __iter__(self):
-        return next(self._store)
+        for item in self._store:
+            yield item
 
     def __len__(self):
         return len(self._store)
@@ -809,14 +810,9 @@ class DBData(object):
         self.lineno = 0
 
     def __iter__(self):
-        return self
-
-    def __next__(self):
-        '''
-        Iterator to return a thunk which, when called, delivers the
-        next object to be loaded into the db.'''
-        self.lineno += 1
-        return self._thunk()
+        for thunk in self._thunk():
+            self.lineno += 1
+            yield thunk
 
     def _thunk():
         '''
@@ -840,15 +836,9 @@ class SQLData(DBData):
         super(SQLData, self).__init__()
         self.stmts = stmts
 
-        def gen(stmts):
-            for stmt in stmts:
-                yield stmt
-
-        self.stmt_gen = gen(stmts)
-
     def _thunk(self):
-        stmt = next(self.stmt_gen)
-        return lambda: stmt
+        for stmt in self.stmts:
+            yield lambda: stmt
 
 
 class UploadData(DBData):
@@ -951,11 +941,11 @@ class UploadData(DBData):
         Return a thunk which, when called, delivers the
         UploadDataLine of the next line of the uploaded file..
         '''
-        line = next(self._fileo)
-        return lambda: UploadDataLine(line,
-                                      self.lineno,
-                                      self._parser,
-                                      self._mapper)
+        for line in self._fileo:
+            yield lambda: UploadDataLine(line,
+                                         self.lineno,
+                                         self._parser,
+                                         self._mapper)
 
     def _extend(self, line, seq):
         '''Give the list as many elements as there are in the header.
@@ -1295,7 +1285,7 @@ class UploadHandler(SessionDBHandler):
         '''
         response = super(UploadHandler, self).write(result, errors)
         if hasattr(self, 'data'):
-            response['lines'] = self.data.lineno - 1
+            response['lines'] = self.data.lineno
         response['e_cnt'] = len(errors)
         response['db_changed'] = (not response['errors']
                                   and self.uf['action'] != '')
@@ -1320,7 +1310,7 @@ class TabularFileUploadHandler(UploadHandler):
     def cleanup(self):
         '''Finish after processing all lines.'''
         lines = self.ue.data.lineno
-        if lines == 1:
+        if lines == 0:
             raise core_ex.DataLineError(
                 1,
                 'File contains no data',