Path: csiph.com!usenet.pasdenom.info!goblin3!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'attributes': 0.07; 'type,': 0.07; '"could': 0.09; 'expected.': 0.09; 'hardcoded': 0.09; 'subject:into': 0.09; 'subject:string': 0.09; 'way:': 0.09; 'cc:addr:python-list': 0.10; 'python': 0.11; 'python.': 0.11; 'exception': 0.13; 'syntax': 0.13; 'def': 0.14; 'suggest': 0.15; 'client()': 0.16; 'conn': 0.16; 'doing,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'subject:object': 0.16; 'syntactic': 0.16; 'try/except': 0.16; 'unambiguous': 0.16; 'wrote:': 0.16; 'string': 0.17; 'attribute': 0.18; 'config': 0.18; 'load': 0.20; 'all,': 0.20; 'variable': 0.20; 'versions': 0.20; 'handling': 0.20; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; '(the': 0.22; '%s"': 0.22; 'terminate': 0.22; 'try:': 0.22; 'code,': 0.23; '2015': 0.23; 'this:': 0.23; 'file.': 0.24; 'header:In-Reply-To:1': 0.24; 'supported': 0.27; 'error': 0.27; 'least': 0.27; 'message- id:@mail.gmail.com': 0.28; 'handled': 0.29; '(which': 0.29; 'function': 0.30; 'connection': 0.30; 'values': 0.30; 'fri,': 0.31; 'print': 0.31; 'code': 0.31; 'run': 0.32; 'gets': 0.32; 'up.': 0.32; 'table': 0.32; 'generally': 0.32; 'usually': 0.33; 'change,': 0.33; 'option.': 0.33; 'file': 0.34; 'received:google.com': 0.34; 'newer': 0.35; 'strongly': 0.35; 'something': 0.35; "isn't": 0.35; 'but': 0.36; 'except': 0.36; 'thanks': 0.36; 'loaded': 0.36; 'subject:: ': 0.37; 'pm,': 0.39; 'does': 0.39; 'skip:e 20': 0.39; 'subject: (': 0.40; 'some': 0.40; 'your': 0.60; 'face': 0.64; '26,': 0.72; "'main'": 0.84; '2.5.': 0.84; 'chrisa': 0.84; 'syntax:': 0.84; 'to:none': 0.90 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=Y5jwli0kDKi3ZuAxqWTRJrU+x+/xbx54LuibMmOuxKI=; b=Hd9xNTPH7GShydNoOJBDoFURpxZUuHt6skl3f1SyOVhFMxVV0EUwQaYQYhUk3dnmrF UxPdGXt+O1hal6Ds1wGZC2VwQRbo4JwNcJbMwuIaAvfJamlyx0pErTA3v16dO7OZjkxW kxzC9kaib2tSYRaii190lOvZKtm9gxtx+5lLvQDCugEeted6T092UKbsicIfBTFRRx6R ZYANbtZlG4OY8EWpmu8lIPGUN6yfLV338xoO7xYFqJQ+t1NKRLt9txkbX54UBXSadLy9 awmXprQEEuc64FFLCFUs5yKaIMczDoT8dwdxmJgoqRhpxQHsimQLu0RNvwq41xQTEPMb lwtQ== MIME-Version: 1.0 X-Received: by 10.50.2.3 with SMTP id 3mr662087igq.34.1435291207339; Thu, 25 Jun 2015 21:00:07 -0700 (PDT) In-Reply-To: <3ba959bc-f25f-4655-8c1f-ce2888210e1b@googlegroups.com> References: <3ba959bc-f25f-4655-8c1f-ce2888210e1b@googlegroups.com> Date: Fri, 26 Jun 2015 14:00:07 +1000 Subject: Re: Turning string into object (name) From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1435291217 news.xs4all.nl 2834 [2001:888:2000:d::a6]:50967 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:93186 On Fri, Jun 26, 2015 at 12:51 PM, wrote: > 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 You can retrieve attributes using strings like this: # Same effect: db = conn.db_solar db = getattr(conn, "db_solar") So a variable attribute name can be handled the same way: db = getattr(conn, dbname) collection = getattr(db, dbcollection) Incidentally, I would suggest not having the try/except at all, since all it does is print an error and terminate (which is the same result you'd get if that error bubbled all the way to top level). But if you are going to use it, then I strongly recommend using the newer syntax: except errors.ConnectionFailure as e: unless you have some reason for supporting ancient versions of Python. For most modern code, you can usually rely on at least 2.7, so the new syntax works; it's unambiguous in the face of multiple-exception handlers, and it works identically on Python 3 (the "except type, name:" syntax isn't supported on Py3). Since it's a trivial syntactic change, there's generally no reason to use the old form, unless you actually need your code to run on Python 2.5. But for what you're doing, chances are you can just let the exception bubble up. That way, a calling function gets the choice of handling it some other way, which currently isn't an option. ChrisA