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

Michael Foord fuzzyman at voidspace.org.uk
Tue Dec 16 20:14:17 CET 2008


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