设为首页 收藏本站
查看: 1436|回复: 0

[经验分享] [翻译] python Tutorial 之一

[复制链接]

尚未签到

发表于 2018-8-8 11:23:07 | 显示全部楼层 |阅读模式
声明:本文做为IronPython-2.0 B3的Tutorial 中文译文,内容全部来自其英文原文,其中本人认为存在疑问的或翻译不当之处会用原文中的内容加以标记,且本文内容完全用于研究和学习IronPython 之用,限于本人英文翻译功底有限,如有错误,欢迎大家批评指正。     原文链接如下:http://www.cnblogs.com/daizhj/articles/1245093.html Introduction
      IronPython 作为 Python 语言在.net平台上的实现(www.python.org),它是一种动态语言且支持许多编程范例[paradigms] ,诸如面向对象,也允许您使用.net代码进行编译。  本tutorial的目标就是使您快速熟悉IronPython 控制台(console),并向您展示如何有
效的使用扩展(extensive).net库。这个 tutorial同时还会展示如何从一些特殊领域入手,这些领域诸如COM的内部操作,用C#扩展 IronPython ,以及嵌入式的IronPython 等。当然本tutorial决不是针对Python语言自身进行介绍,如果您想查阅这方面的资料,我们推荐您去www.python.org 或 看一下由Mark Lutz 与 David Ascher 合著的“Learning Python”  这本书。
  本tutorial所需要的运行环境如下:

  • Microsoft .NET Framework Version 2.0 Redistributable Package (x86)

    • Required to run IronPython.
    • Download from here.
    • 译者注:本人机器上是.net3.5,也可运行部分的示例,所以大家不必非要下载2.0



  • .NET Framework 2.0 Software Development Kit (SDK) (x86)

    • Required for the COM interoperability, extending and embedding tutorials.
    • Download from here.



  • Microsoft WinFX Runtime Components (Avalon)

    • Required for "Advanced IronPython" and "Embedding IronPython" tutorials.
    • Download from here.



  • Mapack (example assembly found on the internet)

    • Required for the "Basic IronPython" tutorial, exercise "Loading .NET Libraries".
    • Download Mapack from here (direct link to the Mapack.zip download is here).
    • Extract Mapack.dll from the zip file directly into the Tutorial directory.

     译者注:如果您本机上安装的是vs2008 RTM(笔者的开发环境),那么你就可以跳过上面的几个安装直接进入到下面的正文了,而如果您安装的是vs2005,那么请您检查相应的条件是否已不足。      本tutorial假设IronPython的发布包已被解压缩至C:\IronPython. 请注意您个人的安装可能会存在一定的差异。     本tutorial假设您将会使用IronPython 控制台功能。将tutorial指导您从tutorial目录启动控制台程序时,您应该定位到tutorial目录下(>cd c:\ironpython\tutorial) ,并运行控制台程序(>..\ipy.exe)。       译者注:ipy.exe可以下面链接中直接下载,我这样做主要是觉得园子里有些朋友可以只是想了解并试用一下,并不想下载那个IronPython的源码包或相应的sdk等资源。  好了,让我们开始正文吧。
Tutorial 1: 基本IronPython应用
      本章节主要强调的是IronPython的interpreter ,以及与.NET 类库的互动场景(environment )  本章节阅读大概需要30分钟
     The objective of this tutorial is to launch the IronPython interpreter, explore the environment of the interactive console and use IronPython to interact with .NET libraries.     The exercises in this tutorial are:

  •     The IronPython interactive console
  •     Using the standard .NET libraries from IronPython
  •     Loading additional .NET libraries
练习 1: IronPython 交互控件台
In this exercise, you will start the IronPython interactive interpreter and perform simple tasks to become acquainted with the IronPython environment. If you are familiar with using the Python interactive console, the import statement and exploring the Python interactive environment using dir() function and __doc__ attribute, you can skip this exercise.任务 1: IronPython console

  •     从本Tutorial的路径下启动IronPython console(c:"ironpython"ipy.exe)。它将如在DOS窗口  下出现如下提示
