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

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

[复制链接]

尚未签到

发表于 2015-4-22 11:09:11 | 显示全部楼层 |阅读模式
在上一篇中,简单介绍了IronPython的相关配置和控制台使用方面的内容,今天继续翻译接下来的内容。Exercise 3: 加载 .NET 库
  IronPython 仅能够直接引用一些最通用的 .NET 库。为了引用其它的 .NET 库, 需要显式引用.
  IronPython维护着一个引用的列表 (请查看在 Task 1 的 clr.References). 为了添加.net 引用,使用
  内置的“clr”模块方法:



  •   clr.AddReference 用于直接添加.NET引用,或指定文件明或编译名称(完整或部分). 这个方法主要提供交
    互性的控索(interactive exploration). 我们推荐在代码模块中,使用如下方法函数, 因为它们会对加载的编
    译库提供更多的控制。


  •   clr.AddReferenceToFile 添加对指定文件的引用,这个方法与加载的编译版本无关。最后, 它不确保正确
    的编译版本被加载。为了确保加载正确的编译版本, 请使用 clr.AddReferenceByName. 而且AddReferenceToFile
    要求编译(库)直接定位在sys.path的路径列表下.


  •   clr.AddReferenceToFileAndPath 提供与AddReferenceToFile相似的功能. 不同之处在于它接受绝对
    路径。并且在加载之前,AddReferenceToFileAndPath 会添加文件路径到sys.path.


  •   clr.AddReferenceByName 添加对指定完整assembly名字的引用。比如:
        'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.


  •   clr.AddReferenceByPartialName 添加对指定“部分”的assembly名称的引用. 这个方法不确保被加载的
    assembly版本正确。使用 clr.AddReferenceByName 添加对指定版本的引用.
