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
|
import csv
|
||||||
|
|
||||||
class CSVImporterBase:
|
class CSVImporterBase:
|
||||||
|
@ -27,16 +28,15 @@ class CSVImporterBase:
|
||||||
|
|
||||||
def __init__(self, input_file):
|
def __init__(self, input_file):
|
||||||
self.in_csv = csv.DictReader(input_file)
|
self.in_csv = csv.DictReader(input_file)
|
||||||
self.entry_seed = self.ENTRY_SEED.copy()
|
self.entry_seed = {}
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
for row in self.in_csv:
|
for row in self.in_csv:
|
||||||
row_data = self._read_row(row)
|
row_data = self._read_row(row)
|
||||||
if row_data is not None:
|
if row_data is not None:
|
||||||
retval = self.entry_seed.copy()
|
copied_fields = {
|
||||||
retval.update(
|
entry_key: row[row_key]
|
||||||
(entry_key, row[row_key])
|
|
||||||
for row_key, entry_key in self.COPIED_FIELDS.items()
|
for row_key, entry_key in self.COPIED_FIELDS.items()
|
||||||
)
|
}
|
||||||
retval.update(row_data)
|
yield collections.ChainMap(
|
||||||
yield retval
|
row_data, copied_fields, self.entry_seed, self.ENTRY_SEED)
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import collections
|
||||||
import decimal
|
import decimal
|
||||||
import functools
|
import functools
|
||||||
|
|
||||||
|
@ -110,10 +111,7 @@ class Invoice2017:
|
||||||
self.actions.append(action)
|
self.actions.append(action)
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
for action in self.actions:
|
return (collections.ChainMap(act, self.base_data) for act in self.actions)
|
||||||
data = self.base_data.copy()
|
|
||||||
data.update(action)
|
|
||||||
yield data
|
|
||||||
|
|
||||||
|
|
||||||
@functools.lru_cache(5)
|
@functools.lru_cache(5)
|
||||||
|
|
Loading…
Reference in a new issue