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

Kristian Jaksch kristian.jaksch at gmail.com
Tue Dec 16 18:08:07 CET 2008


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


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? 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.

Thanks very much!


2008/12/16 Michael Foord <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
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20081216/bd898f09/attachment.html>


More information about the Ironpython-users mailing list