[IronPython] IP 2: Execute multiple expression in different scopes

Curt Hagenlocher curt at hagenlocher.org
Tue Aug 19 04:32:22 CEST 2008


I wouldn't say that there's any "right" way to do this, but given the
requirements as stated, I'd probably build a single Python function that
accepts all n attributes and returns m values, where n is the number of
names/values and m is the number of expressions.  So for the code below, I'd
dynamically build a string that looks like this:

def f(a, b, c):
    return [
        a+b,
        sqrt(b*c),
    ]

After executing this code into a ScriptScope, execute the "f" function bound
to your parameters and the you should be able to enumerate over the result
to pull out the individual values.  I'm not sure if you can bind an
IList<object> directly to the function call -- you'd probably have to turn
it into an object[] instead.  And of course, you couldn't use a function
like sqrt without importing it from the math module first.

On Mon, Aug 11, 2008 at 4:41 AM, Christian Schmidt <
christian2.schmidt at gmx.de> wrote:

> Hi,
>
> I'm trying to figure out the best way to embed IP 2 expressions into C# to
> implement the following:
> I have given a scope that consists of an IList<object> and a mapping of the
> list's indices to a name.
> Now I want to evaluate several expressions within this scope.
> After this, the scope changes, but the mapping stays the same, only the
> IList changes. Then again the expressions are evaluated.
>
> What is the right way to do it?
>
> // Input
> IEnumerable<IList<object>> data = ...
> IList<string> names = { "a", "b", "c" };
> IList<string> pyexpressions = { "a+b", "sqrt(b*c)" };
>
> // expected Output
> delegate object MyEval(IList<object> row);
> IList<MyEval> expressions;
>
> // IP 2 magic
> // ???
>
> // Calculate
> foreach(IList<object> row in data)
>  foreach(MyEval expression in expressions)
>    object obj = expression(row);
>    // ...
>
>
> Thanks,
> Christian
> _______________________________________________
> 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/20080818/e5facc17/attachment.html>


More information about the Ironpython-users mailing list