[IronPython] Evaluating python from within the context of a class

Jonathan Slenders jonathan at slenders.be
Fri Jun 27 19:21:23 CEST 2008


The following works. But I've no idea how to pass params to the member
function. (Anyone: How can you call a function with parameters unpacked from
a tuple??)

But why do you actually want to do this? If the class instance is not
important, just make a static method of this one.

class RT(object):
        def Evaluate(self, code):
                def generateContext():
                        def dropself(f):
                                def g(*p):
                                        f(self)
                                return g
                        context = {}
                        for f in RT.__dict__:
                                context[f] = dropself(RT.__dict__[f])
                        return context

                eval(code, generateContext())

        def test(self):
                print 'RT test'

RT().Evaluate('test()')






2008/6/27 Michael Stephens <falcon at uwyo.edu>:

> Is there anyway cool way you know of to generate the context at runtime
> def Evaluate(_self_,code)
>  exec code in GenerateContext(a)
>
>
> Michael Stephens
>
> Electrical Engineering Graduate Student
> University of Wyoming
> falcon at uwyo.edu or 89iroc at gmail.com
>
> On Fri, Jun 27, 2008 at 11:02 AM, Michael Foord <fuzzyman at voidspace.org.uk>
> wrote:
>
>> Michael Stephens wrote:
>>
>>> I guess I can rebind locals and functions into my own dictionary.. I was
>>> just hoping to avoid that with some snazzy python.
>>>
>>>  I think exec'ing in a custom context is *more* snazzy Python. :-p
>>
>> Michael
>>
>>  Michael Stephens
>>>
>>> Electrical Engineering Graduate Student
>>> University of Wyoming
>>> falcon at uwyo.edu <mailto:falcon at uwyo.edu> or 89iroc at gmail.com <mailto:
>>> 89iroc at gmail.com>
>>>
>>> On Fri, Jun 27, 2008 at 10:58 AM, Michael Foord <
>>> fuzzyman at voidspace.org.uk <mailto:fuzzyman at voidspace.org.uk>> wrote:
>>>
>>>    Michael Stephens wrote:
>>>
>>>        Closer.. but self.__dict__ is empty and cannot find test.  We
>>>        are trying to wire up property expressions in our system.   So
>>>        control.Enable="GetEnabled()" rather than what we have now
>>>        which is control.Enable="currentform.GetEnabled()"
>>>
>>>        I thought I could eval the RHS inside the class to get what i
>>>        wanted but alas no dice.
>>>
>>>
>>>    Well, you could do it in RT.__dict__ - but you would have to make
>>>    test a staticmethod.
>>>
>>>    Why not maintain an explicit dictionary as a context?
>>>
>>>    Michael
>>>
>>>        Michael Stephens
>>>
>>>        Electrical Engineering Graduate Student
>>>        University of Wyoming
>>>        falcon at uwyo.edu <mailto:falcon at uwyo.edu>
>>>        <mailto:falcon at uwyo.edu <mailto:falcon at uwyo.edu>> or
>>>        89iroc at gmail.com <mailto:89iroc at gmail.com>
>>>        <mailto:89iroc at gmail.com <mailto:89iroc at gmail.com>>
>>>
>>>        On Fri, Jun 27, 2008 at 10:46 AM, 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>>> wrote:
>>>
>>>           Michael Stephens wrote:
>>>
>>>               I want to be able to execute arbitrary expressions but have
>>>               their context be within my class.  The end result is
>>>        that RT
>>>               test should be printed and not global test
>>>
>>>               def test():
>>>                print "global test"
>>>
>>>               class RT:
>>>                def test(_self_):
>>>                print 'RT test'
>>>                def Evaluate(_self_,code):
>>>                eval(code)
>>>
>>>
>>>           exec code in self.__dict__
>>>
>>>           Might work.
>>>
>>>           Michael
>>>
>>>
>>>               test()
>>>               a.test()
>>>               a=RT()
>>>               a.Evaluate('test()')
>>>
>>>               Michael Stephens
>>>
>>>               Electrical Engineering Graduate Student
>>>               University of Wyoming
>>>               falcon at uwyo.edu <mailto:falcon at uwyo.edu>
>>>        <mailto:falcon at uwyo.edu <mailto:falcon at uwyo.edu>>
>>>               <mailto:falcon at uwyo.edu <mailto:falcon at uwyo.edu>
>>>        <mailto:falcon at uwyo.edu <mailto:falcon at uwyo.edu>>> or
>>>
>>>               89iroc at gmail.com <mailto:89iroc at gmail.com>
>>>        <mailto:89iroc at gmail.com <mailto:89iroc at gmail.com>>
>>>               <mailto:89iroc at gmail.com <mailto:89iroc at gmail.com>
>>>        <mailto:89iroc at gmail.com <mailto:89iroc at gmail.com>>>
>>>
>>> ------------------------------------------------------------------------
>>>
>>>               _______________________________________________
>>>               Users mailing list
>>>               Users at lists.ironpython.com
>>>        <mailto:Users at lists.ironpython.com>
>>>        <mailto: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/
>>>           http://www.trypython.org/
>>>           http://www.ironpython.info/
>>>           http://www.theotherdelia.co.uk/
>>>           http://www.resolverhacks.net/
>>>
>>>           _______________________________________________
>>>           Users mailing list
>>>           Users at lists.ironpython.com
>>>        <mailto:Users at lists.ironpython.com>
>>>        <mailto: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 <mailto:Users at lists.ironpython.com>
>>>        http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>>
>>>
>>>
>>>    --    http://www.ironpythoninaction.com/
>>>    http://www.voidspace.org.uk/
>>>    http://www.trypython.org/
>>>    http://www.ironpython.info/
>>>    http://www.theotherdelia.co.uk/
>>>    http://www.resolverhacks.net/
>>>
>>>    _______________________________________________
>>>    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/
>> http://www.trypython.org/
>> http://www.ironpython.info/
>> http://www.theotherdelia.co.uk/
>> http://www.resolverhacks.net/
>>
>> _______________________________________________
>> Users mailing list
>> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20080627/6b30b8cb/attachment.html>


More information about the Ironpython-users mailing list