Woot,
I think I just wrote the exact method I was requesting… I
haven’t tested it extensively but it seems to work so far. Here is
an example: My
python script looks like this (MethodTest.py): def Add(s1, s2): return
s1 + s2 And
my C# is like this:
PythonEngine pe = new
PythonEngine();
pe.ExecuteFile("MethodTest.py");
string myString1 = "test1";
string myString2 = "test2";
Console.WriteLine(pe.Evaluate("Add({0}, {1})", myString1, myString2));
Console.WriteLine(pe.Evaluate("Add({0}, {1})", 4, 5)); The
output is then: test1test2 9 As
you can see I’ve added an overload to PythonEngine.Evalute that takes an
extra params array public object
Evaluate(string expr, params
object[] args) {
Dictionary<object,
object> locals = new
Dictionary<object,
object>();
//make sure we have less than 27 args because we are
only using 1 letter variable names
if (args.Length > 27)
return new ArgumentException("You
can only pass up to 27 arguments to this method");
//for each argument, associate it with a one letter
local variable in python
string[] argsVars = new
string[args.Length];
for (int i = 0;
i < args.Length; i++)
{
argsVars[i] = ((char)((int)'a' + i)).ToString(); //"a",
"b", "c", ...
locals.Add(argsVars[i], args[i]);
}
string exprFormat = string.Format(expr,
argsVars);
return Builtin.eval(_module,
exprFormat, Builtin.globals(_module), locals);
} -Brandon
Furtwangler From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Brandon
Furtwangler Is it
possible to call a python method from C# and pass C# variables as the
arguments? For example py:
return person.name If so how
do I call this from C# passing in some C# variable as person? I’m thinking
it should be something like: -Brandon Furtwangler |
_______________________________________________ users mailing list users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com