Python 常用的标准库以及第三方库有哪些

Python 常用的标准库以及第三方库有哪些,第1张

参考:知乎

Python 常用的标准库以及第三方库

standard libs:

itertools

functools 学好python有必要掌握上面这两个库吧,

re 正则

subprocess 调用shell命令的神器

pdb 调试

traceback 调试

pprint 漂亮的输出

logging 日志

threading和multiprocessing 多线程

urllib/urllib2/httplib http库,httplib底层一点,推荐第三方的库requests

os/sys 系统,环境相关

Queue 队列

pickle/cPickle 序列化工具

hashlib md5, sha等hash算法

cvs

json/simplejson python的json库,据so上的讨论和benchmark,simplejson的性能要高于json

timeit 计算代码运行的时间等等

cProfile python性能测量模块

glob 类似与listfile,可以用来查找文件

atexit 有一个注册函数,可用于正好在脚本退出运行前执行一些代码

dis python 反汇编,当对某条语句不理解原理时,可以用dis.dis 函数来查看代码对应的python 解释器指令等等。

3th libs:

paramiko ssh python 库

selenium 浏览器自动化测试工具selenium的python 接口

lxml python 解析html,xml 的神器

mechanize Stateful programmatic web browsing

pycurl cURL library module for Python

Fabric Fabric is a Python (2.5 or higher) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.

xmltodict xml 转 dict,真心好用

urllib3 和 requests: 当然其实requests就够了 Requests: HTTP for Humans

flask web 微框架

ipdb 调试神器,同时推荐ipython!结合ipython使用

redis redis python接口

pymongo mongodbpython接口

PIL python图像处理

mako python模版引擎

numpy , scipy 科学计算

matplotlib 画图

scrapy 爬虫

django/tornado/web.py/web2py/uliweb/flask/twisted/bottle/cherrypy.等等 python web框架/服务器

sh 1.08 — sh v1.08 documentation 用来运行shell 模块的 极佳选择

10个顶级且实用的python库

1、Dash

Dash是比较新的软件包,它是用纯python构建数据可视化app的理想选择,因此特别适合处理数据的任何人。Dash是Flask、Plotly.js和React.js的混合体。

2、Pygame

Pygame是SDL多媒体库的python装饰器,SDL是一个跨平台开发库,旨在提供对以下内容的低级接口:音频、键盘、鼠标、游戏杆、基于OpenGL和Direct3D的图形硬件。

Pygame具有高度的可移植性,几乎可以在所有平台和 *** 作系统上运行。尽管它具有完善的游戏引擎,但您也可以使用此库直接从python脚本播放MP3文件。

3、Pillow

Pillow专门用于处理图像,您可以使用该库创建缩略图,在文件格式之间转换、旋转、应用滤镜、显示图像等等。如果您需要对许多图像执行批量 *** 作,这是理想的选择。

4、Colorama

Colorama允许你在终端使用颜色,非常适合python脚本,文档简短而有趣,可以在Colorama PyPi页面上找到。

5、JmesPath

在python中使用JSON非常容易,因为JSON在python字典上的映射非常好。此外,python带有自己出色的json库,用于解析和创建JSON。对我来说,这是它最好的功能之一,如果我需要使用JSON,可以考虑使用python。

JmesPath使python处理JSON更加容易,它允许您明确地指定如何从JSON文档中提取元素。

6、Requests

Requests建立在世界上下载量最大的python库urllib3上,它令Web请求变得非常简单,功能强大且用途广泛。

Requests可以完成您能想到的所有高级工作,比如:认证,使用cookie,执行POST、PUT、DELETE等,使用自定义证书,使用会话Session、使用代理等。

7、Simplejson

python中的本地json模块有什么问题?没有!实际上,python的json是Simplejson。意思是:python采用了Simplejson的一个版本,并将其合并到每个发行版中,但是使用Simplejson具有一些优点:它适用于更多python版本、它比python随附的版本更新频率更高、它具有用C编写的部分,因此非常快速。

8、Emoji

Emoji库非常意思,但并非每个人都喜欢表情包,分析视角媒体数据时,Emoji包非常有用。

9、Python-dateutil

Python-dateutil模块提供了对标准datetime模块的强大扩展。我的经验是:常规的python日期时间功能在哪里结束,而Python-dateutil就出现了。

10、BeautifulSoup

如果您从网站上提取了一些HTML,则需要对其进行解析以获取实际所需的内容。BeautifulSoup是一个python库,用于从HTML和XML文件中提取数据。它提供了导航,搜索和修改解析树的简单方法。它非常强大,即使损坏了,也能够处理各种HTML,这是一个非常强大的功能。

它的一些主要功能:

①BeautifulSoup会自动将传入文档转换为Unicode,将传出文档转换为UTF-8,您无需考虑编码。

②BeautifulSoup位于流行的python解析器的顶部,使您可以尝试不同的解析策略或提高灵活性。

作为轻量级的本地存储方式,对于构建不依赖服务器的小型项目,用LowDB存储和管理数据是十分理想的选择。在Nodejs, Electron and browser等一些小型项目中经常能看到LowDB的身影。

https://github.com/typicode/lowdb

npm install lowdb

或者:

yarn add lowdb

const low = require('lowdb')

const FileSync = require('lowdb/adapters/FileSync')// 有多种适配器可选择

const adapter = new FileSync('db.json')// 申明一个适配器

