From 122323853d5a17fcd0e2aa7dbb3d4fd50223deb5 Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Tue, 19 Dec 2017 08:33:38 -0500 Subject: [PATCH] importers: Prefer using ChainMaps to copying and updating dicts. --- import2ledger/importers/_csv.py | 14 +++++++------- import2ledger/importers/nbpy2017.py | 6 ++---- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/import2ledger/importers/_csv.py b/import2ledger/importers/_csv.py index 5b12d99..754c3fa 100644 --- a/import2ledger/importers/_csv.py +++ b/import2ledger/importers/_csv.py @@ -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) diff --git a/import2ledger/importers/nbpy2017.py b/import2ledger/importers/nbpy2017.py index 9f1bf84..d871fae 100644 --- a/import2ledger/importers/nbpy2017.py +++ b/import2ledger/importers/nbpy2017.py @@ -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)