class DiagnosisDetail(Model):
__tablename__ = 'vw_svc_diagnosis'
diagnosis_id = Column(String(32), primary_key=True)
first_name = Column(String(255))
last_name = Column(String(255))
mrn = Column(String(255))
dx_code = Column(String(255))
dx_id = Column(String(255), ForeignKey('dx_group.dx_id'))
diagnosisgroup = relationship("DiagnosisGroup")
dx_code_type = Column(String(255))
dx_name = Column(String(255))
__mapper_args__ = {
"order_by":[mrn, dx_name]
}
class DiagnosisGroup(Model):
__tablename__ = 'diagnosis_group'
dx_id = Column(String(32), primary_key=True)
mrn = Column(String(255))
dx_code = Column(String(255))
dx_code_type = Column(String(255))
dx_name = Column(String(255))
diagnosis_datetime = Column(DateTime)
__mapper_args__ = {
"order_by":[mrn, dx_name]
}(or a list of them?) (the answer is..sure? just build Python code to generate objects from a list of DiagnosisDetail objects).
I do not wish to do this through any ORM
session queries, since these two classes have distinct use cases where
they bind to wtform views. Thus, I would like to inherit the properties
of these two classes from another distinct class.
I have not been able to find anything like this, short
of [create-column-properties-that-use-a-groupby][1]
<http://stackoverflow.com/questions/25822393/how-can-i-create-column-properties-that-use-a-groupby/25879453>,
but this uses session queries to achieve the result. I would like to
keep everything within the class itself through inheritance of the
DiagnosisDetail class.
You don't need a relational database to do grouping, if you have a list of data in memory it can be grouped using sets, or most succinctly Python's own groupby function: https://docs.python.org/2/library/itertools.html#itertools.groupby
Note: the primary key for DiagnosisGroup, is a concatenation of dx_code
and another field patient_id. Groupings thus are unique.
OK, so
def keyfunc(detail):
return (detail.dx_code, detail.patient_id)
def get_diagnosis_groups(sorted_list_of_diagnosis_detail):
for (dx_code, patient_id), details in itertools.groupby(sorted_list_of_diagnosisdetail, keyfunc):
diagnosis_group = DiagnosisGroup(
dx_code, patient_id
)
diagnosis_group.details = details
for detail in details:
detail.group = diagnosis_group
yield diagnosis_group
I do also need
the FK relation back to the DiagnosisDetail class, which leads me to
believe there should be three classes, where the two above classes
inherit their properties from a parent class.
--
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+unsubscribe@googlegroups.com
<mailto:sqlalchemy+unsubscribe@googlegroups.com>.
To post to this group, send email to sqlal...@googlegroups.com
<mailto: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 a topic in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/t124IuWhNI0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+unsubscribe@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
an email to sqlalchemy+unsubscribe@googlegroups.com
<mailto:sqlalchemy%2Bunsubscrib...@googlegroups.com>
<mailto:sqlalchemy+unsubscribe@googlegroups.com
<mailto:sqlalchemy%2Bunsubscrib...@googlegroups.com>>.
To post to this group, send email to sqlal...@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>
<mailto:sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>>.
Visit this group at https://groups.google.com/group/sqlalchemy
<https://groups.google.com/group/sqlalchemy>.
For more options, visit https://groups.google.com/d/optout
<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
<http://stackoverflow.com/help/mcve> for a full description.
--- You received this message because you are subscribed to a topic
in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/sqlalchemy/t124IuWhNI0/unsubscribe
<https://groups.google.com/d/topic/sqlalchemy/t124IuWhNI0/unsubscribe>.
To unsubscribe from this group and all its topics, send an email to
To post to this group, send email to sqlal...@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy
<https://groups.google.com/group/sqlalchemy>.
For more options, visit https://groups.google.com/d/optout
<https://groups.google.com/d/optout>.
--
Greg M. Silverman
› flora-script <http://flora-script.grenzi.org/> ‹
* *› grenzi.org <http://grenzi.org/> ›
--
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+unsubscribe@googlegroups.com
<mailto:sqlalchemy+unsubscribe@googlegroups.com>.
<mailto: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 a topic in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/t124IuWhNI0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+unsubscribe@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
an email to sqlalchemy+unsubscribe@googlegroups.com
<mailto:sqlalchemy+unsubscribe@googlegroups.com>.
To post to this group, send email to sqlal...@googlegroups.com
<mailto: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 a topic in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/t124IuWhNI0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+unsubscribe@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
class DiagnosisTest(Model):
__tablename__ = 'vw_svc_diagnosis'
#id = Column(Integer, primary_key=True, autoincrement=True)
dx_id = Column(String(32), primary_key=True)
first_name = Column(String(255))
last_name = Column(String(255))
mrn = Column(String(255))
dx_code = Column(String(255))
#dx_code_orig = Column(String(255))
dx_code_type = Column(String(255))
dx_name = Column(String(255))
#chronic_diagnosis_yn = Column(String(255))
diagnosis_datetime = Column(DateTime)
@property
def get_diagnosis_groups(self):
import itertools as itertools
for (dx_code, patient_id), details in itertools.groupby(self, keyfunc):
diagnosis_group = DiagnosisGroup(
dx_code, patient_id
)
diagnosis_group.details = details
for detail in details:
detail.group = diagnosis_group
yield diagnosis_group
Thanks!
Greg--
<mailto:sqlalchemy%2Bunsubscrib...@googlegroups.com>an email to sqlalchemy+unsubscribe@googlegroups.com
<mailto:sqlalchemy+unsubscribe@googlegroups.com
<mailto:sqlalchemy%2Bunsubscrib...@googlegroups.com>>.
To post to this group, send email to sqlal...@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>
<mailto:sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>>.
Visit this group at https://groups.google.com/group/sqlalchemy
<https://groups.google.com/group/sqlalchemy>.
For more options, visit https://groups.google.com/d/optout
<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
<http://stackoverflow.com/help/mcve> for a full description.
--- You received this message because you are subscribed to a topic
in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/sqlalchemy/t124IuWhNI0/unsubscribe
<https://groups.google.com/d/topic/sqlalchemy/t124IuWhNI0/unsubscribe>.
To unsubscribe from this group and all its topics, send an email to
To post to this group, send email to sqlal...@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy
<https://groups.google.com/group/sqlalchemy>.
For more options, visit https://groups.google.com/d/optout
<https://groups.google.com/d/optout>.
--
Greg M. Silverman
› flora-script <http://flora-script.grenzi.org/> ‹
**› grenzi.org <http://grenzi.org/> ›
--
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+unsubscribe@googlegroups.com <mailto:sqlalchemy+unsubscribe@googlegroups.com>.
To post to this group, send email to sqlal...@googlegroups.com <mailto: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 a topic in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/t124IuWhNI0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+unsubscribe@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
<mailto:mike_mp@zzzcomputing.com
<mailto:sqlalchemy%2Bunsubscri...@googlegroups.coman email to sqlalchemy+unsubscribe@googlegroups.com
<mailto:sqlalchemy%2Bunsubscrib...@googlegroups.com>
<mailto:sqlalchemy%252Bunsubscri...@googlegroups.com>>
<mailto:sqlalchemy+unsubscribe...@googlegroups.com
<mailto:sqlalchemy%2Bunsubscrib...@googlegroups.com>
<mailto:sqlalchemy%2Bunsubscri...@googlegroups.com
<mailto:sqlalchemy%252Bunsubscri...@googlegroups.com>>>.
To post to this group, send email to
<mailto:sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>>
<mailto:sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>
<mailto:sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>>>.
To post to this group, send email to
sqlal...@googlegroups.com <mailto:sqlalchemy@googlegroups.com>
<mailto:sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>>.
<https://groups.google.com/d/optout
<https://groups.google.com/d/optout>>.
-- Greg M. Silverman
› flora-script <http://flora-script.grenzi.org/
<http://flora-script.grenzi.org/>> ‹
**› grenzi.org <http://grenzi.org> <http://grenzi.org/> ›
-- 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
<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+unsubscribe@googlegroups.com
<mailto:sqlalchemy%2Bunsubscrib...@googlegroups.com>
<mailto:sqlalchemy+unsubscribe@googlegroups.com
<mailto:sqlalchemy%2Bunsubscrib...@googlegroups.com>>.
<mailto:sqlalchemy@googlegroups.com>
<mailto:sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>>.
Visit this group at https://groups.google.com/group/sqlalchemy
<https://groups.google.com/group/sqlalchemy>.
For more options, visit https://groups.google.com/d/optout
<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
<http://stackoverflow.com/help/mcve> for a full description.
--- You received this message because you are subscribed to a topic
in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/sqlalchemy/t124IuWhNI0/unsubscribe
<https://groups.google.com/d/topic/sqlalchemy/t124IuWhNI0/unsubscribe>.
To unsubscribe from this group and all its topics, send an email to
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy
<https://groups.google.com/group/sqlalchemy>.
For more options, visit https://groups.google.com/d/optout
<https://groups.google.com/d/optout>.
--
Greg M. Silverman
› flora-script <http://flora-script.grenzi.org/> ‹
**› grenzi.org <http://grenzi.org/> ›
--
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+unsubscribe@googlegroups.com <mailto:sqlalchemy+unsubscribe@googlegroups.com>.
To post to this group, send email to sqlal...@googlegroups.com <mailto: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 a topic in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/t124IuWhNI0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+unsubscribe@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.