标准库中最有用的 Python 模块?

发布于 2024-08-05 05:49:10 字数 1432 浏览 19 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

王权女流氓 2024-08-12 05:49:10

1-2 小时内涵盖的模块完全取决于受众的兴趣或关注点。他们还参加哪些其他课程?他们准备立即使用什么?

请务必提及mathdecimaldatetimetimere

对于将进行面向文件工作的 IT 类型:globfnmatchosos.pathtempfileshutil

数据库人员一定听说过 sqlitejson

模拟观众可能想了解随机

Web 开发人员必须从客户端的角度了解 urllib2。还有 Beautiful Soup 和您选择的 XML 解析器。

Web 开发人员必须从服务器的角度了解loggingwsgiref

Modules to cover in a 1-2 hour slot entirely depend on your audience's interest or focus. What other classes are they taking? What are they prepared to make use of immediately?

Be sure to mention math, decimal and datetime and time and re.

For IT-types who will be doing file-oriented work: glob, fnmatch, os, os.path, tempfile, and shutil.

Database folks must hear about sqlite and json.

Simulation audience may want to hear about random.

Web developers must hear about urllib2 from a client point of view. Also Beautiful Soup and an XML parser of your choice.

Web developers must hear about logging and wsgiref from a server point of view.

负佳期 2024-08-12 05:49:10

我会提供 itertoolsfunctools。这些模块对编程中随处可见的抽象进行操作,因此我认为它们对于研究很有用。在更实际的事情中,xml 模块(xml.domxml.sax)非常有用。

I'd offer itertools and functools. These modules operate over abstractions that are found everywhere in programming, so I think they are useful to study. Among more practical things, xml modules (xml.dom, xml.sax) can be very useful.

撩起发的微风 2024-08-12 05:49:10

查看 PyMOTW(本周 Python 模块)。虽然它不是严格意义上的 stdlib,但它是 python stdlib 的明显和不那么明显的宝石的重要资源。更重要的是,它还可以作为所引入模块的优秀文档。

Have a look at PyMOTW (Python Module Of The Week). Although it is not strictly stdlib, it's a great resource of obvious and not so obvious gems of the python stdlib. What's more, it also serves as excellent documentation of the introduced modules.

一身骄傲 2024-08-12 05:49:10

我会选择一些对于执行典型计算机任务的典型计算机用户/程序员最有意义的模块。这样,他们就很有可能在自己的时间里使用 python。

在我看来,大多数人可能执行的操作是文件操作,例如检查目录中的每个文件并对其执行某些操作。

因此,我想说模块:os和os.path可能是最重要的,并且还提到glob、fnmatch和shutil。此外,子流程也可能非常有用,因为它往往在上述上下文中使用。

最后,我会选择 optparse,因为这将使他们能够非常快速地制作可用的、程序员友好的程序,这也有望鼓励他们实际编写其他人想要使用的程序。

I'd go for a few modules which make the most sense to a typical computer user/programmer performing typical computer tasks. That way, there's the largest chance that they might actually use python on their own time.

In my opinion, the operations most people will likely perform are file operations, for example going over every file in a directory and performing some action on it.

Therefore, I'd say the modules: os and os.path are probably the most important, and also mention glob, fnmatch and shutil. Also, subprocess might be very useful too, since it tends to get used in the above mentioned context.

Lastly, I'd go with optparse, since that will get them into very quickly making usable, programmer-friendly programs, which hopefully will also encourage them to actually write programs that other people want to use.

离旧人 2024-08-12 05:49:10

我只记得一个很实用的模块:copy。
我经常使用其中的 deepcopy() 。

I just remember a very practical module: copy.
I use the deepcopy() from it quite often.

蔚蓝源自深海 2024-08-12 05:49:10

这在一定程度上取决于他们将要做什么以及他们的水平。
我希望有人在我开始时向我指出的一些模块是:

  • StringIO - 阻止他们重新实现它,如果他们没有发现它,他们就会这样做。
  • 日志记录 - 在调试打印输出时将它们置于正确的路径上
  • pickle - 阻止他们尝试在任何地方使用 XML。
  • xml.etree.ElementTree - 当它们实际需要使用 XML 时将它们从 DOM 模型中保存下来。
  • pprint - 使 python 中的嵌套结构不那么令人生畏。

It depends a little on what they will be doing and what level they are.
Some modules I wish someone pointed out to me when I started are:

  • StringIO - to stop them from reimplementing it, which they will if they don't discover it.
  • logging - to put them on the right path when it comes to debug printouts
  • pickle - to stop them from trying to use XML everywhere.
  • xml.etree.ElementTree - To save them from the DOM model when they actually need to work with XML.
  • pprint - to make nested structures in python less intimidating.
时间你老了 2024-08-12 05:49:10

我会将 urllib2 添加到列表中。

I would add urllib2 to the list.

逆夏时光 2024-08-12 05:49:10

在短短一到两个小时的时间内,我将介绍 easy_installPyPI 存储库:即使它们不在标准库中,它们也使您能够安装许多其他外部模块,并且它是第一个位置当您在标准库中找不到时去哪里查找。

除此之外,我还会介绍 numpy、re、doctest/unittest,也许还有 pickle。

In only a one-two hour slot, I would introduce easy_install and the PyPI repository: even if they are not in the standard lib, they enable you to install many other external modules, and it is the first place where to look when you can't find in the standard lib.

Apart from that, I would introduce numpy, re, doctest/unittest, and maybe pickle.

瞄了个咪的 2024-08-12 05:49:10

operator,在已经提到的内容旁边。

operator, next to what's already mentioned.

掀纱窥君容 2024-08-12 05:49:10

不要忘记 datetime、weakref、pickle、StringIO、heapq,可能是线程。

numpy 也值得一提,尽管它不是来自标准库。

Don't forget about datetime, weakref, pickle, StringIO, heapq, may be threading.

And numpy also worths mentioning, although it is not from the standard library.

嗳卜坏 2024-08-12 05:49:10

os 和 os.path:因为这些是任何人在 python 中编写独立于平台的代码所需的核心模块,并且学生在学习 os 和 os.path 后可以从 shell 脚本切换到 python 脚本。

os and os.path: because those are the core modules which anyone will require to write platform independent code in python and students can switch from shell script to python script after learning os and os.path.

有深☉意 2024-08-12 05:49:10

我认为这里的每个人都掌握了所有重要的内容,除了 sys 之外。如果您查看实际的 Python 代码,您会发现 sys 可能是最常用的模块之一(通常是因为 sys.version)。

另外,它并不是真正的模块,但我会提到__future__

没有人应该在不执行 import this 的情况下使用 Python。

I think everyone here got all the important ones, except for sys. If you look at actual Python code, sys is probably one of the most commonly used modules (usually because of sys.version).

Also, it's not really a module, but I would mention __future__.

And nobody should use Python without doing import this.

爱人如己 2024-08-12 05:49:10

除了您提到的那些之外,我发现 subprocess 和 sqlite3 模块特别有用。但我当然会建议学生自己查看标准库模块列表。另外,在标准库之外的模块中,我会提到 numpy(或 numarray)和 pyparsing。

Aside from those you mentioned, I found subprocess and sqlite3 modules particularly useful. But I would certainly advice to students to take a look at the list of standard library modules themselves. Also, from modules outside of standard library, I would mention numpy (or numarray) and pyparsing.

云仙小弟 2024-08-12 05:49:10

我会在 decimal 模块上放置一些权重。如果他们是编程初学者,他们肯定不会意识到浮点精度的含义< /a>.如果使用货币或其他必须通过多次数学运算保持精确的小数精度的单位,则十进制模块非常有价值。

当然,您可能想要触及您不需要那么准确的情况。

I'd place some weight on the decimal module. If they are beginners at programming, they certainly won't be aware of the implications of floating point accuracy. The decimal module is extremely valuable if working with currency or other units that must retain exact decimal precision through several mathematic operations.

Of course, you'd probably want to touch on situations when you don't need to be that accurate as well.

梦里寻她 2024-08-12 05:49:10

对于理科学生来说,一个很少听说但功能强大的模块“networkx”将很有价值。但他们需要先安装它。该模块有详细记录:http://networkx.lanl.gov/index.html

For science student, a rarely heard but powerful module 'networkx' will be valuable. But they need to install it first. This module is well documented: http://networkx.lanl.gov/index.html

淡水深流 2024-08-12 05:49:10

没有 timeit 就很难生活

>>> # Python shell usage
... import timeit
>>> tt = timeit.Timer("foo = 'time this'", "print 'setup with this arg'")
>>> tt.timeit(number=1000)
setup with this arg
0.00021100044250488281
>>>

[mpenning@Bucksnort ~]$ # Bash shell usage
[mpenning@Bucksnort ~]$ # 5 runs with 1000 samples each.
[mpenning@Bucksnort ~]$ python -m timeit -n 1000 -r 5 -s "print 'setup w/ this arg'" \
    "foo = 'time this'"
setup w/ this arg
setup w/ this arg
setup w/ this arg
setup w/ this arg
setup w/ this arg
1000 loops, best of 5: 0.173 usec per loop
[mpenning@Bucksnort ~]$

It is hard to live without timeit

>>> # Python shell usage
... import timeit
>>> tt = timeit.Timer("foo = 'time this'", "print 'setup with this arg'")
>>> tt.timeit(number=1000)
setup with this arg
0.00021100044250488281
>>>

[mpenning@Bucksnort ~]$ # Bash shell usage
[mpenning@Bucksnort ~]$ # 5 runs with 1000 samples each.
[mpenning@Bucksnort ~]$ python -m timeit -n 1000 -r 5 -s "print 'setup w/ this arg'" \
    "foo = 'time this'"
setup w/ this arg
setup w/ this arg
setup w/ this arg
setup w/ this arg
setup w/ this arg
1000 loops, best of 5: 0.173 usec per loop
[mpenning@Bucksnort ~]$
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文