couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jay Luker" <lb...@reallywow.com>
Subject couchdb-python: looking for the equivalent of Database.exists()
Date Sun, 13 Apr 2008 21:07:30 GMT
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