importers: Prefer using ChainMaps to copying and updating dicts.
This commit is contained in:
parent
b41a7a99af
commit
122323853d
2 changed files with 9 additions and 11 deletions
|
@ -1,3 +1,4 @@
|
|||
import collections
|
||||
import csv
|
||||
|
||||
class CSVImporterBase:
|
||||
|
@ -27,16 +28,15 @@ class CSVImporterBase:
|
|||
|
||||
def __init__(self, input_file):
|
||||
self.in_csv = csv.DictReader(input_file)
|
||||
self.entry_seed = self.ENTRY_SEED.copy()
|
||||
self.entry_seed = {}
|
||||
|
||||
def __iter__(self):
|
||||
for row in self.in_csv:
|
||||
row_data = self._read_row(row)
|
||||
if row_data is not None:
|
||||
retval = self.entry_seed.copy()
|
||||
retval.update(
|
||||
(entry_key, row[row_key])
|
||||
copied_fields = {
|
||||
entry_key: row[row_key]
|
||||
for row_key, entry_key in self.COPIED_FIELDS.items()
|
||||
)
|
||||
retval.update(row_data)
|
||||
yield retval
|
||||
}
|
||||
yield collections.ChainMap(
|
||||
row_data, copied_fields, self.entry_seed, self.ENTRY_SEED)
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import collections
|
||||
import decimal
|
||||
import functools
|
||||
|
||||
|
@ -110,10 +111,7 @@ class Invoice2017:
|
|||
self.actions.append(action)
|
||||
|
||||
def __iter__(self):
|
||||
for action in self.actions:
|
||||
data = self.base_data.copy()
|
||||
data.update(action)
|
||||
yield data
|
||||
return (collections.ChainMap(act, self.base_data) for act in self.actions)
|
||||
|
||||
|
||||
@functools.lru_cache(5)
|
||||
|
|
Loading…
Reference in a new issue