IronPython 1.0 on .NET 2.0.50727.42  Copyright (c) Microsoft Corporation. All rights reserved.
>>> _

  •     运行下面的内容(在每一条内容之后,按下回车键都会打印(prints)一条结果)。
2+2print "Hello World!"for i in range(3): print ix = 10print x        运行完上面操作后,控制台窗口将会显示如下内容: >>> 2+24>>> print "Hello World!"Hello World!>>> for i in range(3): print i...0  1
  2
>>> x = 10  >>> print x
10>>>

  •     IronPython 控制台支持多行声明(multi-line statements),这经常要进行函数定义:
...       Unlike C# or Java, where blocks of code are grouped by curly brackets "{...}", blocks of code in Python are grouped based on their level of indentation.  Every new block of code must be indented one more level than the previous block of code.  Blocks of code are used for function and>             例如:定义“add”方法(注:你需要在return 之前敲入空格符):      return a + b       要完成函数定义,按下Enter键。 add(3, 2)add("Iron", "Python")       完成这一步后,控制台内容如下: >>> def add(a, b):  ...     return a + b
  ...
  >>> add(3, 2)
5>>> add("Iron", "Python")'IronPython'>>>  

  •     要退出控制台程序,按下Ctrl+Z 并回车即可(alternatively, press F6 followed by Enter).
^ZTask 2: 内置模块和交互式探查(interactive exploration)

  •     从tutorial路径启动IronPython 控制台 (see Introduction for details).
  •     使用内置dir() 函数, 列出IronPython 环境的内容:
dir()        控制台显示如下: >>> dir()['__builtins__', '__doc__', '__name__']

  •     IronPython 有几个内置模块,经常使用的是"sys". 引入"sys" 模块使用 "import" 声明:
import sys

  •     Python 引入声明与 C# 的 "using" 或Visual Basic的"Imports"声明相似。  The important difference is that the C# and VB statements bring the names from the
      imported namespace into the global namespace to be accessed directly. Python’s import
      doesn’t do that.
      要访问引入模块的名称和属性, 使用module名称的前缀:
sys.version

  •     使用dir()方法去显示环境:
dir()改变环境 (global namespace), 现在它包括"sys" 模块:>>> dir()['__builtins__', '__doc__', '__name__', 'sys']

  •     使用dir() 方法来显示"sys" 模块的内容:
dir(sys)

  •     打印出 "sys" 模块属性的一些值:
sys.pathsys.executableTask 3: 外部 Python 模块
本任务使用 "first.py" (文件)模块位于Tutorial 路径.

  •     声明 "first.py" 模块(您可以运行在Tutorial 路径下的ipy.exe,“first”将位于sys.path路径下):
import first

  •     用dir()函数显示"first" 模块:
dir(first)>>> dir(first)['__builtins__', '__file__', '__name__', 'add', 'factorial', 'hi']

  •     为打印 "add" 和 "factorial" 函数文档内容,使用__doc__属性:
first.add.__doc__first.factorial.__doc__        __doc__属性可用于显示 .NET 方法和它们的参数类型。 >>> first.add.__doc__'add(a, b) - returns a + b'>>> first.factorial.__doc__'factorial(n) - returns factorial of n'

  •     调用 "first" 模块方法并打印“hi”属性内容
first.add(1,2)first.factorial(5)first.hi        输出内容为:>>> first.add(1,2)3>>> first.factorial(5)120>>> first.hi'Hello from IronPython!'

  •     在控制台下Ctrl+Z 或 F6 按回车退出。
Exercise 2: 使用标准.NET 库
     IronPython 的能力在于它可以无缝访问.NET 类库。这个练习将会示范如何使用(标准).NET 类库。Task 1: 基本.NET 类库使用

  •     在tutorial路径下启动IronPython 控制台 (see Introduction for details).
  •     使用 "import" 声明, 引入 .NET 系统名空间:
import System

  •     显示System.Environment 类并访问其下面的属性::
dir(System.Environment)System.Environment.OSVersionSystem.Environment.CommandLine        如下是其命令行的输入: >>> dir(System.Environment) >>> System.Environment.OSVersion<System.OperatingSystem object at 0x000000000000002B [Microsoft Windows NT 6.0.6000.0]>>> System.Environment.CommandLine'C:""IronPython""ipy.exe'  

  •     使用“from ... import ..." 引入相应类库声明,并使用dir()显示全局名空间内容。
