[IronPython] Newbie: convert string to python expression??
Kristian Jaksch
kristian.jaksch at gmail.com
Tue Dec 16 19:45:56 CET 2008
Orestis,
passing the 'context' to 'eval' seems like the right thing to do. I tried
your solution, see below:
*
clr.AddReference("Mapack, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null")
from Mapack import ** * #A library that contain classes for working with
algebra etc.*
.
.
.
*context = {}
def inputBox_KeyDown(s, e):
key = e.Key.value__
result = root.inputBox.Text
if key == 3: #If 'Enter' key is pressed
try:
try:
root.message.Text = eval(result, globals(), locals())
except SyntaxError:
exec(result, globals(), locals())
except Exception, e:
print 'Unhandled exception', e
root.inputBox.Text = "" #Clearing inputBox
*If I then define my matrix (as always): I type *m=Matrix(2,2)* in
'inputBox' and I don't receive any exceptions so I assume the definition is
ok. But if I type *print m* I get an exception: "m is not defined". Why is
that?
Michael,
What do you mean exec the import * inside the context to poulate it?
Something like:
*context['Mapack'] = exec "from Mapack import *" #Doesn't work
*Hmmm... think I'm getting tired.
Anyway thanks for help!
2008/12/16 Michael Foord <fuzzyman at voidspace.org.uk>
> Michael Foord wrote:
>
>> Kristian Jaksch wrote:
>>
>>> *
>>> def inputBox_KeyDown(s, e):
>>> key = e.Key.value__
>>> result = root.inputBox.Text
>>> if key == 3: #If 'Enter' key is pressed
>>> try:
>>> try:
>>> root.message.Text = eval(result)
>>> except SyntaxError:
>>> exec result in context
>>> except Exception, e:
>>> print 'Unhandled exception', e
>>> root.inputBox.Text = "" #Clearing inputBox*
>>>
>>> But this creates a fixed size matrix and I still get an exception if I
>>> type for example *m=Matrix(2,2)* in the 'inputBox'. I want to make this as
>>> general as possible. Can't I place everything that is imported from 'Mapack'
>>> into the 'context' dictionary together with whatever the user types in
>>> besides of that?
>>>
>>
>> You can. I'll give you a clue - import * makes it harder. Try importing
>> Mapack and then it will be easier to put things from the module into the
>> dictionary.
>>
>
> Hmmm - actually you could exec the import * inside the context to populate
> it.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20081216/689e3654/attachment.html>
More information about the Ironpython-users
mailing list