[关闭]
@bergus 2016-04-26T20:45:51.000000Z 字数 5837 阅读 3168

让人耳目一新的Python库

python 类库推荐


摘要

工欲善其事,必先利其器。Python社区高产富饶,有大量的美丽,实用的库, 这里挑选出来一些接口设计简洁的库。

docopt

Github: https://github.com/docopt/docopt
Pythonic的命令行参数解析库:
  1. """Usage:
  2. quick_example.py tcp <host> <port> [--timeout=<seconds>]
  3. quick_example.py serial <port> [--baud=9600] [--timeout=<seconds>]
  4. quick_example.py -h | --help | --version
  5. """
  6. from docopt import docopt
  7. if __name__ == '__main__':
  8. arguments = docopt(__doc__, version='0.1.1rc')
  9. print(arguments)

requests

Github: https://github.com/kennethreitz/requests

大神kennethreitz的作品,简易明了的HTTP请求操作库, 是urllib2的理想替代品

API简洁明了,这才是Python开发者喜欢的:
  1. >>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
  2. >>> r.status_code
  3. 200
  4. >>> r.headers['content-type']
  5. 'application/json; charset=utf8'
  6. >>> r.encoding
  7. 'utf-8'
  8. >>> r.text
  9. u'{"type":"User"...'
  10. >>> r.json()
  11. {u'private_gists': 419, u'total_private_repos': 77, ...}

sh

http://amoffat.github.io/sh/

如其名,子进程接口。
  1. from sh import ifconfig
  2. print(ifconfig("wlan0"))

purl

github: https://github.com/codeinthehole/
拥有简洁接口的URL处理器:

  1. >>> from purl import URL
  2. >>> from_str = URL('https://www.google.com/search?q=testing')
  3. >>> u.query_param('q')
  4. u'testing'
  5. >>> u.host()
  6. u'www.google.com'

path.py

github: https://github.com/jaraco/path.py
一个文件系统处理库,不过目前还在开发阶段

  1. from path import path
  2. d = path('/home/guido/bin')
  3. for f in d.files('*.py'):
  4. f.chmod(0755)

Peewee

https://github.com/coleifer/peewee
小型ORM, 接口很漂亮:

  1. # get tweets by editors ("<<" maps to IN)
  2. Tweet.select().where(Tweet.user << editors)
  3. # how many active users are there?
  4. User.select().where(User.active == True).count()
  5. #类似的我的 CURD.py (https://github.com/hit9/CURD.py) :)
  6. User.create(name="John", email="John@gmail.com") # create
  7. User.at(2).update(email="John@github.com") # update
  8. John = User.where(name="John").select().fetchone() # read
  9. # who wrote posts?
  10. for post, user in (Post & User).select().fetchall():
  11. print "Author: %s, PostName: %s" % (user.name, post.name)

Pony ORM

https://github.com/ponyorm/pony
一个十分独特的ORM,接口简单干净,最大的特点是支持使用generator的语法来进行查询,可以使查询语句变得简洁,灵活,而且漂亮。

  1. 例如可以使用如下的语句来进行一个查询:
  2. select(p for p in Product if p.name.startswith('A') and p.cost <= 1000)
  3. 同时,Pony ORM还提供了一个ER图编辑工具来进行数据库原型设计。

Spyne

https://github.com/arskom/spyne
一个用于构建RPC服务的工具集,支持SOAP,JSON,XML等多种流行的协议。

    现在有诸如 flask-restful 以及 django-rest-framework 等框架用于 REST 服务的开发,人们对于 REST 之外的框架似乎兴趣不大。Spyne 很好地填补了这一空白,它支持多种协议,而且本身也封装地相当好:
  1. class HelloWorldService(ServiceBase):
  2. @srpc(Unicode, Integer, _returns=Iterable(Unicode))
  3. def say_hello(name, times):
  4. for i in range(times):
  5. yield 'Hello, %s' % name
  6. application = Application([HelloWorldService],
  7. tns='spyne.examples.hello',
  8. in_protocol=Soap11(validator='lxml'),
  9. out_protocol=Soap11()
  10. )
  11. #短短几行代码便实现了一个支持SOAP 1.1 协议的服务器端application,接入任何一个WSGI兼容的服务器后端就可以运行了。

schema

https://github.com/halst/schema
同样是docopt的作者编写的,一个数据格式检查库,非常新颖:

  1. >>> from schema import Schema
  2. >>> Schema(int).validate(123)
  3. 123
  4. >>> Schema(int).validate('123')
  5. Traceback (most recent call last):
  6. ...
  7. SchemaError: '123' should be instance of <type 'int'>
  8. Traceback (most recent call last):
  9. ...
  10. SchemaError: '123' should be instance of <type 'int'>

