class Gene:
def __init__(self, identifiers, features):
self.identifiers = identifiers
self.features = features
gene = Gene(identifiers={'locus': 'GENE_0001', 'protein_id': 'PROT_0001'},
features={'mRNA': '1..300,400..500', 'CDS': '1..300,400..500'})
class Gene:
id = Column(Integer, primary_key=True)
locus = Column(String)
protein_id = Column(String)
mRNA = Column(String)
CDS = Column(String)
class Gene:
id = Column(Integer, primary_key=True)
identifiers = {
'locus': Column(String),
'protein': Column(String)
}
...
class Location:
def __init__(self, intervals, ...):
self.intervals = intervals
...
@classmethod
def from_flat(cls, flat):
# convert flat location to list of tuples
# e.g. [(1, 300), (400, 500)]
@reconstructor
def init_on_load(self):
self.mRNA = Location.from_flat(self.mRNA)
...
2) Say for the example above I have created a plain Python Location class to represent a location of a feature in self.features:class Location:def __init__(self, intervals, ...):self.intervals = intervals...
@classmethod
def from_flat(cls, flat):
# convert flat location to list of tuples
# e.g. [(1, 300), (400, 500)]This class has a classmethod that takes the stored flat string (e.g. 1..300,400..500) and converts it to a list of interval tuples, and has some extra logic in it for use in my applications. I understand I can use orm.reconstructor to instantiate these Location objects from the stored attributes upon object loading, i.e.@reconstructor
def init_on_load(self):
self.mRNA = Location.from_flat(self.mRNA)
...However, what I don't get is how I should be shuttling changes in these objects back to the database as updates.Is there some way to map this (instantiate Location on loading; mutate Location; convert Location back to flat format to update the Gene attribute)? Do i need to write setter methods for each that directly set the underlying column attribute to the flat representation of the Location?
Any help is appreciated, cheers
--SQLAlchemy -The Python SQL Toolkit and Object Relational MapperTo 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+...@googlegroups.com.To post to this group, send email to sqlal...@googlegroups.com.Visit this group at https://groups.google.com/group/sqlalchemy.To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/f3105bee-b6dc-489c-82e0-c6d36710a462%40googlegroups.com.For more options, visit https://groups.google.com/d/optout.
class View:
def __init__(self, gene, attrs):
self.gene = gene
self.attrs = attrs
def keys(self):
return iter(self.attrs)
def items(self):
return ((attr, getattr(self.gene, attr)) for attr in self.attrs)
def __getitem__(self, key):
return getattr(self.gene, key)
def __setitem__(self, key, value):
setattr(self.gene, key, value)
class Descriptor:
def __init__(self, *attrs):
self.attrs = attrs
def __get__(self, instance, owner):
return View(instance, self.attrs)
def __set__(self, instance, value):
self.attrs = value
class Gene(Base):
__tablename__ = 'Gene'
id = Column(Integer, primary_key=True)
identifiers = Descriptor('locus', 'protein')
features = Descriptor('gene', 'mRNA', 'CDS')
def __init__(self, **kwargs):
for dic in kwargs.values():
for key, value in dic.items():
setattr(self, key, value)
@classmethod
def __declare_first__(cls):
for value in list(cls.__dict__.values()).copy():
if isinstance(value, Descriptor):
for attr in value.attrs:
setattr(cls, key, Column(attr, String))
CREATE TABLE "gene" (
id INTEGER NOT NULL,
PRIMARY KEY (id)
)
CREATE TABLE "gene" (
id INTEGER NOT NULL,
locus VARCHAR,
protein VARCHAR,
gene VARCHAR,
"mRNA" VARCHAR,
"CDS" VARCHAR,
PRIMARY KEY (id)
)
To unsubscribe from this group and stop receiving emails from it, send an email to sqlal...@googlegroups.com.
Simon
--SQLAlchemy -The Python SQL Toolkit and Object Relational MapperTo 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+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.Visit this group at https://groups.google.com/group/sqlalchemy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/CAFHwexdP95%2BWy5-8OjqhoQcYcs8jRT9RFp-G-SCY9fwwO5UoHg%40mail.gmail.com.