Task 1: 添加 System.Xml 引用

  •   在tutorial路径下启动 IronPython 控制台 (see Introduction for details).
  •   为了引用 System.Xml, 首先要引用 Xml 组件到 IronPython.  使用下面代码来添加 System.Xml
    引用(您可在敲入"clr.References" 之前或之后,添加 clr.AddReference 代码看一下其中的变化):
  import clr
  clr.AddReference("System.Xml")
  from System.Xml import *
  dir()
  


  •   注意clr.AddReference 函数即接受 System.Reflection.Assembly 对象,也接受“字符串”做为参数. 符串
    可以是一个完整的编译名称,部分名称或文件名. 为了对对编译引用施加更多的控制, 可适当引用上述函数.
    例如, 考虑如下的交替声明,对应上面的clr.AddReference("System.Xml"):
  clr.AddReferenceByName('System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  clr.AddReferenceByPartialName("System.Xml")

  •   加载XML文件"load.xml" 到 XmlDocument. 这个 xml 文件包括IronPython 示例游戏"Puzzle" 的保存
    数据。为清楚起见,load.xml 文件被放置在Tutorial 路径下
  d = XmlDocument()
  d.Load("load.xml")

  •   我们可以查询该文档。使用如下代码查询保存的“游戏”(数据):
  n = d.SelectNodes("//Puzzle/SavedGames/Game/@caption")
  for e in n: print e.Value
  
  The output in the console window will be:
  >>> n = d.SelectNodes("//Puzzle/SavedGames/Game/@caption")
>>> for e in n: print e.Value
...
  Seattle (default game)
New York
World
North America


  •   (选项) 引用 "xmlutil.py" 模块放在Tutorial 路径下,然后使用模块方法来遍历Xml 文件的内容:
  import xmlutil
  for e in xmlutil.Walk(d): print e.Name, e.Value
  #document None #comment ************************************************************************* * * Copyright (c) Microsoft Corporation.  * * This source code is subject to terms and conditions of the Microsoft Public License. A  * copy of the license can be found in the License.html file at the root of this distribution. If  * you cannot locate the Microsoft Public License, please send an email to  * ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound  * by the terms of the Microsoft Public License. * * You must not remove this notice, or any other, from this software. * * * ***************************************************************************  Puzzle None SavedGames None Game None caption Seattle (default game) type a y 714 x 327 level 11 dimension 3 Game None caption New York type r y 1538 x 1205 level 12 dimension 3 Game None caption World type h y 0 x 0 level 2 dimension 4 Game None caption North America type a x 2 y 5 level 4 dimension 3 TopLeftPreviewTile None x -3 y -3 dimension 3 level 5 Cache None allow true
  
  遍历方法是一个生成器[generator] (Python 方法包含 "yield" 声明). 在遍历方法执行时, 它逐个返回(yields)

  XML 结点数据给调用者[caller]. 遍历代码如下:
  
  def Walk(xml):
    yield xml

    if hasattr(xml, "Attributes"):
        attrs = xml.Attributes
        if attrs:
            for attr in attrs:
                yield attr

    for child in xml:
        for c in Walk(child):
            yield c

  •   按下Ctrl+Z 或 F6 回车键退出python 控制台。
Task 2: Mapack - 加载.NET 库 - AddReferenceToFile
  这个任务要求 Mapack.dll 库来进行线性代数运算(linear algebra computations).  这个库并不是IronPython
发布的一部分. See prerequisites for download details.

  •   启动tutorial 路径下的IronPython 控制台.(see Introduction for details).
  •   使用 clr.AddReferenceToFile 函数加载矩阵[Matrix]库 "Mapack.dll":
  import clr
  clr.AddReferenceToFile("Mapack.dll")
  from Mapack import *
  dir()
  
  控制台显示如下输出:


  >>> import clr
>>> clr.AddReferenceToFile("Mapack.dll")
>>> from Mapack import *
>>> dir()
  ['CholeskyDecomposition', 'EigenvalueDecomposition', 'LuDecomposition', 'Matrix', 'QrDecomposition',
  'SingularValueDecomposition', '__builtins__', '__doc__', '__name__', 'clr']

  •   创建 Matrix 类实例:
  m = Matrix()
  
  哦(Oops), 错误的构造函数参数.  下一步您将会了解构造函数的有效方式.
  
  Traceback (most recent call last):
File , line unknown, in Initialize##30
TypeError: Matrix() takes at least 1 argument (0 given)

  •   使用__doc__ 属性, 查看关于Matrix 构造函数的信息:
  print Matrix.__new__.__doc__
  >>> print Matrix.__new__.__doc__
  __new__(cls, int rows, int columns)
__new__(cls, int rows, int columns, float value)
__new__(cls, Array[Array[float]] value)

  •   通过正确的使用构造函数,并手工正确的设置矩阵数据来创建Matrix 类实例。IronPython 支持.NET类
    自定义索引[custom indexing].

  m = Matrix(2, 2, 1.2)
  n = Matrix(2,1)
  n[0,0] = 4
  print m
  print n
  >>> m = Matrix(2, 2, 1.2)
>>> n = Matrix(2,1)
>>> n[0,0] = 4
>>> print m
  1.2 0
0 1.2  
  >>> print n
  4
0  

  •   (可选项) IronPython 也支持操作符重载.  矩阵重载操作符+, - (二进制和一元) 和 *.  您能看到操作符
    表示 (__add__, __mul__, __sub__, ...) :
  dir(m)


  •   简单的矩阵复数(matrices)运算:
  m * n
  n.Transpose() * m
  m * 3
  n + -n
  
  控制台显示如下输出:
  
  >>> m * n
  Mapack.Matrix
  >>> n.Transpose() * m
  Mapack.Matrix
  >>> m * 3
  Mapack.Matrix
  >>> n + -n
  Mapack.Matrix
  TypeError: Matrix() takes at least 1 argument (0 given)

  •   按下Ctrl+Z 或 F6 回车退出 Python 控制台
Exercise 4: 获取和使用Python 标准库
  在这个练习中,您将获取并指向 Python 标准库.
Task 1: 配置IronPython ,使用Python 标准库

  •   从 http://www.python.org/download/ 下载最新的Python (安装包)installer 并进行安装.  其余的练习
    将假设您已使用默认设置(比如:安装到c:"python25).
  •   创建一个文件,将其命名为"site.py" 。然后将它放在IronPython "Lib" 路径下.  如果它已经存在, 您
    需要编辑它. "site.py" 文件会在每次运行 IronPython 时运行. 告之IronPython (Python)标准类库所在位
    置。您可以添加Python的"lib"路径到IronPython 的路径下. 这样做了之后,将下面代码敲入到"site.py" 文件
    。(用实际路径替换 “c:"python25"lib” 路径):
  import sys
  sys.path.append(r"c:"python25"lib")

  •   在tutorial路径下启动python控制台.(see Introduction for details).
  •   现在您可以在IronPython使用 Python 标准库, 比如获取当前的工作路径 (output uses assumed location
    of your IronPython installation):
  import os
  os.getcwd()
  'C:""ironpython""Tutorial'
  

  
  Tutorial (阶段)总结:
  在这部分tutorial 中您已进行了如下练习.

  •   IronPython 交互控制台
  •   在IronPython中使用标准.net类库
  •   加载.NET 库
  •   获取并使用Python 标准类库
  在本tutorial中, 您已熟悉了IronPython 交互控制台, 包括使用dir()方法动态控索(dynamic exploratory)。
并且使用“__doc__” 属性显示环境.  您也了解了在IronPython中加载和引用.NET 库 (使用引用声明), 创建.NET
类实例(包括泛型类), 调用方法,遍历.NET 集合, 以及重载 .NET 对象重载操作符. 最终, 您还了解了如何访问标准
Python 类库。
  
  好了,今天的内容就先到这里了(其实到这里,还有一半内容)。

      在下一篇中,将会介绍IronPython 高级篇(Advanced )。

运维网声明 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-59566-1-1.html 上篇帖子: [翻译] python Tutorial 之四 下篇帖子: python操作数据库
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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