Re: [PhillyPUG] Python hackery, any better ideas?

From: Kevin H.
Sent on: Thursday, January 12, 2012 1:06 PM
On 01/12/[masked]:53 PM, Mat Schaffer wrote:
> Kevin, your mention of exec is interesting as is execfile. I may be able
> to to use those to make my overrides even less invasive into the
> original code. You meant exec('foo') though right? exec(foo()) gives me
> an error over here.
>
> -Mat

Correct, the first argument should be a string of valid python code.

Something like this:

 >>> QUOTE_ALL = 'Hello'
 >>> print QUOTE_ALL
Hello
 >>> from csv import *
 >>> print QUOTE_ALL
1
 >>> my_vars = {}
 >>> for k,v in locals().items():
 >>>     my_vars[k] = v
 >>> print my_vars['QUOTE_ALL']­
1
 >>> my_vars['QUOTE_ALL']­ = 'Hello'
 >>> exec("print '%s world' % QUOTE_ALL", my_vars)
Hello world

You don't want to modify locals() so you need to copy the values to your 
own dictionary and pass that into exec() instead.

- khill

Our Sponsors

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy