`
luliangok
  • 浏览: 780347 次
文章分类
社区版块
存档分类
最新评论

(Python编程)集成的方式

 
阅读更多

Programming Python, 3rd Edition 翻译
最新版本见:http://wiki.woodpecker.org.cn/moin/PP3eD

22.2. Integration Modes

22.2. 集成的方式

The last two technical chapters of this book introduce Python's tools for interfacing to the outside world and discuss both its ability to be used as an embedded language tool in other systems and its interfaces for extending Python scripts with new modules and types implemented in C-compatible languages. We'll also briefly explore other integration techniques that are less C specific, such as the Component Object Model (COM) and Jython.

本书最后的这两个技术章节介绍Python与外部语言的接口,不仅讨论Python的内嵌能力,即用作其它系统的内嵌语言工具,也讨论Python的扩展接口,即用C兼容语言实现新的模块和类型。我们也会简要地考察其它非特定于C的集成技术,比如组件对象模型(COM)和Jython.

Our focus in these chapters is on tight integrationwhere control is transferred between languages by a simple, direct, and fast in-process function call. Although it is also possible to link components of an application less directly using Inter-Process Communication (IPC) and networking tools such as sockets and pipes that we explored earlier in the book, we are interested in this part of the book in more direct and efficient techniques.

这两章关注的是紧密的集成,即通过简单,直接,快速的进程内函数调用,使控制在不同语言之间跳转。尽管也可以使用本书前面所述的进程间通信(IPC)和网络工具如socket和管道,不那么直接地,连接应用程序的各个部件,但我们现在感兴趣的是更直接有效的集成技术。

When you mix Python with components written in C (or other compiled languages) either Python or C can be "on top." Because of that, there are two distinct integration modes and two distinct APIs:

当你混合Python和C语言(或其它编译型语言)写的部件,“在上面”的即可以是Python,也可以是C。因此,有两种截然不同的集成方式和两套截然不同的API:

The extending interface
For running compiled C library code from Python programs
The embedding interface
For running Python code from compiled C programs

扩展接口
用于Python程序中运行已编译的C代码库
内嵌接口
用于C程序中运行Python代码

Extending generally has three main roles: to optimize programsrecoding parts of a program in C is a last-resort performance boost; to leverage existing librariesopening them up for use in Python code extends their reach; and to allow Python programs to do things not directly supported by the languagePython code cannot normally access devices at absolute memory addresses, for instance, but can call C functions that do. For example, the NumPy package for Python is largely an instance of extending at work: by integrating optimized numeric libraries, it turns Python into a flexible and efficient system that some compare to Matlab.

扩展方式一般有三个作用:优化程序——用C重写部份程序是最后可用的提升性能的手段;借用现有的库——在Python代码中使用库,扩展它们的能力范围;以及,使Python程序员做到语言本身不直接支持的事——比如,Python代码不能在绝对内存地址常规地访问设备,但是可以调用C函数来做到。如Python的NumPy包,很大程度上就是一个可用的扩展:它通过集成优化的数值库,将Python转变为一个灵活高效的数值系统,几乎可比Matlab.

Embedding typically takes the role of customizationby running user-configurable Python code, a system can be modified without shipping or building its full source code. For instance, some game systems provide a Python customization layer that can be used to modify the game or characters. Embedding is also sometimes used to route events to Python coded handlers. Python GUI toolkits, for example, usually employ embedding.

内嵌方式的典型作用是定制——通过运行用户可配置的Python代码,就可以修改系统,而无需附带或构建所有的源代码。例如,某些游戏提供了Python定制层,可以用来修改游戏或角色。内嵌Python有时也用来传送事件到Python编码的事件处理器。如Python GUI工具包,常常使用内嵌Python。

Figure 22-1 sketches this traditional dual-mode integration model. In extending, control passes from Python through a glue layer on its way to C code. In embedding, C code processes Python objects and runs Python code by calling Python C API functions. In some models, things are not as clear-cut. For example, in the COM and CORBA systems, calls are passed through different kinds of intermediaries. Under cytpes, Python scripts make library calls rather than employing glue code. And in systems such as Pyrex, things are more different still. We will meet such alternative systems later in this part of the book. For now, our focus is on traditional Python/C integration models.

图22-1勾画了这种传统的双模集成方式。扩展方式下,控制从Python经过一个粘合层到达C代码。内嵌方式下,C代码通过调用Python C API函数处理Python对象并运行Python代码。而在有些方式下,事情不是这样清晰。例如,在COM和CORBA系统,调用会在各种不同中间件之间传递。使用ctypes模块时,Python脚本直接调用库函数,而不是通过粘合层。在诸如Pyrex的系统中,事情则更加不同。本书会在以后讲到这样的不同系统。现在,我们关注的是传统的Python/C集成方式。

Figure 22-1. Traditional integration model

图22-1. 传统的集成方式


(Todo: 谁来把图片插上?)



This chapter covers extending, and the next explores embedding. Although we will study these topics in isolation, keep in mind that many systems combine the two techniques. For instance, embedded Python code run from C can also import and call linked-in C extensions to interface with the enclosing application. And in callback-based systems, C code initially accessed through extending interfaces may later use embedding techniques to run Python callback handlers on events.

本章讲扩展方式,下章是内嵌方式。虽然我们分开来研究,但是记住,许多系统会组合运用这两种技术。例如,C语言中内嵌的Python代码可以导入并调用C语言编译的扩展,来与包裹在外面的应用交互。又如,在基于回调的系统中,Python通过扩展接口操纵C代码,使之用内嵌技术运行Python事件回调函数。

For example, when we created buttons with Python's Tkinter GUI library earlier in the book, we called out to a C library through the extending API. When our GUI's user later clicked those buttons, the GUI C library caught the event and routed it to our Python functions with embedding. Although most of the details are hidden to Python code, control jumps often and freely between languages in such systems. Python has an open and reentrant architecture that lets you mix languages arbitrarily.

例如,使用前述的Python Tkinter GUI库,当我们创建按钮时,我们通过扩展API调用了C库。当用户点击按钮,GUI C库触发事件,并传送到我们内嵌的Python函数。虽然大多数细节对Python代码是隐藏的,但在这样的系统中,控制常常是在语言之间自由地跳转。Python具有开放和可重入的体系结构,可以让你任意地混和各种语言。

22.2.1. Presentation Notes

22.2.1. 说明

Before we get into details, I should also mention that Python/C integration is a big topic. In principle, the entire set of extern C functions in the Python system makes up its runtime interface. Because of that, these next two chapters focus on the tools commonly used to implement integration with external componentsjust enough to get you started.

在进入细节以前,我应该指出Python/C集成是一个很大的主题。理论上,Python系统的运行时接口都是由外部C函数组成的。所以,下面两章仅仅是能够让你起步,主要讲实现与外部组件集成的常用工具。

For additional examples beyond this book and its examples distribution, see the Python source code itself; its Modules and Objects directories are a wealth of code resources. Most of the Python built-ins we have used in this bookfrom simple things such as integers and strings to more advanced tools such as files, system calls, Tkinter, and the DBM files underlying shelves. Their utilization of integration APIs can be studied in Python's source code distribution as models for extensions of your own.

除了本书的例子,还可以看Python源码随带发布的一些例子;它的Modules和Objects目录是丰富的代码资源。其中包含我们已使用过的大多数Python的内建模块,从简单的如整数和字符串到更高级的如文件,系统调用,Tkinter和DBM及shelves。阅读Python发布的源码中的模块,学习它们如何使用集成API,可以作为你自己的扩展模块的基础。

In addition, Python's Extending and Embedding and Python/C API manuals are now reasonably complete, and they provide supplemental information to the presentation here. If you plan to do integration, you should browse these as a next step. For example, the manuals go into additional details about C extensions in threaded programs and multiple interpreters in embedded programs, which we will largely finesse here.

另外,有关Python扩展与内嵌及Python/C API的帮助手册现在已相当完善,它们提供了对此处示例的补充信息。如果你要进行集成,下一步你应该浏览那些信息。例如,手册上有关于多线程和多处理器的C语言扩展的额外细节,而我们避开了大部份这些内容。

These chapters also assume that you know basic C programming concepts. If you don't, you won't miss much by skipping or skimming these chapters. Typically, C developers code the extending and embedding interfaces of a system, and others do the bulk of the system's programming with Python alone. But if you know enough about C programming to recognize a need for an extension language, you probably already have the required background knowledge for this chapter.

这两章同时假定你了解基本的C语言编程的概念。如果不是,你可以跳过这两章,也不会有问题。一般是,C开发者对系统的扩展和内嵌接口进行编码,而其他人仅使用Python做系统的主体编程。如果你对C编程很了解,以至于认识到需要一种扩展语言,那么你可能已经具备了本章所要求的背景知识。

The good news in both chapters is that much of the complexity inherent in integrating Python with a static compiled language such as C can be automated with tools in the extension domain and higher-level APIs in the embedding world. For example, in this chapter we begin with a brief look at basic C API use, but then quickly move onto using the automated SWIG integration code generator in three examples and introduce alternatives such as ctypes, Pyrex, and Boost.Python at the end of the chapter. The next chapter takes a similar approach to embedding, with a basic introduction followed by a higher-level library. For now, let's get started with some extending fundamentals.

在这两章中,好消息是,对于Python和静态编译语言如C语言集成的内在复杂性,扩展方面可使用自动化工具,内嵌方面可由高层的API自动处理。例如,本章我们先简单看看基本的C API使用,但是马上转向使用SWIG自动集成代码生成,会举三个实例进行使用,结束时介绍一些其它的工具,如ctypes, Pyrex和Boost.Python。下一章讲内嵌,也是同样,先是基本介绍,然后是高级的库调用。现在,让我们以一些扩展的基本原理开始吧。
分享到:
评论

相关推荐

    python编程基础周志化课后答案2019.docx

    python编程基础周志化课后答案2019全文共9页,当前为第1页。python编程基础周志化课后答案2019全文共9页,当前为第1页。python编程基础周志化课后答案2019 python编程基础周志化课后答案2019全文共9页,当前为第1页...

    Python 编程入门经典

    《Python编程入门经典》(作者James Payne)主要介绍Python 3.1。Python 3.1发布于2009年,是Python程序语言的最新主版本。由于Python是一门跨平台的语言,本书中的内容和示例适用于任何平台(除非特别指出的例外情况)...

    Python编程入门经典高清中文版.pdf

    《Python编程入门经典》(作者James Payne)主要介绍Python 3.1。Python 3.1发布于2009年,是Python程序语言的最新主版本。由于Python是一门跨平台的语言,本书中的内容和示例适用于任何平台(除非特别指出的例外情况)...

    Python编程入门经典

    13.1 Python的GUI编程工具箱 213 13.2 Tkinter简介 215 13.3 用Tkinter创建GUI 小组件 215 13.3.1 改变小组件的尺寸 215 13.3.2 配置小组件选项 216 13.3.3 使用小组件 217 13.3.4 创建布局 217 13.3.5 填充顺序 218...

    wingide--基于Python编程语言开发的集成开发环境

    wingide--基于Python编程语言开发的集成开发环境

    Python编程语言的基础入门教程.docx

    Python编程语言的基础入门教程全文共6页,当前为第1页。Python编程语言的基础入门教程全文共6页,当前为第1页。Python编程语言的基础入门教程 Python编程语言的基础入门教程全文共6页,当前为第1页。 Python编程语言...

    python编程技术讲解(1).docx

    python编程技术讲解(1)全文共2页,当前为第1页。python编程技术讲解(1)全文共2页,当前为第1页。python编程技术讲解 python编程技术讲解(1)全文共2页,当前为第1页。 python编程技术讲解(1)全文共2页,当前为第1页。...

    python编程:第1课-初识Python.pptx

    初识Python python编程:第1课-初识Python全文共16页,当前为第1页。 知识回顾 Python是一种跨平台、开源、解释型的高级编程语言。 1.语法简单。 2.黏性扩展。 3.类库丰富。 代码简洁 "胶水语言" 集百家所长 python...

    Python核心编程第二版

    很不错的python书 第1部分 Python核心  第1章 欢迎来到Python世界   1.1 什么是Python   1.2 起源   1.3 特点   1.3.1 高级   1.3.2 面向对象   1.3.3 可升级   1.3.4 可扩展   1.3.5 可...

    Gridstudio是一个基于Web的电子表格应用完全集成了Python编程语言

    Grid studio是一个基于Web的电子表格应用,完全集成了Python编程语言

    python计算机视觉编程

    4.4.4 综合集成 4.4.5 载入模型 练习 第5章 多视图几何 5.1 外极几何 5.1.1 一个简单的数据集 5.1.2 用matplotlib绘制三维数据 5.1.3 计算f:八点法 5.1.4 外极点和外极线 5.2 照相机和三维结构...

    PYTHON 面向对象编程指南

    Python面向对象编程指南深入介绍Python语言的面向对象特性,全书分3个部分共18章。第1部分讲述用特殊方法实现Python风格的类,分别介绍了__init__()方法、与Python无缝集成—基本特殊方法、属性访问和特性及修饰符、...

    Python面向对象编程指南

    第1部分讲述用特殊方法实现Python风格的类,分别介绍了__init__()方法、与Python无缝集成—基本特殊方法、属性访问和特性及修饰符、抽象基类设计的一致性、可调用对象和上下文的使用、创建容器和集合、创建数值类型...

    Python快速编程入门习题参考答案.docx

    Python快速编程入门习题参考答案 Python快速编程入门习题参考答案全文共40页,当前为第1页。Python快速编程入门习题参考答案全文共40页,当前为第1页。...( ) PyCharm是Python的集成开发环境。( ) 模块

    10个好用的Python集成开发环境.docx

    Python IDE工具是每个Python工程师必须使用的开发工具,选择正确的编辑器对Python编程效率的影响是非常大的,因此选择合适的Python开发工具十分重要,以下是通过长期实践发掘的好用的Python IDE,它们功能丰富,...

    基于Python编程语言下的简单小游戏猜谜语游戏源码

    开发工具:Python编程语言,可以使用任何文本编辑器或集成开发环境(IDE)如PyCharm、Visual Studio Code等。通过运行以上代码,玩家可以开始猜谜语游戏,挑战自己的智力和想象力。 适合人群:具备一定编程基础,...

    [Python高级编程].(法)莱德.扫描版.part1.rar

    《Python高级编程》通过大量的实例,介绍了Python语言的最佳实践和敏捷开发方法,并涉及整个软件生命周期的高级主题,诸如持续集成、版本控制系统、包的发行和分发、开发模式、文档编写等。《Python高级编程》首先...

    Python高级编程.zip

    《Python高级编程》通过大量的实例,介绍了Python语言的最佳实践和敏捷开发方法,并涉及整个软件生命周期的高级主题,诸如持续集成、版本控制系统、包的发行和分发、开发模式、文档编写等。《Python高级编程》首先...

    Thonny IDE,用于ESP32 Python编程

    Thonny是一个专为Python初学者设计的集成开发环境(IDE),它**支持多种操作系统,包括Windows、Linux和macOS**。 以下是Thonny编辑器的一些特点: 1. **用户友好**:Thonny具有简洁直观的用户界面,非常适合初学者...

Global site tag (gtag.js) - Google Analytics