Mercurial > treepkg
changeset 24:beec3ed6e0df
turn the (de)serialize functions of the status fields into methods
author | Bernhard Herzog <bh@intevation.de> |
---|---|
date | Tue, 13 Mar 2007 20:19:48 +0100 |
parents | 7510173b68be |
children | 47d69e5d8ace |
files | treepkg/status.py |
diffstat | 1 files changed, 22 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/treepkg/status.py Tue Mar 13 19:38:22 2007 +0100 +++ b/treepkg/status.py Tue Mar 13 20:19:48 2007 +0100 @@ -15,33 +15,41 @@ # special object to indicate no default value nodefault = object() -def serialize_datetime(d): - return d.strftime("%Y-%m-%d %H:%M:%S") - -def deserialize_datetime(s): - return datetime.datetime(*time.strptime(s.strip(), "%Y-%m-%d %H:%M:%S")[:6]) - class FieldDesc(object): - def __init__(self, serialize, deserialize, default=nodefault): - self.serialize = serialize - self.deserialize = deserialize + def __init__(self, default=nodefault): self.default = default def has_default(self): return self.default is not nodefault + def serialize(self, value): + raise NotImplementedError + + def deserialize(self, string): + raise NotImplementedError + + class StringFieldDesc(FieldDesc): - def __init__(self, **kw): - super(StringFieldDesc, self).__init__(str, lambda s: s.strip(), **kw) + def serialize(self, value): + return str(value) + + def deserialize(self, value): + return value.strip() + class DateFieldDesc(FieldDesc): - def __init__(self, **kw): - super(DateFieldDesc, self).__init__(serialize_datetime, - deserialize_datetime, **kw) + date_format = "%Y-%m-%d %H:%M:%S" + + def serialize(self, value): + return value.strftime(self.date_format) + + def deserialize(self, string): + return datetime.datetime(*time.strptime(string.strip(), + self.date_format)[:6]) class Status(object):