Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #93185 > unrolled thread

Turning string into object (name)

Started byliam.oshea@o2-v2.com
First post2015-06-25 19:51 -0700
Last post2015-06-27 21:51 -0700
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Turning string into object (name) liam.oshea@o2-v2.com - 2015-06-25 19:51 -0700
    Re: Turning string into object (name) Chris Angelico <rosuav@gmail.com> - 2015-06-26 14:00 +1000
      Re: Turning string into object (name) Rustom Mody <rustompmody@gmail.com> - 2015-06-26 21:11 -0700
        Re: Turning string into object (name) Chris Angelico <rosuav@gmail.com> - 2015-06-27 14:30 +1000
          Re: Turning string into object (name) Rustom Mody <rustompmody@gmail.com> - 2015-06-27 21:51 -0700

#93185 — Turning string into object (name)

Fromliam.oshea@o2-v2.com
Date2015-06-25 19:51 -0700
SubjectTurning string into object (name)
Message-ID<3ba959bc-f25f-4655-8c1f-ce2888210e1b@googlegroups.com>
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

[toc] | [next] | [standalone]


#93186

FromChris Angelico <rosuav@gmail.com>
Date2015-06-26 14:00 +1000
Message-ID<mailman.99.1435291217.3674.python-list@python.org>
In reply to#93185
On Fri, Jun 26, 2015 at 12:51 PM,  <liam.oshea@o2-v2.com> 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

[toc] | [prev] | [next] | [standalone]


#93219

FromRustom Mody <rustompmody@gmail.com>
Date2015-06-26 21:11 -0700
Message-ID<37e631f2-16ea-431a-aa18-fb74cdedc278@googlegroups.com>
In reply to#93186
On Friday, June 26, 2015 at 9:30:38 AM UTC+5:30, Chris Angelico wrote:
> 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:

I wonder Chris if you know anything about this [yeah a bit of a hijack]:

There is this git plugin https://github.com/felipec/git-remote-hg
that allows one to bi-direct talk to hg

I tried to add a little better error checking so that it doesnt barf with a backtrace when connecting to git (instead of hg which is the intent):

My modifs: https://github.com/rusimody/git-remote-hg/commit/51cc665eff174e0d6bbf2e1b61ca332af345e76a

I got some response (from github) to the effect that travis-continuous failed 
or some such.
Yeah I know about nothing about github pull requests but it just could be
this -- obsolete except syntax.

Any thoughts? Suggestions?

[toc] | [prev] | [next] | [standalone]


#93220

FromChris Angelico <rosuav@gmail.com>
Date2015-06-27 14:30 +1000
Message-ID<mailman.120.1435379463.3674.python-list@python.org>
In reply to#93219
On Sat, Jun 27, 2015 at 2:11 PM, Rustom Mody <rustompmody@gmail.com> wrote:
> On Friday, June 26, 2015 at 9:30:38 AM UTC+5:30, Chris Angelico wrote:
>> 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:
>
> I wonder Chris if you know anything about this [yeah a bit of a hijack]:

Well.... yes, that's a complete thread hijack. Whatever.

> There is this git plugin https://github.com/felipec/git-remote-hg
> that allows one to bi-direct talk to hg
>
> I tried to add a little better error checking so that it doesnt barf with a backtrace when connecting to git (instead of hg which is the intent):
>
> My modifs: https://github.com/rusimody/git-remote-hg/commit/51cc665eff174e0d6bbf2e1b61ca332af345e76a
>
> I got some response (from github) to the effect that travis-continuous failed
> or some such.
> Yeah I know about nothing about github pull requests but it just could be
> this -- obsolete except syntax.
>
> Any thoughts? Suggestions?

Standard principle when asking for help with a Python program: Quote
the error text! You say you got a response back - quote it!

Standard debugging technique: If you have an idea, try it! Most code
is deterministic enough that you can simply change to the modern
except syntax and try again. Maybe it won't have any effect, but maybe
it'll tell you exactly what the problem is.

Beyond that, I don't think I can much help you.

ChrisA

[toc] | [prev] | [next] | [standalone]


#93266

FromRustom Mody <rustompmody@gmail.com>
Date2015-06-27 21:51 -0700
Message-ID<fadf7b95-a963-4ff3-82a7-8cd8afb335b4@googlegroups.com>
In reply to#93220
On Saturday, June 27, 2015 at 10:01:15 AM UTC+5:30, Chris Angelico wrote:
> On Sat, Jun 27, 2015 at 2:11 PM, Rustom Mody  wrote:
> Beyond that, I don't think I can much help you.

Yeah its more python-OT than I first thought...
Need to go through the ropes of linking github with 
https://travis-ci.org/
etc etc

[O the sweet pleasures of cloud-computing!]

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web