20081011 c# ironpython

public void testIronPython1()
        &leftsign;
            Hashtable ht = new Hashtable();
            PythonEngine pe = new PythonEngine();
           
            EngineModule em = pe.CreateModule("foo", false);
            
            //pe.Execute("from System.Collections import *\\r\\nht = &leftsign;\’str1\’ : \’sss1\’ , \’str2\’ : \’sss2\’ &rightsign;\\r\\nret_val = Hashtable( ht )");//"from System.Collections import *\\r\\n ht = &leftsign;\’str1\’ : \’sss1\’ , \’str2\’ : \’sss2\’ &rightsign;\\r\\n ret_val = Hashtable( ht )");
            pe.ExecuteFile("RemoteControlKeyMap.py");
            ht = pe.EvaluateAs<Hashtable>("tt");
            foreach (DictionaryEntry de in ht)
            &leftsign;
                Trace.WriteLine(de.Key.ToString() + "\\t" + de.Value.ToString());
            &rightsign;
//            #C#とIronPython間のオブジェクトI/F
//from System.Collections import *

//ht = &leftsign;\’t\’ : \’sf\’ , \’t1\’ : \’sdfsdf\’ &rightsign;
//ret_val = Hashtable( ht )

//ht2 = &leftsign;\’1\’ : \’234\’ , \’234324\’ : \’s343245434\’ &rightsign;
//tt = Hashtable( ht2 )

 

        &rightsign;

 ~~~~~~~~~~~~~~~

  class ClassA
     &leftsign;
         private int value;
        
         public ClassA()
         &leftsign;
             this.value = 1;
         &rightsign;
 
         public int Value
         &leftsign;
             get &leftsign; return value; &rightsign;
             set &leftsign; this.value = value; &rightsign;
         &rightsign;
     &rightsign;
        static void ProcessByPython(object a)
         &leftsign;
            PythonEngine engine = new PythonEngine();
            EngineModule em = engine.CreateModule("foo", false);

            em.Globals["objectA"] = a;

            engine.ExecuteFile("RemoteControlKeyMap.py", em);
           
        &rightsign;
        [Test]
        public void testIronPython2()
        &leftsign;
            try
            &leftsign;
                ClassA a = new ClassA();
                ProcessByPython(a);
                Console.WriteLine("a.value = " +a.Value);
            &rightsign;
            catch (Exception e)
            &leftsign;
                Console.WriteLine(e);
            &rightsign;
        &rightsign;

def foo(a):
    a.Value = 33333

foo(objectA)
 ~~~~~~~~~~~

using System;
using IronPython.Hosting;

delegate double Example(int p1, int p2);

class CodeSample
&leftsign;
    static void Main()
    &leftsign;
        PythonEngine engine = new PythonEngine();

        // The method body. Notice that the variables used are nStart
        // and nEnd, which are not the parameter names from the delegate
        // type.
        string statements = "total = 0\\n" +
                            "if nStart > 0 and nEnd > nStart:\\n" +
                            "  element = 1\\n" +
                            "  for i in range(2, nStart):\\n" +
                            "    element *= i\\n" +
                            "  for i in range(nStart, nEnd + 1):\\n" +
                            "    element *= i\\n" +
                            "    total += element\\n" +
                            "return total";

        // Specify the variable names that are actually used in the
        // method body.
        string[] paramNames = &leftsign; "nStart", "nEnd" &rightsign;;

        EngineModule testModule = engine.CreateModule("TM", true);

        Example sumNFactorial =
            engine.CreateMethod<Example>(statements, paramNames, testModule);

        Console.WriteLine("Sum of n! for n = 3 to 6: &leftsign;0&rightsign;",
            sumNFactorial(3, 6));
        Console.WriteLine("Sum of n! for n = 5 to 10: &leftsign;0&rightsign;",
            sumNFactorial(5, 10));
    &rightsign;
&rightsign;

/* This code example produces the following output:

Sum of n! for n = 3 to 6: 870
Sum of n! for n = 5 to 10: 4037880
 */

历史博文

标签:, ,
六月 11, 2009 at 4:19 下午 by yippee 1,013 次
Category: Dev
Tags: , ,