Inside the Head of PyDanny

Hi, I'm Daniel Roy Greenfeld, and welcome to my blog. I write about Python, Django, and much more.

Parsing MongoDB URI

Rather than hard-code the configuration into a Python based settings file, when using a PaaS such as Heroku you want to pick up the MongoDB URI from the system settings. Here's what I do:

# get the dynamic elements from the MongoURI
import os
import re
r = r'^mongodb\:\/\/(?P<username>[_\w]+):(?P<password>[\w]+)@(?P<host>[\.\w]+):(?P<port>\d+)/(?P<database>[_\w]+)$'
regex = re.compile(r)
mongolab_url = os.environ['MONGOLAB_URI']
match = regex.search(mongolab_url)
data = match.groupdict()

# Save the data to settings
MONGO_HOST = data['host']
MONGO_PORT = int(data['port'])
MONGO_NAME = data['username']
MONGO_DATABASE = data['database']
MONGO_PASSWORD = data['password']

# Connect to MongoEngine
from mongoengine import connect as me_connect
me_connect(
    data['database'], 
    host=data['host'], 
    port=int(data['port']), 
    username=data['username'], 
    password=data['password'])    

Tags: python mongodb howto


Subscribe!

If you read this far, you might want to follow me on twitter or github and subscribe via email below (I'll email you new articles when I publish them).

Email

Comments

Content Copyright © 2012-2018 Daniel Greenfeld. Proudly harnessed by Mountain, powered by Flask, and rendered by Frozen Flask, all of which take great advantage of Python.