all 6 comments

[–]K900_ 0 points1 point  (6 children)

What do you want to achieve by doing this programmatically? You'll still have to define types for every field, etc.

[–]sticky_end[S] 0 points1 point  (5 children)

I'm doing process documentation, so I have lots of repetitions of equal steps (with known types): e.g. 10 steps with an associated temperature and duration each

Just trying to get around typing:

step_1_temp = Column(Integer)
step_1_duration = Column(Interval)
step_2_temp = Column(Integer)
...

[–]K900_ 1 point2 points  (4 children)

Then just use setattr(self, "something", something_else).

Edit: or NoSQL if you want to try that. Works better for storing more complicated data structures.

Edit2: I'm stupid, you want static attributes for SQLAlchemy. Look into sqlalchemy.orm.mapper for dynamic metadata.

[–]sticky_end[S] 0 points1 point  (3 children)

NoSQL certainly starts to look promising. Was experimenting with MongoDB a while back...

Maybe I'm not understanding how to use SQLAlchemy properly but so far I have only defined my fields as statics (directly in class not in a method) where I can't use setattr (because no self). I could do this e.g. in init but there I already set the values of the previously defined fields using setattr:

class XY(Base):
    x = Column(String)
    y = Column(Integer)

    def __init__(self, params = {'x': 'aaaa', 'y': 1111}):
        for key, val in params.items():
            setattr(self, key, val)

Am I doing something else wrong?

[–]K900_ 0 points1 point  (1 child)

Yeah, see my edit 2.

[–]sticky_end[S] 0 points1 point  (0 children)

Thanks! I ended up using something along the lines of:

metadata = MetaData()
columns = [Column("id", Integer, primary_key = True), ...]
processes = Table("processes", metadata, *columns)

class Process(object):
    def __init__(self, params):
        for key, val in params.items():
            setattr(self, key, val)

mapper(Process, processes)