ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 24 Sep 2015 06:59:10 -0500How can I automatically redefine user-defined expressions?http://ask.sagemath.org/question/29558/how-can-i-automatically-redefine-user-defined-expressions/Dear all,
I imported expressions from different worksheets using load_session() consecutively. Now the problem is that I used different notations in some of them, so in order to unify the notations, I created a dictionary with the equivalent expressions and would like to crawl through all user-defined expressions and redefine them by substituing with the dictionary. Minimal example:
load_session('ws1')
load_session('ws2')
ndict = {}
ndict[P_va] = P_wa
list_names = show_identifiers()
list_eqs = [i for i in list_names if i[0:3] == 'eq_']
for expr1 in list_eqs:
eval(expr1) = eval(expr1).subs(ndict)
> TypeError: eval() arg 1 must be a string or code object
Does anyone have an idea how to do this? Thanks a lot!
Thu, 24 Sep 2015 06:50:50 -0500http://ask.sagemath.org/question/29558/how-can-i-automatically-redefine-user-defined-expressions/Answer by stan for <p>Dear all,
I imported expressions from different worksheets using load_session() consecutively. Now the problem is that I used different notations in some of them, so in order to unify the notations, I created a dictionary with the equivalent expressions and would like to crawl through all user-defined expressions and redefine them by substituing with the dictionary. Minimal example:</p>
<pre><code>load_session('ws1')
load_session('ws2')
ndict = {}
ndict[P_va] = P_wa
list_names = show_identifiers()
list_eqs = [i for i in list_names if i[0:3] == 'eq_']
for expr1 in list_eqs:
eval(expr1) = eval(expr1).subs(ndict)
</code></pre>
<blockquote>
<p>TypeError: eval() arg 1 must be a string or code object</p>
</blockquote>
<p>Does anyone have an idea how to do this? Thanks a lot!</p>
http://ask.sagemath.org/question/29558/how-can-i-automatically-redefine-user-defined-expressions/?answer=29559#post-id-29559Dammit, I should have searched in the python resources! I found a hint here:
http://stackoverflow.com/questions/4687364/assigning-values-to-variables-in-a-list-using-a-loop
Need to change the definition in globals(). The solution to the above problem is then:
load_session('ws1')
load_session('ws2')
ndict = {}
ndict[P_va] = P_wa
list_names = show_identifiers()
list_eqs = [i for i in list_names if i[0:3] == 'eq_']
for eq in list_eqs:
globals()[eq] = eval(eq).subs(ndict)
Thu, 24 Sep 2015 06:59:10 -0500http://ask.sagemath.org/question/29558/how-can-i-automatically-redefine-user-defined-expressions/?answer=29559#post-id-29559