from datetime import datetime from sqlalchemy import Column, Integer, Float, DateTime, String, ForeignKey from sqlalchemy.orm import relationship, backref from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class Sensor(Base): __tablename__ = 'sensor' id = Column(Integer, primary_key=True) name = Column(String) description = Column(String, default="") def __init__(self, name, description=""): self.name = name self.description = description def __repr__(self): return "" % (self.id, self.name, self.description) class Value(Base): __tablename__ = 'value' id = Column(Integer, primary_key=True) timestamp = Column(DateTime) sensor_id = Column(Integer, ForeignKey("sensor.id")) sensor = relationship(Sensor, backref=backref('values', order_by=timestamp)) value = Column(Float(precision=4)) def __init__(self, sensor, value, timestamp=datetime.now()): self.sensor = sensor self.value = value self.timestamp = timestamp def __repr__(self): return "" % (self.id, self.sensor, self.value, self.timestamp)