Update csventry.py

This commit is contained in:
madmaurice 2018-09-17 00:14:40 +02:00
parent e56c6ca585
commit 56c5970395

View file

@ -1,10 +1,31 @@
import csv 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: class CSVEntry:
_contents = {}
def __init__(self, **kvargs): def __init__(self, **kvargs):
assert ( set(kvargs.keys()) - set(self.__class__.fields) ) == set()
for field in self.__class__.fields: 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 @classmethod
def fromRow(cls, row): def fromRow(cls, row):