#取行數
import linecache
count = linecache.getlines('mv')[1]
print(count)
創業公司喜好的3款Python庫
html
Instavest上發表了一篇博文,文章分享了深受創業公司喜好的3款Python庫,該文章在Hacker News上引起了開發者的激烈探討。python
1. Whitenoise(見上面)mysql
2. Phonenumbers(精簡版)linux
要識別出電話號碼不是件容易的事情,而正則表達式也不必定能處理好各類五花八門的有效電話格式。git
例如:程序員
可見依賴於單一的正則檢測不必定能獲得想要的答案,因此,要適當藉助工具—Phonenumbers。推薦緣由是它小巧,實用簡便,沒有地理代編碼,運營商,時區等metadata數據。它能識別多種格式,而後使用不一樣的格式/樣式進行有效匹配。github
3. Pdfkitweb
藉助Pdfkit能夠便捷地把HTML轉換成PDF文件。這有何用處呢?比方說你的應用有一個含有發票信息的頁面,你就能夠透過Pdfkit幫助生成一個PDF文件供用戶進行下載,其用法以下:正則表達式
4.Python-dateutilsql
Numerous date utilities for calculating differences, etc. The most useful of these is a resilient date parser:
import dateutil.parser >>> dateutil.parser.parse("May 4th, 2012") datetime.datetime(2012, 5, 4, 0, 0) >>> dateutil.parser.parse("5-4-2012") datetime.datetime(2012, 5, 4, 0, 0) >>> dateutil.parser.parse("5.4.2012") datetime.datetime(2012, 5, 4, 0, 0) >>> dateutil.parser.parse("4th May 2012") datetime.datetime(2012, 5, 4, 0, 0)[Three Useful Python Libraries for Startups]
讓人耳目一新的Python庫
github: https://github.com/codeinthehole/purl
擁有簡潔接口的URL處理器:
>>> from purl import URL >>> from_str = URL('https://www.google.com/search?q=testing') >>> u.query_param('q') u'testing' >>> u.host() u'www.google.com'
github: https://github.com/jaraco/path.py
一個文件系統處理庫,不過目前還在開發階段
from path import path d = path('/home/guido/bin') for f in d.files('*.py'): f.chmod(0755)
https://github.com/coleifer/peewee
小型ORM, 接口很漂亮:
# get tweets by editors ("<<" maps to IN) Tweet.select().where(Tweet.user << editors) # how many active users are there? User.select().where(User.active == True).count()
相似的個人 CURD.py (https://github.com/hit9/CURD.py) :)
User.create(name="John", email="John@gmail.com") # create User.at(2).update(email="John@github.com") # update John = User.where(name="John").select().fetchone() # read # who wrote posts? for post, user in (Post & User).select().fetchall(): print "Author: %s, PostName: %s" % (user.name, post.name)
https://github.com/ponyorm/pony
一個十分獨特的ORM,接口簡單幹淨,最大的特色是支持使用generator的語法來進行查詢,可使查詢語句變得簡潔,靈活,並且漂亮。
例如可使用以下的語句來進行一個查詢:
select(p for p in Product if p.name.startswith('A') and p.cost <= 1000)
同時,Pony ORM還提供了一個ER圖編輯工具來進行數據庫原型設計。
https://github.com/halst/schema
一樣是docopt的做者編寫的,一個數據格式檢查庫,很是新穎:
>>> from schema import Schema >>> Schema(int).validate(123) 123 >>> Schema(int).validate('123') Traceback (most recent call last): ... SchemaError: '123' should be instance of <type 'int'> Traceback (most recent call last): ... SchemaError: '123' should be instance of <type 'int'>
https://github.com/kachayev/fn.py
加強Python的函數式編程:
from fn import _ print (_ + 2) # "(x1) => (x1 + 2)" print (_ + _ * _) # "(x1, x2, x3) => (x1 + (x2 * x3))"
pocoo出的庫,必屬精品。 http://www.pocoo.org/
它的庫很出名: flask, jinja2, pygments,sphinx
[讓人耳目一新的Python庫]
Github上Python開發者應該關心的Repo
carbaugh/lice
lice : Generate license files for your projects
一個用來爲你的項目生成許可證的工具。這下可方便了,不用手工的去修改了!
peewee: a small, expressive orm – supports postgresql, mysql and sqlite
你在用SQLAlchemy ? 我強烈推薦你看下peewee
來看一個sample:
User.select().where(User.active == True).order_by(User.username)
一個單文件的Python ORM.至關輕巧,支持三個數據庫。並且,它最討人喜歡的是它的輕量級的語法。
autopep8 : A tool that automatically formats Python code to conform to the PEP 8 style guide.
每一個Python程序員都應該checkout的repo.自動的把你的Python代碼轉成符合PEP8風格的代碼.
使用 -i 參數來直接修改你的 Python文件:
autopep8 -i mycode.py
fn.py : Functional programming in Python: implementation of missing features to enjoy FP
這是個頗有趣的項目,來彌補Python在函數式編程方面沒有的一些特性。來看個sample:
from fn import _ assert list(map(_ * 2, range(5))) == [0,2,4,6,8]
python-patterns : A collection of design patterns implemented (by other people) in python
這個repo收集了不少設計模式的python寫法
six : Six is a Python 2 and 3 compatibility library
Six沒有託管在Github上,而是託管在了Bitbucket上,不過這些都不是重點,重點是它的做用。
衆所周知 Python 2 和 Python 3 版本的分裂給 Python 開發者們帶來了很大的煩惱,爲了使代碼同時兼容兩個版本,每每要增長大量的代碼。 因而 Six 出現了。正如它的介紹所說,它是一個專門用來兼容 Python 2 和 Python 3 的庫。它解決了諸如 urllib 的部分方法不兼容, str 和 bytes 類型不兼容等「知名」問題。
它的效果怎麼樣?pypi上單日十萬以上,單月幾百萬的下載量足以說明了。要知道諸如 Flask 和 Django 這類知名的庫,月下載量也只有幾十萬。
[Github上Python開發者應該關心的Repo]
你可能沒聽過的11個Python庫
2) prettytable
你可能從未聽過該庫,由於它託管在GoogleCode。prettytable主要用於在終端或瀏覽器端構建很好的輸出。
3.snowballstemmer
好吧,我也是首次安裝該庫。這是一款很是瘦小的語言轉換庫,支持15種語言。
4.wget
你是否還記得,每一次都會由於某個目的而編寫網絡爬蟲工具,之後不再用了,由於wget就足夠你使用了。wget是Python版的網絡爬蟲庫,簡單好用。
5.PyMC
scikit-learn彷佛是全部人的寵兒,但在我看來,PyMC更有魅力。PyMC主要用來作Bayesian分析。
7.fuzzywuzzy
Fuzzywuzzy是一個能夠對字符串進行模糊匹配的庫,你們有空能夠去 查看源碼。
progressbar是一個進度條庫,該庫提供了一個文本模式的progressbar。
9.colorama
colorama主要用來給文本添加各類顏色,而且很是簡單易用。
bashplotlib是一個繪圖庫,它容許你使用stdin繪製柱狀圖和散點圖等。
[英文原文: 11 Python Libraries You Might Not Know]