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

Orestis Markou orestis at resolversystems.com
Wed Dec 17 11:14:41 CET 2008


You should definitely use a separate context, or you will get bitten by 
it later on.

To debug similar things, use the tried and true print: print the context 
before you execute something, and after. In your case:

print 'before', context
print eval(result, context)
print 'after', context

Also, in case Silverlight is difficult to debug, you can always rip out 
snippets and use them in either a text file or in the interactive 
interpreter.

Have fun!

Kristian Jaksch wrote:
> Michael,
> 
> Thank you for your help! It finally works. Now there is lots of other 
> things left but this has really helped a lot.
> 
> 
> 
> 2008/12/16 Michael Foord <fuzzyman at voidspace.org.uk 
> <mailto:fuzzyman at voidspace.org.uk>>
> 
>     Kristian Jaksch wrote:
> 
> 
>         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.
> 
>     exec doesn't return anything.
> 
>     Try:
> 
>     *exec "from Mapack import *" in context*
> 
>     It looked like from your response to Orestis that you were moving
>     away from using an explicit context - which I think will get you
>     into confusion with local variables created by your user code.
> 
>     Michael
> 
>         Anyway thanks for help!
> 
> 
> 
>         2008/12/16 Michael Foord <fuzzyman at voidspace.org.uk
>         <mailto:fuzzyman at voidspace.org.uk>
>         <mailto:fuzzyman at voidspace.org.uk
>         <mailto: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.
> 
>         ------------------------------------------------------------------------
> 
>         _______________________________________________
>         Users mailing list
>         Users at lists.ironpython.com <mailto:Users at lists.ironpython.com>
>         http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>          
> 
> 
> 
>     -- 
>     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