correction, you'd also name the relationship "_readings", eg. self.sensor._readings
On Thu, Jul 27, 2017 at 4:50 PM, Mike Bayer <mike...@zzzcomputing.com> wrote: > On Thu, Jul 27, 2017 at 4:43 PM, Mike Bayer <mike...@zzzcomputing.com> wrote: >> On Thu, Jul 27, 2017 at 10:50 AM, Ruben Di Battista >> <rubendibatti...@gmail.com> wrote: >>> Hello, I'm trying to figure out a streamlined way to store some children >>> values that are stored in numpy arrays in Python. As example let's assume I >>> have a parent object that is a sensor that has some readings associated to >>> it: >>> >>> class Sensor(object): >>> __tablename__ = 'sensor' >>> id = Column(Integer, primary_key=True), >>> name = Column(String) >>> readings = relationship("Reading", backref="sensor") >>> >>> >>> class Reading(object): >>> __tablename__ = 'reading' >>> id = Column(Integer, primary_key=True), >>> date = Column(DateTime), >>> voltage = Column(Float), >>> value = Column(Float), >>> >>> sensor_id = Column(Integer, ForeignKey('sensor.id')) >>> >>> >>> What I would like to achieve is something like: >>> sensor = Sensor(name='Bedroom Sensor') >>> dates, voltages, values = get_sensor_data_from_somewhere() #<-- This >>> returns three numpy arrays respectively of datetime, float, float types, >>> same len! >>> >>> sensor.readings['date'] = dates >>> sensor.readings['voltage'] = voltages >>> sensor.values['value'] = values > > oh, .readings. Unfortunately you'd need to develop a custom > collection class which achieves this and make it available by a > @property on Sensor: > > class Sensor(...): > @property > def readings(self): > return MagicReadingCollection(self) > > then you do the __getitem__ / __setitem__ thing on MagicReadingCollection: > > class MagicReadingCollection(object): > def __init__(self, sensor): > self.sensor = sensor > > def __setitem__(self, field, values): > start_appending_at = len(self.sensor.readings) > for index, value in enumerate(values): > if index > start_appending_at: > reading = Reading() > self.sensor.readings.append(reading) > else: > reading = self.sensor.readings[index] > setattr(reading, field, value) > > > > > > >>> >>> session.add(sensor) >>> >>> Is this possible somehow? It's similar to the attribute_mapped_collection, >>> but I need to map three different keys to the three attributes of the >>> Reading object. >> >> Application of appropriate __getitem__() and __setitem__() methods on >> your Sensor class would achieve this. >> >> >>> >>> -- >>> SQLAlchemy - >>> The Python SQL Toolkit and Object Relational Mapper >>> >>> http://www.sqlalchemy.org/ >>> >>> To post example code, please provide an MCVE: Minimal, Complete, and >>> Verifiable Example. See http://stackoverflow.com/help/mcve for a full >>> description. >>> --- >>> You received this message because you are subscribed to the Google Groups >>> "sqlalchemy" group. >>> To unsubscribe from this group and stop receiving emails from it, send an >>> email to sqlalchemy+unsubscr...@googlegroups.com. >>> To post to this group, send email to sqlalchemy@googlegroups.com. >>> Visit this group at https://groups.google.com/group/sqlalchemy. >>> For more options, visit https://groups.google.com/d/optout. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.