From 7a57ef77a3093f480ab414400d44d546f283d81c Mon Sep 17 00:00:00 2001 From: Yves Date: Sun, 27 Jun 2010 20:25:50 +0200 Subject: Value class inheritanced by StringValue, IntValue FloatValue --- ebus/__init__.py | 2 +- ebus/web/model.py | 52 +++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/ebus/__init__.py b/ebus/__init__.py index ad79846..23e6595 100644 --- a/ebus/__init__.py +++ b/ebus/__init__.py @@ -163,7 +163,7 @@ class EbusXMLMixin(object): elif field.tag == "bcd": yield fields.Bcd( name, int(offset) ) elif field.tag == "byteEnum": - options = dict(map(lambda opt: ( int(opt.get("value"),16), opt.get("name")), + options = dict(map(lambda opt: ( int(opt.get("value")[2:],16), opt.get("name")), field.xpath("./option"))) yield fields.ByteEnum( name, int(offset), options) else: diff --git a/ebus/web/model.py b/ebus/web/model.py index e9fc04c..04b15ed 100644 --- a/ebus/web/model.py +++ b/ebus/web/model.py @@ -19,8 +19,7 @@ class Sensor(ModelBase): def __repr__(self): return "" % (self.id, self.name, self.description) - - +# http://www.sqlalchemy.org/docs/05/reference/ext/declarative.html#inheritance-configuration class Value(ModelBase): __tablename__ = 'value' @@ -29,17 +28,52 @@ class Value(ModelBase): sensor_id = Column(Integer, ForeignKey("sensor.id")) sensor = relationship(Sensor, backref=backref('values', order_by=timestamp)) - value_float = Column(Float(precision=4)) - value_int = Column(Integer) - value_string = Column(String) + discriminator = Column('type', String(50)) + __mapper_args__ = {'polymorphic_on': discriminator} - def __init__(self, sensor, value_float=None, value_int=None, value_string=None, timestamp=datetime.now()): + def __init__(self, sensor, timestamp=datetime.now()): self.timestamp = timestamp self.sensor = sensor - self.value_float = value_float - self.value_int = value_int - self.value_string = value_string def __repr__(self): return "" % (self.id, self.sensor, self.value, self.timestamp) + def value(self): + raise NotImplementedException() + +class ValueInt(Value): + __mapper_args__ = {'polymorphic_identity': 'value'} + value_float = Column(Float(precision=4)) + + def __init__(self, sensor, value, timestamp=datetime.now()): + Value.__init__(self, sensor, timestamp) + self.value_float = value + + def value(self): + return self.value_float + +class ValueInt(Value): + __mapper_args__ = {'polymorphic_identity': 'value'} + value_int = Column(Integer) + + def __init__(self, sensor, value, timestamp=datetime.now()): + Value.__init__(self, sensor, timestamp) + self.value_int = value + + def value(self): + return self.value_int + +class ValueString(Value): + __mapper_args__ = {'polymorphic_identity': 'value'} + value_string = Column(String) + + def __init__(self, sensor, value, timestamp=datetime.now()): + Value.__init__(self, sensor, timestamp) + self.value_int = value + + def value(self): + return self.value_string + + + + -- cgit v1.2.1