importers: Prefer using ChainMaps to copying and updating dicts.

This commit is contained in:
Brett Smith 2017-12-19 08:33:38 -05:00
parent b41a7a99af
commit 122323853d
2 changed files with 9 additions and 11 deletions

View file

@ -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)

View file

@ -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)