Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to elegantly check the existence of an object/instance/variable and simultaneously assign it to variable if it exists in python?

I am using SQLAlchemy to populate a database and often I need to check if a orm object exists in a database before processing. This may be an unconventional question, but I found myself encountering this pattern often:

my_object = session.query(SomeObject).filter(some_fiter).first()
if my_object: # Mostly in databases...
    # Juchee it exists
    # process
else:
    # It does not exist. :-(
    my_object = SomeObject()
    # process

What I am dreaming of would be something like:

if my_object = session.query(someObject).blabla.first():
    # if my_object is None this scope is left alone
    # if my_object is not None I can work with my_object here...

I know, that this syntax is wrong, but I wanted to explain, what I mean by this example. Any equivalent way would make me happy.

Is there an elegant python approach for this pattern? This question aims not only at SQLAlchemy, but to each equivalent scenario.

closing my eyes hitting "Post your question" and waiting for the smart people and pythonistas by heart to hunt me down for asking something mayhaps inappropriate ;-)

like image 939
Aufwind Avatar asked Jul 05 '11 19:07

Aufwind


3 Answers

You want to execute a Exist query to be efficient

(ret, ), = Session.query(exists().where(SomeObject.field==value))

Mike Bayer explain it in his blog post:
http://techspot.zzzeek.org/2008/09/09/selecting-booleans/

You can use scalar if you don't want to have a tuple as result:

ret = Session.query(exists().where(SomeObject.field==value)).scalar()
like image 105
Rach Avatar answered Sep 22 '22 09:09

Rach


This has been asked a long time ago but for future visitors a more concise way to check is

 if session.query(model).filter(some_filter).count():
     # do stuff
like image 37
elbear Avatar answered Sep 22 '22 09:09

elbear


wrap it on a function (shamelessly stolen from django get_or_create, this doesnt return a tuple though)

get_or_create(model, **kwargs):
    try:
        # basically check the obj from the db, this syntax might be wrong
        object = session.query(model).filter(**kwargs).first()
        return object
    except DoesNotExistException: # or whatever error/exception it is on SQLA
        object = model()
        # do it here if you want to save the obj to the db
        return object

that's it. to use it:

obj = get_or_create(SomeObject, filters)

change the **kwargs to a simple argument (like some_filters) if you want

try to wrap something you often use (wrap them to functions or classes)

thats only pseudo code, there might be syntax error.

EDIT: emphasize

like image 29
kusut Avatar answered Sep 21 '22 09:09

kusut