11 Python Libraries You Might Not Know

by Greg | January 20, 2015


There are tons of Python packages out there. So many that no one man or woman could possibly catch them all. PyPi alone has over 47,000 packages listed!

Recently, with so many data scientists making the switch to Python, I couldn't help but think that while they're getting some of the great benefits of pandasscikit-learn, and numpy, they're missing out on some older yet equally helpful Python libraries.

In this post, I'm going to highlight some lesser-known libraries. Even you experienced Pythonistas should take a look, there might be one or two in there you've never seen!

1) delorean

Delorean is a really cool date/time library. Apart from having a sweet name, it's one of the more natural feeling date/time munging libraries I've used in Python. It's sort of like moment in javascript, except I laugh every time I import it. The docs are also good and in addition to being technically helpful, they also make countless Back to the Future references.

from delorean import Delorean
EST = "US/Eastern"
d = Delorean(timezone=EST)

2) prettytable

There's a chance you haven't heard of prettytable because it's listed on GoogleCode, which is basically the coding equivalent of Siberia.

Despite being exiled to a cold, snowy and desolate place, prettytable is great for constructing output that looks good in the terminal or in the browser. So if you're working on a new plug-in for the IPython Notebook, check out prettytable for your HTML __repr__.

from prettytable import PrettyTable
table = 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 |
+----------------------+----------+

3) snowballstemmer

Ok so the first time I installed snowballstemmer, it was because I thought the name was cool. But it's actually a pretty slick little library. snowballstemmer will stem words in 15 different languages and also comes with a porter stemmer to boot.

from snowballstemmer import EnglishStemmer, SpanishStemmer
EnglishStemmer().stemWord("Gregory")
# Gregori
SpanishStemmer().stemWord("amarillo")
# amarill

4) wget

Remember every time you wrote that web crawler for some specific purpose? Turns out somebody built it...and it's called wget. Recursively download a website? Grab every image from a page? Sidestep cookie traces? Done, done, and done.

Movie Mark Zuckerberg even says it himself

First up is Kirkland, they keep everything open and allow indexes on their apache configuration, so a little wget magic is enough to download the entire Kirkland facebook. Kid stuff!

The Python version comes with just about every feature you could ask for and is easy to use.

import wget
wget.download("http://www.cnn.com/")
# 100% [............................................................................] 280385 / 280385

Note that another option for linux and osx users would be to use do: from sh import wget. However the Python wget module does have a better argument handline.

5) PyMC

I'm not sure how PyMC gets left out of the mix so often. scikit-learn seems to be everyone's darling (as it should, it's fantastic), but in my opinion, not enough love is given to PyMC.

from pymc.examples import disaster_model
from pymc import MCMC
M = MCMC(disaster_model)
M.sample(iter=10000, burn=1000, thin=10)
[-----------------100%-----------------] 10000 of 10000 complete in 1.4 sec

If you don't already know it, PyMC is a library for doing Bayesian analysis. It's featured heavily in Cam Davidson-Pilon's Bayesian Methods for Hackers and has made cameos on a lot of popular data science/python blogs, but has never received the cult following akin to scikit-learn.

6) sh

I can't risk you leaving this page and not knowing about shsh lets you import shell commands into Python as functions. It's super useful for doing things that are easy in bash but you can't remember how to do in Python (i.e. recursively searching for files).

from sh import find
find("/tmp")
/tmp/foo
/tmp/foo/file1.json
/tmp/foo/file2.json
/tmp/foo/file3.json
/tmp/foo/bar/file3.json

7) fuzzywuzzy

Ranking in the top 10 of simplest libraries I've ever used (if you have 2-3 minutes, you can read through the source), fuzzywuzzy is a fuzzy string matching library built by the fine people at SeatGeek.

fuzzywuzzy implements things like string comparison ratios, token ratios, and plenty of other matching metrics. It's great for creating feature vectors or matching up records in different databases.

from fuzzywuzzy import fuzz
fuzz.ratio("Hit me with your best shot", "Hit me with your pet shark")
# 85

8) progressbar

You know those scripts you have where you do a print "still going..." in that giant mess of a for loop you call your __main__? Yeah well instead of doing that, why don't you step up your game and start using progressbar?

progressbar does pretty much exactly what you think it does...makes progress bars. And while this isn't exactly a data science specific activity, it does put a nice touch on those extra long running scripts.

Alas, as another GoogleCode outcast, it's not getting much love (the docs have 2 spaces for indents...2!!!). Do what's right and give it a good ole pip install.

from progressbar import ProgressBar
import time
pbar = ProgressBar(maxval=10)
for i in range(1, 11):
pbar.update(i)
time.sleep(1)
pbar.finish()
# 60% |######################################################## |

9) colorama

So while you're making your logs have nice progress bars, why not also make them colorful! It can actually be helpful for reminding yourself when things are going horribly wrong.

colorama is super easy to use. Just pop it into your scripts and add any text you want to print to a color:

10) uuid

I'm of the mind that there are really only a few tools one needs in programming: hashing, key/value stores, and universally unique ids. uuid is the built in Python UUID library. It implements versions 1, 3, 4, and 5 of the UUID standards and is really handy for doing things like...err...ensuring uniqueness.

