这是一篇译文,文中提及了一些不常见但是有用的Python库
原文地址:http://blog.yhathq.com/posts/11-python-libraries-you-might-not-know.html
moment 库,每次我导入它的时候都会想笑。文档也很棒,除了有技术指导外,他们还引用了《回到未来》的无数内容(来丰富文档)。|
1
2
3
|
from delorean import DeloreanEST = "US/Eastern"d = Delorean(timezone=EST) |
prettytable 仍旧是最棒的结构化输出的库,它能在终端或浏览器里构建良好的输出。因此,如果你正在用 IPython Notebook的新插件,建议你用prettytable 来代替__repr__进行HTML输出。|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
from prettytable import PrettyTabletable = PrettyTable(["animal", "ferocity"])table.add_row(["wolverine", 100])table.add_row(["grizzly", 87])table.add_row(["Rabbit of Caerbannog", 110])table.add_row(["cat", -1])table.add_row(["platypus", 23])table.add_row(["dolphin", 63])table.add_row(["albatross", 44])table.sort_key("ferocity")table.reversesort = True+----------------------+----------+| animal | ferocity |+----------------------+----------+| Rabbit of Caerbannog | 110 || wolverine | 100 || grizzly | 87 || dolphin | 63 || albatross | 44 || platypus | 23 || cat | -1 |+----------------------+----------+ |
snowballstemmer,是因为我觉得这个名字很酷炫。但它的确是一个小巧好使的包。snowballstemmer 通过porter stemmer算法来提取15种语言的单词词干。|
1
2
3
4
5
|
from snowballstemmer import EnglishStemmer, SpanishStemmerEnglishStemmer().stemWord("Gregory")# GregoriSpanishStemmer().stemWord("amarillo")# amarill |
wget.想要以递归的方式下载所有页面?想要抓取页面上的每张图?想要避免cookie追踪?wget可以给你想要的一切。从柯克兰(寝室名)开始,这里的一切公共目录都是公开的,还允许在Apache系统里插入目录。所以用个wget就能下载柯克兰全部的照片库里的图片了。易如反掌!
|
1
2
3
|
import wgetwget.download("<a href="http://www.cnn.com/">http://www.cnn.com/</a>")# 100% [............................................................................] 280385 / 280385 |
linux和osx的用户还会用到另一个选项:from sh import wget。不过Python wget模块还有更好的参数处理。
PyMC 包的了。scikit-learn 似乎是所有人的宠儿(它应得的,它太出色了),但是依我看来,PyMC更有魅力。|
1
2
3
4
5
|
from pymc.examples import disaster_modelfrom pymc import MCMCM = MCMC(disaster_model)M.sample(iter=10000, burn=1000, thin=10)[-----------------100%-----------------] 10000 of 10000 complete in 1.4 sec |
PyMC 主要用来做贝叶斯定理分析。它的特点在Cam Davidson-Pilon的 Bayesian Methods for Hackers 里着重介绍过,它在许多流行的数据科学/python博客上也是一颗闪耀的钻石,但是它从来没得到过像它的同类scikit-learn一样的狂热追捧。sh库的情况下,就让你离开。 sh用来将shell命令导入到Python中。在bash它超有用,但在Python里你可能就不住怎么使用(即递归搜索文件)。|
1
2
3
4
5
6
7
|
from sh import findfind("/tmp")/tmp/foo/tmp/foo/file1.json/tmp/foo/file2.json/tmp/foo/file3.json/tmp/foo/bar/file3.json |
fuzzywuzzy 实现了字符串的相似率,令牌比和许多其他的匹配模式。它也可以用来创建特征向量或者匹配不同数据库的记录。|
1
2
3
|
from fuzzywuzzy import fuzzfuzz.ratio("Hit me with your best shot", "Hit me with your pet shark")# 85 |
__main__循环的时候,你用过print "still going...” 这样的提示么?你知道么,这样会感觉特别low。想要找东西替代它么?为什么不用progressbar来提升你游戏的档次呢?progressbar 在针对精确数据的时候效果很好,它提供了一个文本模式的progressbar。但即便是一个变动的不精确数据,使用它也比用那些很长的脚本好。pip install可以安装它。|
1
2
3
4
5
6
7
8
|
from progressbar import ProgressBarimport timepbar = ProgressBar(maxval=10)for i in range(1, 11): pbar.update(i) time.sleep(1)pbar.finish()# 60% |######################################################## | |
colorama 很容易使用。只要把它写进你的脚本,添加到想要打印的文本之前:uuid 就是Python的一个UUID包。它实现了UUID standards 标准的1,3,4,5版本。在确保唯一性上真的很方便。|
1
2
3
|
import uuidprint uuid.uuid4()# e7bafa3d-274e-4b0a-b9cc-d898957b4b61 |
bashplotlib 是我创建的一个库。它通过标准输入绘制出柱状图和散点图。当然,你不需要考虑用它来替代ggplot或matplotlib来作为你每天绘图的包,只要作为新奇玩意试试就好。但至少,你可以使用它把你的日志文件弄的好看点。原文:http://www.cnblogs.com/zer0Black/p/4262456.html