[sqlalchemy] building query using list.

SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL.
This is very powerful tool to handle databases.
I build o/r mapper for some databases to build QSAR models.(can not show code…)
I want to build query using list, and asked google how to do it.
Then, I found “in_” method.

For exmple, using chembldb…
pychembldb is very cool library.
pychembldb

ids = ['1344708', '1273485', '1273478']  # they are "molregno". 
from pychembldb import *
query = chembldb.query(Molecule)
res = query.filter( Molecule.molregno.in_( ids ))
for m in res:
    print m.structure.cannonical_smiles

>    NC(=O)c1cnc(N)c2cc(sc12)c3ccc(cc3)N4CCOCC4
>   CNC(=O)c1cnc(N)c2cc(sc12)c3ccccc3
>    C(CN1CCCC1)Oc2ccc3Nc4nccc(n4)c5coc(COC\C=C\COCc2c3)c5

It’s very easy.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s