[IronPython] Newbie: convert string to python expression??

Orestis Markou orestis at resolversystems.com
Tue Dec 16 19:08:55 CET 2008


you have to pass the context to eval as well:

eval(result, context)

make sure that the context doesn't change between calls to eval and 
exec. It should be a property on your form, really, but it doesn't seem 
so. Just make sure that you don't re-create the context every time.

If you don't want to setup the context everytime, what you can do is:

eval(result, globals(), locals())
exec(result, globals(), locals())

But this is a major security concern as the user can access (and 
change!) everything that is in every scope. You probably don't want to 
do that. Try printing globals() to see what kind of objects you have 
lying around.



Michael Foord wrote:
> Kristian Jaksch wrote:
>> Ok, did you mean something like 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 = {}
>>
>> context['m'] = Matrix(5,5)    #creating a 5 rows, 5 columns matrix 
>> object in context
>>
>> *
> 
> Looks good.
>> *
>> 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.
>> There are several more classes that must be placed in 'context'. It 
>> would be nice to have the user type *n=m.Transpose()* or whatever 
>> method he wants to get out from the 'Mapack' library and then let the 
>> code be executed correctly.
> 
> Does that not work already - it should do.
> 
> Michael
>>
>> Thanks very much!
>>
>>
>> 2008/12/16 Michael Foord <fuzzyman at voidspace.org.uk 
>> <mailto:fuzzyman at voidspace.org.uk>>:
>> > xkrja wrote:
>> >>
>> >> Thanks!
>> >>
>> >> I'm using Ironpython with Silverlight so I can't get access to
>> >> Windows.Forms
>> >>  >
>> > Ah - in which case it probably lives in System.Windows.Input
>> >>
>> >> Please bear with me :-) New stuff's showing up all the time. The 
>> solution
>> >> you proposed worked but there must be something I don't get with the
>> >> scope.
>> >> Please look at my snippet below:
>> >>
>> >>  >
>> > The advantage of a specific context is that you control what objects 
>> the
>> > code has access to. Try setting the Matrix object into the 
>> dictionary with
>> > the key 'm' and your code should have access to it.
>> >
>> > Michael
>> >
>> >> import clr, sys
>> >> clr.AddReference("Mapack, Version=1.0.0.0, Culture=neutral,
>> >> PublicKeyToken=null")
>> >> from Mapack import *
>> >> .
>> >> .
>> >> .
>> >>
>> >> 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 = ""                         m = 
>> Matrix(2,2)    >>             #NOTICE: If I hard code this
>> >> definition in the function it works
>> >>       print m                            #but if I try to type
>> >> m=Matrix(2,2) in inputBox it says:
>> >>                                            #"Matrix is not defined"
>> >>
>> >>
>> >> I can create an object of the Matrix()-class if I code it straight 
>> into
>> >> the
>> >> function as shown in my snippet above but I can't type 
>> m=Matrix(2,2) in
>> >> the
>> >> 'inputBox' and then execute it. Then I get an exception: "Matrix is 
>> not
>> >> defined".
>> >> How can I work around this?
>> >>
>> >> Thanks very much!
>> >>  >>
>> >> Michael Foord-5 wrote:
>> >>  >>>
>> >>> Oh - and Windows Forms has a Keys enumeration so that you don't 
>> have to
>> >>> rely on the underlying value of the event key property.
>> >>>
>> >>> from System.Windows.Forms import Keys
>> >>>
>> >>> if e.Key == Keys.Enter:
>> >>>
>> >>> (or something like that - check out the MSDN documentation for the
>> >>> enumeration.)
>> >>>
>> >>> Michael Foord
>> >>>
>> >>>
>> >>>    >>
>> >>  >
>> >
>> > --
>> > http://www.ironpythoninaction.com/
>> > http://www.voidspace.org.uk/blog
>> >
>> >
>> > _______________________________________________
>> > Users mailing list
>> > Users at lists.ironpython.com <mailto:Users at lists.ironpython.com>
>> > http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>> >
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>   
> 
> 

-- 
Orestis Markou
Software Engineer,
Resolver Systems Ltd.
orestis at resolversystems.com
+44 (0) 20 7253 6372




More information about the Ironpython-users mailing list