You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Jay Luker <lb...@reallywow.com> on 2008/04/13 23:07:30 UTC

couchdb-python: looking for the equivalent of Database.exists()

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

Re: couchdb-python: looking for the equivalent of Database.exists()

Posted by Jay Luker <lb...@reallywow.com>.
Ah, I'd failed to notice that Server implemented __iter__. (Bit of a
python n00b).
Thats perfect - thanks!

--jay

On Sun, Apr 13, 2008 at 5:12 PM, Yoan Blanc <yo...@gmail.com> wrote:
> Do this work for you?
>
>  if dbname in couch:
>     db = couch[dbname]
>  else:
>     db = couch.create(dbname)
>
>  Cheers,
>
>  -- Yoan

Re: couchdb-python: looking for the equivalent of Database.exists()

Posted by Yoan Blanc <yo...@gmail.com>.
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 <lb...@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
>