<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">
<div>Can any Python gurus help me find a cleaner way to attack a problem? I am of the opinion that <i>anything</i> can be done in Python (though not always cleanly or wisely), but so far how to define variables in the local namespace from a dict has so far
 eluded me. </div>
<div><br>
</div>
<div>Let me set the problem in context, in case there is a more pythonic way to approach this task: I am working on a set of routines that will evaluate an arbitrary expression. At least as I see myself framing the problem right now, I will end up with a str
 with the equation and a dict containing the values for each variable in the expression. I will not know what variable names to expect in the expression, so I must deal with them as strings at run-time. </div>
<div><br>
</div>
<div>I want to evaluate the expression reasonably quickly (as I will later need numerical derivatives). The code fragment below does almost exactly what I want, except the values end up in the module's global namespace, but I would prefer they were in the local
 namespace or that of an object --- so that the 3rd call to evaleq would produce an exception. </div>
<div><br>
</div>
<div>
<div>
<div><font class="Apple-style-span" face="Courier"><span class="Apple-style-span" style="font-size: 11px; ">import numpy as np</span></font></div>
<div><font class="Apple-style-span" face="Courier"><span class="Apple-style-span" style="font-size: 11px; ">def evaleq(eq,vals):</span></font></div>
<div><font class="Apple-style-span" face="Courier"><span class="Apple-style-span" style="font-size: 11px; ">    for k,v in vals.iteritems(): globals()[k] = v</span></font></div>
<div><font class="Apple-style-span" face="Courier"><span class="Apple-style-span" style="font-size: 11px; ">    return 'expression '+eq+' => '+str(eval(eq))</span></font></div>
<div><font class="Apple-style-span" face="Courier"><span class="Apple-style-span" style="font-size: 11px; ">myeq = 'a*np.exp(b)'</span></font></div>
<div><font class="Apple-style-span" face="Courier"><span class="Apple-style-span" style="font-size: 11px; ">myvals = {'a':1, 'b':0}</span></font></div>
<div><font class="Apple-style-span" face="Courier"><span class="Apple-style-span" style="font-size: 11px; ">print evaleq(myeq,myvals)</span></font></div>
<div><font class="Apple-style-span" face="Courier"><span class="Apple-style-span" style="font-size: 11px; ">print evaleq('2*Abc',{'Abc':21.})</span></font></div>
<div><font class="Apple-style-span" face="Courier"><span class="Apple-style-span" style="font-size: 11px; ">print evaleq('2*Abc+a',{'Abc':21.})</span></font></div>
</div>
</div>
<div><br>
</div>
<div>Note that locals() is not supposed to be used in the way that I use globals() above, at least as far as I understand, so that is not an option. </div>
<div><br>
</div>
<div>Any suggestions? </div>
<div><br>
</div>
<div>Brian</div>
<div><br>
</div>
<div><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; ">
<div><span class="Apple-style-span" style="font-size: 12px; "><font class="Apple-style-span" face="Courier">********************************************************************</font></span>
<div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 12px; ">
<font class="Apple-style-span" face="Courier">Brian H. Toby, Ph.D.                            office: 630-252-5488</font></div>
<div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 12px; ">
<font class="Apple-style-span" face="Courier">Senior Physicist/Section Head for Scientific Software</font></div>
<div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 12px; ">
<font class="Apple-style-span" face="Courier">Advanced Photon Source</font></div>
<div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 12px; ">
<font class="Apple-style-span" face="Courier">9700 S. Cass Ave, Bldg. 401/B4192             </font></div>
<div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 12px; ">
<font class="Apple-style-span" face="Courier">Argonne National Laboratory        </font></div>
<div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 12px; ">
<font class="Apple-style-span" face="Courier">Argonne, IL 60439-4856         e-mail: brian dot toby at anl dot gov </font></div>
<div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 12px; ">
<font class="Apple-style-span" face="Courier">********************************************************************</font></div>
<div style="font-size: 12px; "><font class="Apple-style-span" face="Courier">"<span class="Apple-style-span" style="font-family: Helvetica; ">We will restore science to its rightful place, and wield technology's wonders... We will harness the sun and the winds
 and the soil to fuel our cars and run our factories...  All this we can do. All this we will do."</span></font></div>
</div>
</span></div>
<br>
</body>
</html>