人生是一场不能存盘的RPG,我只能尽量多搞几个Screenshot

September 14, 2006

IronPython Python on the .NET Framework笔记

Filed under: .NET

见IronPython: Python on the .NET Framework
http://msdn.microsoft.com/msdntv/episode.aspx?xml=episodes/en/20051110PythonJH/manifest.xml
————————————-
Build IronPython
————————————-
msbuild IronPython.sln
若在vs中build,请把 IronPythonConsole设为startup project.

————————————-
构成
————————————-
ipy.exe IronPython的控制台
ipyw.exe
Iron.Math.dll
Iron.Python.dll
Lib\Site.py

————————————-
IronPython 用到了.net2.0 哪些 Feature?
————————————-
These include Generics, DynamicMethods, new kinds of delegates, and more.

————————————-
Demo中feature的学习
————————————-
1.import Python 库

>>> import sys # 导入sys module;
>>> sys.path.append(”c:/Python24/Lib”) # 增加搜索路径
>>> import this # 导入Python中名为this的module;
>>> import random # 导入Python中名为random的module;
>>> random.__doc__ # 打印模块的document;
>>> cards = range(51) # range方法用于产生51个元素的List
>>> random.shuffle(cards) # 洗牌
>>> cards # 打印洗牌结果

2. 使用.net 类库

>>> import System # import the standard system module
>>> System.Environment.Version # 打印syste version
>>> form System.Math import * # import all the functions from the math module
>>> Sin(PI/2)
>>> form System import Random
>>> r = Random() # Create new instance
>>> [r.Next(0, 100) for i in range(10)] # 生成10个随机数

3. 派生
>>> class MyRandom(Random): pass # pass表示一条空语句
>>> mr = new MyRandow()
>>> [mr.Next(0, 100) for i in range(10)]
>>> mr.foo = 42 # 为实例mr增加了一个名为foo的field
>>> def Sample(self): return 0.5 # 定义了一个名为Sample的function
>>> MyRandom.Sample = Sample # Override Random 的Sample方法
>>> [mr.Next(0,100) for i in range(10)] # 此时由于改写了Sample方法,而Next方法需要依靠Sample方法的返回结果,
# 因此得到值全为50
>>> def Sample(self): return 0.8 + 0.2*super(MyRandom, self).Sample() # 重新定义了Sample function,调用基类方法

4. 使用From,为control定义event handler
>>> from AvalonStartup import *
>>> w = Window()
>>> w.Show()
>>> b = Button(Content=”Click me”)
>>> w.Content = b
>>> def doIt(* args ): print args
>>> b.Click += doIt

4. 使用 XAML
>>> calc = LoadXaml(’calc.xaml’)
>>> w.Content = calc
>>> for node in Walk(calc) : print node

>>> [node for node in Walk(calc) if isinstance(node, Button)] # 判断一个instance 的type
>>> buttons = _
>>> for b in buttons : print b
>>> for b in buttons : b.Background = Brushes.Blue
>>> ss = SpeakchSynthesizer()
>>> def sayIt(b , e) : ss.SpeakTextAsync(b.Name)
>>> for b in buttons : b.Click += sayIt

5. 与C#调用PythonEngine, 在vs中debug .py调本
在xaml文件中写
<Button Content=”Run Script” Click =”RunScript”>

在cs文件中
using IronPython.Hosting;
public void RunScript(object a , object b)
{
PythonEngine engine = new PythonEngine();
engine.SetVariable(”win” , this);
engine.Execute(”win.SetImage(’c:/images/itrun.jpg’)”);

System.Windows.Forms.OpFileDialog ofd = new System.Windows.Forms.OpFileDialog();
ofd.ShowDialog();
eng.RunFile(ofd.FileName); # 执行文件simple.py
}

# simple.py
text = TextBlock(Text=”Hello”, FontSize=100, Forground=Brush.White)
a = DoubleAnination(0.0 , Duration(TimeSpan.FromSeconds(3)))
a.RepeatBehavior = RepeatBehavior(3)
text.BeginAnimation(Shape.OpacityProperty , a)
Canvas.SetLeft(text , 50)
Canvas.SetTop(text , 10)
win.Content.Children[1].Children.Add(text)

由于IronPython代码最终是会转换为IL代码,由.NET Framework运行,故而IronPyton实现了以下功能:

在c#中调用一个.py文件,如果文件中出错,vs可以brack到出错的代码,
.net debugger可以可以在.py文件中加断点,
可以在debug时修改.py中变量的值

6. 在.py脚本中调用COM组件
首先使用.tlbimp生成将COM组件的wrapper
>tlbimp c:\WINDOWS\msagent\agentsvr.exe
生成AgentServerOjbects.dll

# merlin.py
import sys
sys.LoadAssemblyFromFile(AgentServerObjects.dll)
from AgentServerOjbects import *
a = AgentServerClass
id, rast = a.Load(”merlin.acs”)
ch = a.GetCharacter(id)
ch.SetSize(128,128)
ch.Show(0)
ch.MoveTo(600,100,2000)

我使用的是IronPython1.0, 在1.0中,去掉了LoadAssemblyByName和LoadAssemblyFromFile这两个方法.
而使用built-in module:clr来提供Loading .NET libraries的功能:
clr.AddReference
clr.AddReferenceToFile
clr.AddReferenceToFileAndPath
clr.AddReferenceByName
clr.AddReferenceByPartialName

代码修要改为
# merlin.py for IronPython 1.0
import clr
clr.AddReferenceToFileAndPath(’c:\IronPython\AgentServerObjects.dll’)
from AgentServerObjects import *
a = AgentServerClass()
id, rast = a.Load(”merlin.acs”)
ch = a.GetCharacter(id)
ch.SetSize(128,128)
ch.Show(0)
ch.MoveTo(600,100,2000)

Comments »

The URI to TrackBack this entry is: http://recordsome.blogsome.com/2006/09/14/p169/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham