couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Yoan Blanc" <yoan.bl...@gmail.com>
Subject Re: couchdb-python: looking for the equivalent of Database.exists()
Date Sun, 13 Apr 2008 21:12:00 GMT
Do this work for you?

if dbname in couch:
    db = couch[dbname]
else:
    db = couch.create(dbname)

Cheers,

-- Yoan

On Sun, Apr 13, 2008 at 11:07 PM, Jay Luker <lbjay@reallywow.com> wrote:
> Hi,
>
>  I'm basically wondering if there's a better way to implement this:
>
>  def get_or_make_db(dbname):
>     couch = Server('http://localhost:5984')
>     db = None
>     try:
>         db = couch.create(dbname)
>     except ResourceConflict,e:
>         logger.debug("db %s already existed: %s" % (dbname,e))
>         db = couch[dbname]
>     return db
>
>  Having to catch the ResourceConflict exception seems backwards to me.
>  I came to this solution after my natural inclination didn't seem to
>  behave like I expected. I initially tried this:
>
>  def get_or_make_db(dbname):
>     couch = Server('http://localhost:5984')
>     db = None
>     try:
>         db = couch[dbname]
>     except ResourceNotFound,e:
>         logger.debug("db %s already existed: %s" % (dbname,e))
>         db = couch.create(dbname)
>     return db
>
>  The problem with that is that "db = couch[dbname]" won't throw a
>  ResourceNotFound exception if the database doesn't exist.
>
>  --jay
>

Mime
View raw message