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

Reply via email to