[IronPython] Newbie: convert string to python expression??
Michael Foord
fuzzyman at voidspace.org.uk
Tue Dec 16 19:18:27 CET 2008
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.
Michael
>> 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
>>
>
>
--
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog
More information about the Ironpython-users
mailing list