const db = low(adapter)

db.defaults({posts: [], user: {}, count: 0})

.write()

db.get('posts')

.push({id: 1, title: 'lowdb is awesome'})

.write()

db.set('user.name', 'typicode')

.write()

db.update('count', n =>n + 1)

.write()

运行程序会在项目中添加db.json文件,里面存储了添加的数据:

{

"posts": [

{

"id": 1,

"title": "lowdb is awesome"

}

],

"user": {

"name": "typicode"

},

"count": 1

}

lowdb是基于lodash构建的,所以可以使用任何lodash强大的函数,比如: _.get() 和 _.find(),并且可以串联地使用:

db.get('users')

.find({sex: 'male'})

.value()

函数 功能

low(adapter) 返回一个具有特定属性和功能的 lodash chain

db.[...].write() / .value() 写 / 读数据

db.getState() / .setState() 获取 / 设置数据库的状态

db._ 数据库lodash的实例,可以利用这个添加自己的函数或者第三方的mixins,比如lodash-id

db._.mixin({

second: function(array) {

return array[1]

}

})

db.get('posts')

.second()

.value()

针对lowdb自带的适配器:FileSync、FileAsync 和 LocalBrowser,有以下可选参数:

defaultValue: 文件不存在时的默认值;

serialize/deserialize: 写之前和读之后的 *** 作。

const adapter = new FilSync('db.json',{

serialize: (data) =>encrypt(JSON.stringify(data)),

deserialize: (data) =>JSON.parse(decrypt(data))

})

可以直接使用lodash的函数进行查询。需要注意的是有些 *** 作可能会导致原数据被修改,为了避免这种误 *** 作,需要使用 .cloneDeep(), *** 作都是惰性的,只有调用 .value()或 .write()后才会正式执行。

检查users是是否存在

db.has('users')

.value()

设置users

db.set('users', [])

.write()

排序、选择

db.get('users')

.filter({sex: 'male'})

.sortBy('age')

.take(5)

.value()

获取特定字段

db.get('users')

.map('name')

.value()

获取数量

db.get('users')

.size()

.value()

获取特定信息

db.get('users[0].name')

.value()

更新信息

db.get('users')

.find({name: 'Tom'})

.assign({name: 'Tim'})

.write()

删除信息

db.get('users')

.remove({name: 'Time'})

.write()

移除属性

db.unset('users.name)

.write()

深拷贝

db.get('users')

.cloneDeep()

.value()

可以使用 shortid 和 lodash-id 为数据库中的每一条记录创建唯一的id索引,然后通过id检索 *** 作记录:

const shortid = require('shortid')

const postId = db

.get('posts')

.push({ id: shortid.generate(), title: 'low!' })

.write()

.id

const post = db

.get('posts')

.find({ id: postId })

.value()

const lodashId = require('lodash-id')

const FileSync = require('lowdb/adapters/FileSync')

const adapter = new FileSync('db.json')

const db = low(adapter)

db._.mixin(lodashId)

// We need to set some default values, if the collection does not exist yet

// We also can store our collection

const collection = db

.defaults({ posts: [] })

.get('posts')

// Insert a new post...

const newPost = collection

.insert({ title: 'low!' })

.write()

// ...and retrieve it using its id

const post = collection

.getById(newPost.id)

.value()

low( ) 函数接受自定义的Adapter

class MyStorage {

constructor() {

// ...

}

read() {

// Should return data (object or array) or a Promise

}

write(data) {

// Should return nothing or a Promise

}

}

const adapter = new MyStorage(args)

const db = low(adapter)

==============================================

英文官网介绍,更加简洁

Lowdb 3 is a pure ESM package. If you're having trouble importing it in your project, please read this.

You can use TypeScript to type check your data.

You can also add lodash or other utility libraries to improve lowdb.

For CLI, server and browser usage, seeexamples/ directory.

Lowdb has two classes (for asynchronous and synchronous adapters).

Callsadapter.read() and sets db.data .

Note: JSONFile and JSONFileSync adapters will set db.data to null if file doesn't exist.

Callsadapter.write(db.data) .

Holds your db content. If you're using the adapters coming with lowdb, it can be any type supported byJSON.stringify .

For example:

Adapters for reading and writing JSON files.

In-memory adapters. Useful for speeding up unit tests.

Synchronous adapter forwindow.localStorage .

Adapters for reading and writing text. Useful for creating custom adapters.

If you've published an adapter for lowdb, feel free to create a PR to add it here.

You may want to create an adapter to writedb.data to YAML, XML, encrypt data, a remote storage, ...

An adapter is a simple class that just needs to expose two methods:

For example, let's say you have some async storage and want to create an adapter for it:

Seesrc/adapters/ for more examples.

To create an adapter for another format than JSON, you can useTextFile or TextFileSync .

For example:

Lowdb doesn't support Node's cluster module.

If you have large JavaScript objects ( ~10-100MB ) you may hit some performance issues. This is because whenever you call db.write , the whole db.data is serialized using JSON.stringify and written to storage.

Depending on your use case, this can be fine or not. It can be mitigated by doing batch operations and callingdb.write only when you need it.

If you plan to scale, it's highly recommended to use databases like PostgreSQL or MongoDB instead.


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10067291.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-04
下一篇2023-05-04

发表评论

登录后才能评论

评论列表(0条)

    保存