From 56c5970395263340dcfe03d4f5843bc605c70158 Mon Sep 17 00:00:00 2001 From: Mad Date: Mon, 17 Sep 2018 00:14:40 +0200 Subject: [PATCH] Update csventry.py --- csventry.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/csventry.py b/csventry.py index c6cba52..53562f0 100644 --- a/csventry.py +++ b/csventry.py @@ -1,10 +1,31 @@ import csv +class CSVFieldUnknown(Exception): + def __init__(self, cls, field): + super(CSVFieldUnknown, self).__init__( + "Unknown field %s for class %s" % ( + field, + cls.__name__ + ) + ) + class CSVEntry: + _contents = {} def __init__(self, **kvargs): - assert ( set(kvargs.keys()) - set(self.__class__.fields) ) == set() for field in self.__class__.fields: - setattr(self, field, kvargs[field] or "") + setattr(self, field, "") + for field, val in kvargs.items(): + setattr(self, field, val) + + def __getattr__(self, key): + if key not in self.__class__.fields: + raise CSVFieldUnknown(self.__class__, key) + return self._contents[key] + + def __setattr__(self, key, value): + if key not in self.__class__.fields: + raise CSVFieldUnknown(self.__class__, key) + self._contents[key] = value @classmethod def fromRow(cls, row):