zoukankan      html  css  js  c++  java
  • IronPython tutorial

    . Getting Started

    1.1. Introduction

    The simplest way to get starded with embedding IronPython in your application is to use the Python class defined in IronPython.Hosting. This class includes functions for creating IronPython script engines and accessing commonly used Python-specific functionality. Much of the functionality is exposed as .NET extension methods so that it seamlessly extends the normal DLR hosting API surface.

    Most of the functions in the Python class have overloads that operate on either a ScriptRuntime or a ScriptEngine. These functions provide identical functionality but both forms are provided for convenience for when you may be working with either an engine or an entire runtime.

    1.2. Samples

    1.2.1. Hello World

    The simplest program to start off with is always Hello World. Here is a simple Hello World app that creates a Python engine from C#:

    using System;
    using IronPython.Hosting;
    
    public class HelloWorld {
        public static void Main(string[] args) {
            var engine = Python.CreateEngine();
            engine.CreateScriptSourceFromString("print 'hello world'").Execute();
        }
    }
    Compile using:
    csc HelloWorld.cs /r:IronPython.dll /r:Microsoft.Scripting.dll

    Here’s the equivalent program in VB.NET:

    Imports System
    Imports IronPython.Hosting
    
    Public Class HelloWorld
        Public Shared Sub Main(ByVal args As String())
            Python.CreateEngine.CreateScriptSourceFromString("print 'hello world'").Execute
        End Sub
    End Class
    Compile using:
    vbc HelloWorld.vb /r:IronPython.dll /r:Microsoft.Scripting.dll /r:Microsoft.Scripting.Core.dll

    And again in in IronPython:

    import clr
    clr.AddReference('IronPython')
    from IronPython.Hosting import Python
    
    engine = Python.CreateEngine()
    engine.CreateScriptSourceFromString("print 'hello world'").Execute()

    1.2.2. Calling Python from C#

    This sample demonstrates how to get functions and classes defined in Python and access them from your .NET application.

    In order to get back the results of executed Python code we need to first create a ScriptScope in which we can execute the code. We then execute the code we’re interested inside of that scope and fetch the values back out. To get the values back out we use the GetVariable method defined on ScriptScope. There is both a generic version of this method as well as a non-generic version - here we use the generic version which allows us to perform conversions on the the result of the variable. One of the most convenient conversions for functions and classes is to convert these objects to delegates. This allows us to repeatedly call the same function or class with different arguments.

    C# Example:

    using System;
    using IronPython.Hosting;
    
    public class CallingPython {
        public static void Main(string[] args) {
            var engine = Python.CreateEngine();
            var scope = engine.CreateScope();
            var source = engine.CreateScriptSourceFromString(
                "def adder(arg1, arg2):\n" +
                "   return arg1 + arg2\n"  +
                "\n" +
                "class MyClass(object):\n" +
                "   def __init__(self, value):\n" +
                "       self.value = value\n");
            source.Execute(scope);
    
            var adder = scope.GetVariable<Func<object, object, object>>("adder");
            Console.WriteLine(adder(2, 2));
            Console.WriteLine(adder(2.0, 2.5));
    
            var myClass = scope.GetVariable<Func<object, object>>("MyClass");
            var myInstance = myClass("hello");
    
            Console.WriteLine(engine.Operations.GetMember(myInstance, "value"));
        }
    }
  • 相关阅读:
    Java创建对象的几种方式
    Sqlserver建立Oracle的鏈接服務器
    千万级的大表!MySQL这样优化更好
    ConurrentHashMap和Hashtable的区别
    BTree和B+Tree详解
    为什么MySQL数据库索引选择使用B+树?
    网易跟贴这么火,背后的某个力量不可忽视
    知物由学 | 如何利用人工智能来对抗DDoS攻击?
    揭秘医疗安全防卫战:“我们仍在购买不安全的医疗设备”
    6月第5周业务风控关注 | 《网络安全等级保护条例(征求意见稿)》本周正式发布
  • 原文地址:https://www.cnblogs.com/lexus/p/2816400.html
Copyright © 2011-2022 走看看