Ask Your Question
0

How can I automatically redefine user-defined expressions?

asked 2015-09-24 13:50:50 +0100

stan gravatar image

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!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2015-09-24 13:59:10 +0100

stan gravatar image

Dammit, I should have searched in the python resources! I found a hint here: http://stackoverflow.com/questions/46...

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)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-09-24 13:50:50 +0100

Seen: 273 times

Last updated: Sep 24 '15