11个你可能不知道的Python库.docx
- 文档编号:25662058
- 上传时间:2023-06-11
- 格式:DOCX
- 页数:12
- 大小:986.21KB
11个你可能不知道的Python库.docx
《11个你可能不知道的Python库.docx》由会员分享,可在线阅读,更多相关《11个你可能不知道的Python库.docx(12页珍藏版)》请在冰豆网上搜索。
11个你可能不知道的Python库
11个你可能不知道的Python库
(中文)
阅读目录
∙1)delorean
∙2)prettytable
∙3)snowballstemmer
∙4)wget
∙5)PyMC
∙6)sh
∙7)fuzzywuzzy
∙8)progressbar
∙9)colorama
∙10)uuid
∙11)bashplotlib
1) delorean
非常酷的日期/时间库
1
2
3
from delorean import Delorean
EST = "US/Eastern"
d = Delorean(timezone=EST)
2) prettytable
可以在浏览器或终端构建很不错的输出
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 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 |
+----------------------+----------+
|RabbitofCaerbannog | 110 |
| wolverine| 100 |
| grizzly| 87 |
|dolphin | 63 |
| albatross| 44 |
| platypus| 23 |
| cat| -1 |
+----------------------+----------+
3) snowballstemmer
非常瘦小的语言转换库,支持15种语言
1
2
3
4
5
from snowballstemmer import EnglishStemmer, SpanishStemmer
EnglishStemmer().stemWord("Gregory")
# Gregori
SpanishStemmer().stemWord("amarillo")
# amarill
4) wget
Python的网络爬虫库
1
2
3
import wget
wget.download("
# 100% [............................................................................] 280385 / 280385
5) PyMC
PyMC,一个用于贝叶斯分析的函数库
1
2
3
4
5
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
6) sh
将shell命令作为函数导入Python脚本
1
2
3
4
5
6
7
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
用于字符串匹配率、令牌匹配等
1
2
3
from fuzzywuzzy import fuzz
fuzz.ratio("Hit me with your best shot", "Hit me with your pet shark")
# 85
8) progressbar
如其名,一个滚动条函数库
1
2
3
4
5
6
7
8
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
一个色彩库,可以为文本添加丰富的色彩
10) uuid
一个可以产生唯一uuid的库
1
2
3
import uuid
print uuid.uuid4()
# e7bafa3d-274e-4b0a-b9cc-d898957b4b61
11) bashplotlib
Python的绘图控件,可以绘制直方图、散点图等
1
2
$ pip install bashplotlib
$ scatter --file data/texas.txt --pch x
(原文)
TherearetonsofPythonpackagesoutthere.Somanythatnoonemanorwomancouldpossiblycatchthemall.PyPi alonehasover47,000packageslisted!
Recently,withsomanydatascientistsmakingtheswitchtoPython,Icouldn'thelpbutthinkthatwhilethey'regettingsomeofthegreatbenefitsof pandas, scikit-learn,and numpy,they'remissingoutonsomeolderyetequallyhelpfulPythonlibraries.
Inthispost,I'mgoingtohighlightsomelesser-knownlibraries.EvenyouexperiencedPythonistasshouldtakealook,theremightbeoneortwointhereyou'veneverseen!
1) delorean
Delorean isareallycooldate/timelibrary.Apartfromhavingasweetname,it'soneofthemorenaturalfeelingdate/timemunginglibrariesI'veusedinPython.It'ssortoflike moment injavascript,exceptIlaugheverytimeIimportit.Thedocsarealsogoodandinadditiontobeingtechnicallyhelpful,theyalsomakecountless BacktotheFuture references.
fromdeloreanimportDelorean
EST="US/Eastern"
d=Delorean(timezone=EST)
2) prettytable
There'sachanceyouhaven'theardof prettytable becauseit'slistedonGoogleCode,whichisbasicallythecodingequivalentofSiberia.
Despitebeingexiledtoacold,snowyanddesolateplace, prettytable isgreatforconstructingoutputthatlooksgoodintheterminalorinthebrowser.Soifyou'reworkingonanewplug-infortheIPythonNotebook,checkoutprettytable foryourHTML __repr__.
fromprettytableimportPrettyTable
table=PrettyTable(["animal","ferocity"])
table.add_row(["wolverine",100])
table.add_row(["grizzly",87])
table.add_row(["RabbitofCaerbannog",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|
+----------------------+----------+
|RabbitofCaerbannog|110|
|wolverine|100|
|grizzly|87|
|dolphin|63|
|albatross|44|
|platypus|23|
|cat|-1|
+----------------------+----------+
3) snowballstemmer
OksothefirsttimeIinstalled snowballstemmer,itwasbecauseIthoughtthenamewascool.Butit'sactuallyaprettyslicklittlelibrary. snowballstemmer willstemwordsin15differentlanguagesandalsocomeswithaporterstemmertoboot.
fromsnowballstemmerimportEnglishStemmer,SpanishStemmer
EnglishStemmer().stemWord("Gregory")
#Gregori
SpanishStemmer().stemWord("amarillo")
#amarill
4) wget
Remembereverytimeyouwrotethatwebcrawlerforsomespecificpurpose?
Turnsoutsomebodybuiltit...andit'scalled wget.Recursivelydownloadawebsite?
Grabeveryimagefromapage?
Sidestepcookietraces?
Done,done,anddone.
MovieMarkZuckerberg evensaysithimself
FirstupisKirkland,theykeepeverythingopenandallowindexesontheirapacheconfiguration,soalittle wgetmagic isenoughtodownloadtheentireKirklandfacebook.Kidstuff!
The Pythonversion comeswithjustabouteveryfeatureyoucouldaskforandiseasytouse.
importwget
wget.download("
#100%[............................................................................]280385/280385
Notethatanotheroptionforlinuxandosxuserswouldbetousedo:
fromshimportwget.HoweverthePythonwgetmoduledoeshaveabetterargumenthandline.
5) PyMC
I'mnotsurehow PyMC getsleftoutofthemixsooften. scikit-learn seemstobeeveryone'sdarling(asitshould,it'sfantastic),butinmyopinion,notenoughloveisgivento PyMC.
frompymc.examplesimportdisaster_model
frompymcimportMCMC
M=MCMC(disaster_model)
M.sample(iter=10000,burn=1000,thin=10)
[-----------------100%-----------------]10000of10000completein1.4sec
Ifyoudon'talreadyknowit, PyMC isalibraryfordoingBayesiananalysis.It'sfeaturedheavilyinCamDavidson-Pilon's BayesianMethodsforHackers andhasmadecameosonalotofpopulardatascience/pythonblogs,buthasneverreceivedthecultfollowingakinto scikit-learn.
6) sh
Ican'triskyouleavingthispageand notknowingabout sh. sh letsyouimportshellcommandsintoPythonasfunctions.It'ssuperusefulfordoingthingsthatareeasyinbashbutyoucan'trememberhowtodoinPython(i.e.recursivelysearchingforfiles).
fromshimportfind
find("/tmp")
/tmp/foo
/tmp/foo/file1.json
/tmp/foo/file2.json
/tmp/foo/file3.json
/tmp/foo/bar/file3.json
7) fuzzywuzzy
Rankinginthetop10ofsimplestlibrariesI'veeverused(ifyouhave2-3minutes,youcan readthroughthesource),fuzzywuzzy isafuzzystringmatchinglibrarybuiltbythefinepeopleat SeatGeek.
fuzzywuzzy implementsthingslikestringcomparisonratios,tokenratios,andplentyofothermatchingmetrics.It'sgreatforcreatingfeaturevectorsormatchinguprecordsindifferentdatabases.
fromfuzzywuzzyimportfuzz
fuzz.ratio("Hitmewithyourbestshot","Hitmewithyourpetshark")
#85
8) progressbar
Youknowthosescriptsyouhavewhereyoudoa print"stillgoing..." inthatgiantmessofaforloopyoucallyour __main__?
Yeahwellinsteadofdoingthat,whydon'tyoustepupyourgameandstartusing progressbar?
progressbar doesprettymuchexactlywhatyouthinkitdoes...makesprogressbars.Andwhilethisisn'texactlyadatasciencespecificactivity,itdoesputanicetouchonthoseextralongrunningscripts.
Alas,asanotherGoogleCodeoutcast,it'snotgettingmuchlove(thedocshave2spacesforindents...2!
!
!
).Dowhat'srightandgiveitagoodole pipinstall.
fromprogressbarimportProgressBar
importtime
pbar=ProgressBar(maxval=10)
foriinrange(1,11):
pbar.update(i)
time.sleep
(1)
pbar.finish()
#60%|########################################################|
9) colorama
Sowhileyou'remakingyourlogshaveniceprogressbars,whynotalsomakethemcolorful!
Itcanactuallybehelpfulforremindingyourselfwhenthingsaregoinghorriblywrong.
colorama issupereasytouse.Justpopitintoyourscriptsandaddanytextyouwanttoprinttoacolor:
10) uuid
I'mofthemindthattherearereallyonlyafewtoolsoneneedsinprogramming:
hashing,key/valuestores,anduniversallyuniqueids. uuid isthebuiltinPythonUUIDlibrary.Itimplementsversions1,3,4,and5ofthe UUIDstandards andisreallyhandyfordoingthingslike...err...ensuringuniqueness.
Thatmightsoundsilly,buthowmanytimeshaveyouhadrecordsforamarketingcampaign,orane-maildropandyouwanttomakesureeveryonegetstheirownpromocodeoridnumber?
Andifyou'reworriedaboutrunningoutofids,thenfearnot!
ThenumberofUUIDsyoucangenerateiscomparabletothenumberofatomsintheuniverse.
importuuid
printuuid.uuid4()
#e7bafa3d-274e-4b0a-b9cc-d898957b4b61
Wellifyouwerea uuid youprobablywouldbe.
11) bashplotlib
Shamelessself-promotionhere, bashplotlib isoneofmycreations.Itletsyouplothistogramsandscatterplotsusingstdin.Sowhileyoumightnotfinditreplacingggplotormatplotlibasyoureverydayplottinglibrary,thenoveltyvalueisquitehigh.Attheveryleast,useitasawaytospruceupyourlogsabit.
$pipinstallbashplotlib
$scatter--fil
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 11 可能 知道 Python
![提示](https://static.bdocx.com/images/bang_tan.gif)