翻译进度
7
分块数量
6
参与人数

标准库满足不了你的需求?

这是一篇协同翻译的文章,你可以点击『我来翻译』按钮来参与翻译。

尽管Python标准库已经非常丰富了,但也有一个由第三方开发人员提供的、可从  Python 包索引 获得模块的生态系统。本附录描述了其中的一些模块,包括您可能使用它们来补充或甚至替换标准库的情况。

正文

 String  模块包括一个分叉基本的模块工具, 但 Jinja2  和  Mako  是流行的独立代替品。它们都支持循环和条件控制结构,以及用于将数据与模版组合易产生文本输出的其他特性。

Re 模块包括用于使用正式描述的模式(正则表达式)来搜索和解析文本功能。然而这不是解析文本的唯一方式。

 PLY  包支持一 GNU 工具 LXX 合 YACC 的方式构建语法分析器,通常用于构建语言编译器。通过提供描述有效的指令输入、语法和每次遇到时采取的操作,可以构建完全功能的编译和解释器,以及更直接的数据解析器。

PyParsing 是构建解析器的另一种工具。输入是可以是操作符合方法调用链接在一起构建语法的类的实例。

最后, NLTK  是处理自然语言文本的一个软件包——人类语言而不是计算机语言。它支持将句子解析为词类、查找词根形式和基本语义处理。

算法

Functools  模块包括一些用于创建装饰器的工具,这些函数包装其他函数以改变它们的行为。 Wrapt包比 functools.wrap() 包更进一步,以确保装饰器被正确构造并适用于所有边缘情况。

zhanggh 翻译于 1个月前

日期和时间

time 和 datetime 提供了用于处理时间和日期的函数与类。 两者均包含用于解析字符串以将其转换为内部表示的函数。 dateutil 包包含了更为灵活的解析器,可以更轻松地构建优雅地处理不同输入格式的健壮应用程序。

datetime 模块包括一个时区感知类,用于表示特定时期的特定时间。 但是,它并没有包含完整的时区数据库。  pytz 包确提供了这样一个数据库。它并不和标准库一起发布,因为是由其它作者开发维护,它会随着时区和夏令时间的改变而频繁更新。

数学

math 模块包含高级数学函数的快速实现。 NumPy 包扩展了支持的函数集,包括线性代数和傅里叶变换函数。它同样内含了改进版的 array - 快速实现的多维数组。

lunyang 翻译于 2周前

数据持久化与数据交换

本书 sqlite3 章节中的例子直接运行SQL语句,并使用了低级数据结构。然而大型应用程序通常需会使用对象关系映射器( Object Relational Mapper, ORM ) 将类映射到数据库中的表。 sqlalchemy ORM 库提供用于将类与表关联、构建查询以及与不同类型的生产级关系数据库连接的API。

