Update csventry.py
This commit is contained in:
parent
e56c6ca585
commit
56c5970395
1 changed files with 23 additions and 2 deletions
25
csventry.py
25
csventry.py
|
@ -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):
|
||||||
|
|
Loading…
Reference in a new issue