fn.py

https://github.com/kachayev/fn.py
增强Python的函数式编程:

  1. from fn import _
  2. print (_ + 2) # "(x1) => (x1 + 2)"
  3. print (_ + _ * _) # "(x1, x2, x3) => (x1 + (x2 * x3))"

when.py

https://github.com/dirn/When.py
友好的时间日期库:

  1. >>> import when
  2. >>> when.timezone()
  3. 'Asia/Shanghai'
  4. >>> when.today()
  5. datetime.date(2013, 5, 14)
  6. >>> when.tomorrow()
  7. datetime.date(2013, 5, 15)
  8. >>> when.now()
  9. datetime.datetime(2013, 5, 14, 21, 2, 23, 78838)

clize

https://github.com/epsy/clize
用 docopt 写程序的使用doc是不是很爽, clize是一个类似的库。可以用程序的函数名字来作为使用方法

  1. #!/usr/bin/env python
  2. from clize import clize
  3. @clize
  4. def echo(text, reverse=false):
  5. if reverse:
  6. text = text[::-1]
  7. print(text)
  8. if __name__ == '__main__':
  9. import sys
  10. echo(*sys.argv)
  11. 而这个小程序就可以这么使用:
  12. $ ./echo.py --help
  13. Usage: ./echo.py [OPTIONS] text
  14. Positional arguments:
  15. text
  16. Options:
  17. --reverse
  18. -h, --help Show this help

sphinx-theme-rux

A no-sidebar red sexy sphinx theme
https://github.com/hit9/sphinx-theme-rux

lice

一个用来为你的项目生成许可证的工具。这下可方便了,不用手工的去修改了!https://github.com/licenses/lice

autopep8

每个Python程序员都应该checkout的repo.自动的把你的Python代码转成符合PEP8风格的代码.
使用 -i 参数来直接修改你的 Python文件:
autopep8 -i mycode.py
https://github.com/hhatto/autopep8

powerline

如果你是个linux(or mac)下的开发者,又喜欢在终端下工作的话,你一定喜欢用powerline来美化自己的工作空间。
之前github上兴起了vim-powerline,tmux-powerline,还有powerline-bash,现在Lokaltog提供了一个统一的解决方案,只要安装这个python包,再追加些东西到配置文件就可以使用漂亮的powerline了
https://github.com/Lokaltog/powerline

Pocoo小组

pocoo出的库,必属精品。 http://www.pocoo.org/
它的库很出名: flask, jinja2, pygments,sphinx

HanLP

HanLP是由一系列模型与算法组成的Java工具包,目标是普及自然语言处理在生产环境中的应用。不仅仅是分词,而是提供词法分析、句法分析、语义理解等完备的功能。HanLP具备功能完善、性能高效、架构清晰、语料时新、可自定义的特点。
http://hanlp.linrunsoft.com/
http://www.ltp-cloud.com/

PyTables

PyTables is a package for managing hierarchical datasets and designed to efficiently cope with extremely large amounts of data.
It is built on top of the HDF5 library and the NumPy package. It features an object-oriented interface that, combined with C extensions for the performance-critical parts of the code (generated using Cython), makes it a fast, yet extremely easy to use tool for interactively save and retrieve very large amounts of data. One important feature of PyTables is that it optimizes memory and disk resources so that they take much less space (between a factor 3 to 5, and more if the data is compressible) than other solutions, like for example, relational or object oriented databases.
https://github.com/PyTables/PyTables

Watchdog

Python API library and shell utilities to monitor file system events.
https://pythonhosted.org/watchdog/

P

Python Version Management, Simplified.
https://github.com/qw3rtman/p

pip-tools

pip-tools = pip-compile + pip-sync
A set of tools to keep your pinned Python dependencies fresh.
https://github.com/nvie/pip-tools

Curdling

Curdling is a command line tool for managing Python packages.
http://clarete.li/curdling/

ptpython

A better Python REPL
s
s

Tablib

format-agnostic tabular dataset library
https://github.com/kennethreitz/tablib

dataset

在数据库中存储Python字典 – 可以协同SQLite,MySQL,和 PostgreSQL工作。
https://dataset.readthedocs.org/en/latest/

知乎推荐

后言和链接

你可能对 Github上Python开发者应该关心的Repo 感兴趣
看下这个gist https://gist.github.com/medecau/797129
HN: https://news.ycombinator.com/item?id=4772261

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注