Re: [sqlalchemy] How can I get the field names from an object?
On Mon, Jan 28, 2019 at 5:57 AM wrote: > > > Lets say I have a class > > class Dog(AlchemyBase): > __tablename__ = 'dogs' > name = Column(String, primary_key=True) > color = Column(String) > flees = relationship("Flee", backref="dogs") > > > > How can I get the list of fields ['name', 'color', 'flees'] from the class? > > I'd like to write a generic load(Dog, dict) method, that will create a Dog() > with the field values that exist in dict. > > e.g. > d = {'name: 'snoopy', 'junk': 1} > dog = load(Dog, d) > is equivalent to dog = Dog(name = 'snoopy') > > d1 = {'alias' : 'kitti'} > but load(Cat, d1) > is equivalent to cat = Cat(alias = 'kitti) > > Load will pick field names that exist in the passed Object, and have a value > in dict the names you can get using inspect from sqlalchemy import inspect attr_names = inspect(Dog).attrs.keys() but a typical mapped class can take that dictionary directly in the constructor def load(cls, dict_): return cls(**dict_) is what you describe > > Thanks, > Dan > > -- > 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.
Re: [sqlalchemy] Sqlalchemy queries return unicode instead of str type
This is one of those things that we say you "shouldnt" do. For example it's a non starter in Python 3. So we need to know what the actual problem you need to solve is, as that would determine if/when this bytestring conversion (or perhaps, lack of a Unicode conversion if the issue is performance) takes place. On Mon, Jan 28, 2019, 6:32 AM Hello, > > I'm using sqlalchemy on a PSQL(9.5) DB. When I do a query(below) to the > following table: > > class Molrepo(Base): > > __tablename__ = 'molrepo' > id = Column(Text, primary_key=True) > molrepo_name = Column(Text, index=True) > src_id = Column(Text) > smiles = Column(Text) > inchikey = Column(Text, index=True) > inchi = Column(Text) > > QUERY: > > query.with_entities(Molrepo.molrepo_name, Molrepo.src_id, Molrepo.smiles, > Molrepo.inchikey, Molrepo.inchi).all() > > The returned values are of type *unicode*. I would like that by default > the return values to be of type *str*. How can I do that? The DB client > encoding is already set to 'UTF-8'. > Thanks, > > oriol > > -- > 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.
Re: [sqlalchemy] Question about connection pool by host instead of db
When you say shards it's not clear if you want per-shard connect points or if you are looking for a round robin distribution under one connect point. For the latter case, I did work up a new kind of connection pool which does this, which is at https://bitbucket.org/zzzeek/haalchemy/src/6623f93c122f5e4ac9de208c09512699e30a76f8/haalchemy/clients/sqlalchemy/pool.py?at=master=file-view-default , however the whole haalchemy thing I abansoned explicitly because proxysql exists and solves the same problem. That is, you're using proxysql. Sqlalchemys connection pool is per process no matter what so this means it can't do distributed behaviors without a middle tier. Since proxysql already pools you can just use NullPool on the sqlalchemy side and be done with it. On Mon, Jan 28, 2019, 1:56 AM Carson Ip Hi, is it possible to have the connection pool pooling connections by host > instead of db? i.e. it will change db upon reuse. > > I'm asking because I have a lot of shards (databases) on every MySQL host > and if I create a connection pool size of N, and the host contains M > shards, it will create N*M connections to MySQL. Now that I use ProxySQL to > do connection pooling, the actual number of connections to MySQL is > lowered, but the number of connections to ProxySQL is still N*M, causing > high CPU on ProxySQL due to system call poll and sleep. > > Thanks! > > -- > 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.
[sqlalchemy] How can I get the field names from an object?
Lets say I have a class class Dog(AlchemyBase): __tablename__ = 'dogs' name = Column(String, primary_key=True) color = Column(String) flees = relationship("Flee", backref="dogs") How can I get the list of fields ['name', 'color', 'flees'] from the class? I'd like to write a generic load(Dog, dict) method, that will create a Dog() with the field values that exist in dict. e.g. d = {'name: 'snoopy', 'junk': 1} dog = load(Dog, d) is equivalent to dog = Dog(name = 'snoopy') d1 = {'alias' : 'kitti'} but load(Cat, d1) is equivalent to cat = Cat(alias = 'kitti) Load will pick field names that exist in the passed Object, and have a value in dict Thanks, Dan -- 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] Sqlalchemy queries return unicode instead of str type
Hello, I'm using sqlalchemy on a PSQL(9.5) DB. When I do a query(below) to the following table: class Molrepo(Base): __tablename__ = 'molrepo' id = Column(Text, primary_key=True) molrepo_name = Column(Text, index=True) src_id = Column(Text) smiles = Column(Text) inchikey = Column(Text, index=True) inchi = Column(Text) QUERY: query.with_entities(Molrepo.molrepo_name, Molrepo.src_id, Molrepo.smiles, Molrepo.inchikey, Molrepo.inchi).all() The returned values are of type *unicode*. I would like that by default the return values to be of type *str*. How can I do that? The DB client encoding is already set to 'UTF-8'. Thanks, oriol -- 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.