From 33f2767c2612bf7caeeda9ee61cc3202dcfade93 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Tue, 29 Dec 2020 12:51:09 -0600 Subject: [PATCH] Do iteration with standard coding idioms --- src/pgwui_core/core.py | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/src/pgwui_core/core.py b/src/pgwui_core/core.py index 99d8cce..0243dea 100644 --- a/src/pgwui_core/core.py +++ b/src/pgwui_core/core.py @@ -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', -- 2.34.1