Turning string into object (name)

liam.oshea at o2-v2.com liam.oshea at o2-v2.com
Thu Jun 25 22:51:57 EDT 2015


Hi all,
I have something like this:

def dbconn():
	#Establishes connection to local db
	try:
		conn = client()
		db = conn.db_solar #dbname
		collection = db.main # dbcollection / Table
		print "Connected to database successfully!!!"
		return(db,collection)
	except errors.ConnectionFailure, e:
		print "Could not connect to MongoDB: %s" % e 
		sys.exit()

Now I want to remove the hardcoded 'db_solar' and 'main' (from db.main) and load these values from a config file.

Once I have loaded in a string 'db_solar' from a config file how do I use it such that something like db=conn.db_solar will be constructed and run as expected.
Thanks



More information about the Python-list mailing list