博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IronPython tutorial
阅读量:6868 次
发布时间:2019-06-26

本文共 3480 字,大约阅读时间需要 11 分钟。

 

. 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 SystemImports IronPython.HostingPublic Class HelloWorld    Public Shared Sub Main(ByVal args As String())        Python.CreateEngine.CreateScriptSourceFromString("print 'hello world'").Execute    End SubEnd Class
Compile using:
vbc HelloWorld.vb /r:IronPython.dll /r:Microsoft.Scripting.dll /r:Microsoft.Scripting.Core.dll

And again in in IronPython:

import clrclr.AddReference('IronPython')from IronPython.Hosting import Pythonengine = 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
>("adder"); Console.WriteLine(adder(2, 2)); Console.WriteLine(adder(2.0, 2.5)); var myClass = scope.GetVariable
>("MyClass"); var myInstance = myClass("hello"); Console.WriteLine(engine.Operations.GetMember(myInstance, "value")); }}

转载地址:http://klbfl.baihongyu.com/

你可能感兴趣的文章
Java Lambda表达式初探
查看>>
JS 和 HTML 中的单引号与双引号
查看>>
GLSL使用FBO实现MRT(Multiple Render Targets)绘制到多张纹理 【转】
查看>>
诺贝尔文学奖
查看>>
(转)Delphi2009初体验 - 语言篇 - 智能指针(Smart Pointer)的实现
查看>>
分享一个开源的流程图绘制软件--Diagram Designer
查看>>
非典型的scala程序及其编译后的结果
查看>>
Android手势监听类GestureDetector的使用
查看>>
每天学点Python之comprehensions
查看>>
【Dubbo实战】 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务(二)...
查看>>
PostgreSQL 配置远程访问
查看>>
Redis之数据存储结构
查看>>
Android 在已有工程中实现微信图片压缩
查看>>
米哈游贺甲:如何实现次世代卡通渲染效果?
查看>>
Javascript继承6:终极继承者----寄生组合式继承
查看>>
【转】xhEditor技术手册 网页编辑器基础教程
查看>>
又添新枕头...
查看>>
卷积的意义【转】
查看>>
用组策略部署Windows防火墙
查看>>
巧用组策略关闭危险端口
查看>>