from System.Math import *dir()       现在您可以调用Math 方法,而不必指定名空间和类前缀:   Sin(PI/2)       显示输出为: >>> from System.Math import *>>> dir()['Abs', 'Acos', 'Asin', 'Atan', 'Atan2', 'BigMul', 'Ceiling', 'Cos','Cosh', 'DivRem', 'E', 'Equals', 'Exp', 'Floor', 'GetHashCode','GetType', 'IEEERemainder', 'Log', 'Log10', 'Max', 'Min', 'PI', 'Pow','Round', 'Sign', 'Sin', 'Sinh', 'Sqrt', 'System', 'Tan', 'Tanh','ToString', 'Truncate', '__builtins__', '__doc__', '__name__']>>> Sin(PI/2)1.0Task 2: 使用.NET 类

  •     声明“System.Collections”名空间到“全局名空间”内:
from System.Collections import *

  •     创建 Hashtable 类实例并使用dir()显示实例的内容:
h = Hashtable()dir(h)

  •     在hash表中插入一些元素:
h["a"] = "IronPython"h["b"] = "Tutorial"         IronPython 支持C# 风格语法来访问hash表元素. 相同的语法可用于支持索引的对象(Arrays, Array lists etc): h["a"]         上面的操作步骤将会显示如下输出: >>> h["a"] = "IronPython"  >>> h["b"] = "Tutorial"
>>> h["a"]'IronPython'

  •     使用声明"for ... in ..." 列举hash表的内容. hash 表元素作为"DictionaryEntry" 类实例.  打印"Key"和"Value"属性:
for e in h: print e.Key, ":", e.Value         控制台显示如下输入:>>> for e in h: print e.Key, ":", e.Value  ...
a : IronPython  b : Tutorial

  •     你可以向python内置列表传递参数来初始化集合类. 您可以通过[1,2,3]来创建指定元素来创  建Python 列表[1,2,3].
      You create tuples by specifying elements in the parentheses: (1,2,3).
l = ArrayList([1,2,3])for i in l: print is = Stack((1,2,3))while s.Count: s.Pop()         输出如下: >>> l = ArrayList([1,2,3])  >>> for i in l: print i
  ...
1  2
  3
>>> s = Stack((1,2,3))  >>> while s.Count: s.Pop()
  ...
3  2
  1
Task 3: 泛型

  •     从System.Collections.Generic名空间中,来声明泛型集合类:
from System.Collections.Generic import *

  •     初始化泛型类实例, 其泛型类型参数必须被指定.  IronPython 使用如下语法来指定类型参数:  generic_type[type_argument, ...] (泛型类型[类型参数]).  创建泛型string列表实例:
l = List[str]()

  •     添加字符串数据到列表中( 因为创建了字符串列表,可以进行添加):
l.Add("Hello")l.Add("Hi")

  •     尝试添加其它类型的数据:
l.Add(3)l.Add(2.5)l.Add([1,2,3])         显然,添加“非字符串”型数据会导致如下“类型错误”输出:>>> l.Add(3)Traceback (most recent call last):  File , line unknown, in Initialize##40
  TypeError: expected str, got int

  •     遍历泛型集合:
for i in l: print iThe output will be:>>> for i in l: print i  ...
Hello  Hi
TypeError: expected str, got int TypeError: expected str, got float TypeError: expected str, got list

  •     按下Ctrl+Z 或 F6 回车,退出IronPython 控制台。
      好了,今天就先翻译到这里了,看了看原文,发现刚翻译了1/4, 看来要再用2-3个篇幅来完成全篇翻译。我  想等全部翻译结束后,将翻译的各篇文章拼揍起来。
感兴趣的朋友可以通过EMAIL或在回复中与我联系。      控制台程序下载链接:http://www.cnblogs.com/files/daizhj/ipy.zip Tag标签: iron python,daizhj

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-548605-1-1.html 上篇帖子: Python---BeautifulSoup 简单的爬虫实例 下篇帖子: Python ATM小程序 v1
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表