lxml 包封装了 libxml2 和 libxslt 库,为我们提供了除 [xml.etree.ElementTree](https://pymotw.com/3/ xml.etree.ElementTree/index.html#module-xml.etree.ElementTree "xml.etree.ElementTree:XML Manipulation API") 中的 XML 解析器外的另一个选择。熟悉使用其他语言的开发人员可能会发现在 Python 中使用 lxml 会更容易一些。

defusedxml 包修复了大量XML 实体扩展拒绝服务攻击漏洞(例如 「 Billion Laughs 」),使得使用不受信任的 XML 文件时比单独使用标准库更安全。

jinyu121 翻译于 1周前

Cryptography

The team building the cryptography package says "Our goal is for it to be your 'cryptographic standard library'." The cryptography package exposes high-level APIs to make it easy to add cryptographic features to applications and the package is actively maintained with frequent releases to address vulnerabilities in the underlying libraries such as OpenSSL.

Concurrency with Processes, Threads, and Coroutines

The event loop built into asyncio is a reference implementation based on the abstract API defined by the module. It is possible to replace the event loop with a library such as uvloop, which gives better performance in exchange for adding extra application dependencies.

The curio package is another concurrency package similar to asyncio but with a smaller API that treats everything as a coroutine and does not support callbacks in the way asyncio does.

The Twisted library provides an extensible framework for Python programming, with special focus on event-based network programming and multiprotocol integration. It is mature, robust, and well-documented.

The Internet

The requests package is a very popular replacement for urllib.request. It provides a consistent API for working with remote resources addressable via HTTP, includes robust SSL support, and can use connection pooling for better performance in multi-threaded applications. It also provides features that make it well suited for accessing REST APIs, such as built-in JSON parsing.

Python's html module includes a basic parser for well-formed HTML data. However, real world data is rarely well structured, making parsing it problematic. The BeautifulSoup and PyQuery libraries are alternatives to html that are more robust in the face of messy data. Both define APIs for parsing, modifying, and constructing HTML.

The built-in http.server package includes base classes for creating simple HTTP servers from scratch. It does not offer much support beyond that for building web-based applications, though. The Django and Pyramid packages are two popular web application frameworks that provide more support for advanced features like request parsing, URL routing, and cookie handling.

Many existing libraries do not work with asyncio because they do not integrate with the event loop. A new set of libraries such as aiohttp is being created to fill this gap as part of the aio-libs project.

Email

The API for imaplib is relatively low-level, requiring the caller to understand the IMAP protocol to build queries and parse results. The imapclientpackage provides a higher-level API that is easier to work with for building applications that need to manipulate IMAP mailboxes.

Application Building Blocks

The two standard library modules for building command line interfaces, argparse and getopt, both separate the definition of command line arguments from their parsing and value processing. Alternatively, click (the "Command Line Interface Construction Kit"), works by defining command processing functions and then associating option and prompt definitions with those commands using decorators.

cliff ("Command Line Interface Formulation Framework") provides a set of base classes for defining commands and a plugin system for extending applications with multiple sub-commands that can be distributed in separate packages. It uses argparse to build the help text and argument parser, so the command line processing is familiar.

The docopt package reverses the typical flow by asking the developer to write the help text for a program, which it then parses to understand the sub-commands, valid combinations of options, and sub-commands.

For interactive terminal-based programs, prompt_toolkit includes advanced features like color support, syntax highlighting, input editing, mouse support, and searchable history. It can be used to build command-oriented programs with a prompt loop like the cmd module, or full-screen applications like text editors.

While INI files such as used by configparser continue to be popular for application configuration, the YAML file format is also very popular. YAML provides many of the data structure features of JSON in a format that is easier for people to read. The PyYAML library provides access to a YAML parser and serializer.

Developer Tools

The standard library module venv is new in Python 3. For similar application isolation under both Python 2 and 3, use virtualenv.

The fixtures package provides several test resource management classes tailor made to work with the addCleanup() method of test cases from the unittest module. The provided fixture classes can manage loggers, environment variables, temporary files, and more in a consistent and safe way that ensures each test case is completely isolated from others in the suite.

The distutils module in the standard library for packaging Python modules for distribution and reuse is deprecated. The replacement, setuptools, is packaged separately from the standard library to make it easier to deliver new versions frequently. The API for setuptools includes tools for building the list of files to include in a package. There are extensions to automatically build the list from the set of files managed by a version control system. For example, using setuptools-git with source in a git repository causes all of the tracked files to be included in the package by default. After a package is built, the twine application will upload it to the package index to be shared with other developers.

Tools like tabnanny are good at finding common formatting mistakes in Python code. The Python Code Quality Authority maintains an extensive range of more advanced static analysis tools, including tools that enforce style guidelines, find common programming errors, and even help avoid excessive complexity.

See also

  • Python Package Index or PyPI -- The site for finding and downloading extension modules distributed separately from the Python runtime.

本文章首发在 PythonCaff
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

参与译者:6
讨论数量: 0
发起讨论


暂无话题~