That might sound silly, but how many times have you had records for a marketing campaign, or an e-mail drop and you want to make sure everyone gets their own promo code or id number?

And if you're worried about running out of ids, then fear not! The number of UUIDs you can generate is comparable to the number of atoms in the universe.

import uuid
print uuid.uuid4()
# e7bafa3d-274e-4b0a-b9cc-d898957b4b61

Well if you were a uuid you probably would be.

11) bashplotlib

Shameless self-promotion here, bashplotlib is one of my creations. It lets you plot histograms and scatterplots using stdin. So while you might not find it replacing ggplot or matplotlib as your everyday plotting library, the novelty value is quite high. At the very least, use it as a way to spruce up your logs a bit.

$ pip install bashplotlib
$ scatter --file data/texas.txt --pch x

11 Python Libraries You Might Not Know的更多相关文章

  1. 1.3 Essential Python Libraries(一些重要的Python库)

    1.3 Essential Python Libraries(一些重要的Python库) 如果不了解Python的数据生态,以及本书中即将用到的一些库,这里会做一个简单的介绍: Numpy 这里就不过 ...

  2. macosx 10.11 python pip install 出现错误OSError: [Errno 1] Operation not permitted:

    Exception: Traceback (most recent call last): File , in main status = self.run(options, args) File , ...

  3. 11.python之线程,协程,进程,

    一,进程与线程 1.什么是线程 线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行 ...

  4. #11 Python字典

    前言 前两节介绍了Python列表和字符串的相关用法,这两种数据类型都是有序的数据类型,所以它们可以通过索引来访问内部元素.本文将记录一种无序的数据类型——字典! 一.字典与列表和字符串的区别 字典是 ...

  5. 11 Python 文件操作

    文件操作基本流程 计算机系统分为:计算机硬件,操作系统,应用程序三部分. 我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必须要保存于硬盘中,这就涉及到应用程序要操作硬件,众所周知 ...

  6. 11.python中的元组

    在学习什么是元组之前,我们先来看看如何创建一个元组对象: a = ('abc',123) b = tuple(('def',456)) print a print b

  7. 11 python初学 (文件)

    对文件的操作分为 3 步: 打开文件: f = open('望月怀古', 'r', encoding='utf8') # 路径可以写绝对路径,也可以写相对路径: 操作 关闭文件: f.close() ...

  8. 11.python描述符---类的装饰器---@property

    描述符1.描述符是什么:描述符本质就是一个新式类,在这个新式类中,至少实现了__get__(),__set__(),__delete__()这三个内置方法中的一个,描述符也被称为描述符协议(1):__ ...

  9. 11: python递归

    1.1 递归讲解 1.定义 1. 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 2.递归特性 1. 必须有一个明确的结束条件 2. 每次进入更深一层递归时,问题 ...

随机推荐

  1. Comparable和Comparator接口是干什么的?列出它们的区别

    Java提供了只包含一个compareTo()方法的Comparable接口.这个方法可以个给两个对象排序.具体来说,它返回负数,0,正数来表明输入对象小于,等于,大于已经存在的对象. Java提供了 ...

  2. Ubuntu 16.04 PHP5.6

    Cannot add PPA: 'ppa:ondrej/php5-5.6' Ubuntu 16.04 PHP5.6 安装 Apache + PHP 5.6 + mysql 5.5 系统: Ubuntu ...

  3. html标签注意事项

    1,关于媒体的video标签 在同一个页面上如果有多个video标签,并且初始化都赋值,则video不会播放, 解决办法,用计时器每隔50ms给后面的video标签设置src,设置完为止 2,关于ch ...

  4. wpf 绑定除数据上下文外的属性

    例如: listview 绑定了一个windows.datacontext.一个集合,那么其中一个item想绑定windows.datacontext.A属性怎么办: 通过查找祖先的方法:

  5. Android Studio 安装及汉化

    { https://www.bilibili.com/video/av48649403?from=search&seid=15739157224002905777 Tool: https:// ...

  6. Android中的Parcel机制(下)

    上一篇中我们透过源码看到了Parcel背后的机制,本质上把它当成一个Serialize就可以了,只是它是在内存中完成的序列化和反序列化,利用的是连续的内存空间,因此会更加高效. 我们接下来要说的是Pa ...

  7. [学习笔记] $FWT$

    \(FWT\)--快速沃尔什变化学习笔记 知识点 \(FWT\)就是求两个多项式的位运算卷积.类比\(FFT\),\(FFT\)大多数求的卷积形式为\(c_n=\sum\limits_{i+j=n}a ...

  8. splay 模板 洛谷3369

    题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入 xx 数 删除 xx 数(若有多个相同的数,因只删除一个) 查询 xx 数的排名(排名定义为比当前数小的数 ...

  9. (转)JAVA国际化

    转:http://www.cnblogs.com/jjtech/archive/2011/02/14/1954291.html 国际化英文单词为:Internationalization,又称I18N ...

  10. CSS3:CSS3 文本效果

    ylbtech-CSS3:CSS3 文本效果 1.返回顶部 1. CSS3 文本效果 CSS3 文本效果 CSS3中包含几个新的文本特征. 在本章中您将了解以下文本属性: text-shadow bo ...