diff options
author | Yves <yvesf-git@xapek.org> | 2010-06-27 20:25:50 +0200 |
---|---|---|
committer | Yves <yvesf-git@xapek.org> | 2010-06-27 20:25:50 +0200 |
commit | 7a57ef77a3093f480ab414400d44d546f283d81c (patch) | |
tree | bd1a42cf3b4366539ba44c5e70f451f77c05a758 | |
parent | 155abf125c0b40483d404bf7aa10b7800398ac66 (diff) | |
download | ebus-alt-7a57ef77a3093f480ab414400d44d546f283d81c.tar.gz ebus-alt-7a57ef77a3093f480ab414400d44d546f283d81c.zip |
Value class inheritanced by StringValue, IntValue FloatValue
-rw-r--r-- | ebus/__init__.py | 2 | ||||
-rw-r--r-- | 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 "<Sensor('%s','%s','%s')>" % (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 "<Value('%s','%s','%s','%s'>" % (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 + + + + |