[Continuously updated] python common module, package usage summary

[Continuously updated] python common module, package usage summary

yaml

yaml is designed to write the language configuration file

pip install pyyaml
//通过pip安装
//用vim新建一个test1.yaml文件,内容如下
#example//#表示注释
NAME: zhangchen//大小写敏感

name: ZhangChen//大小写敏感

DATA:
    YEAR: 2019//使用缩进表示层级关系
    MONTH: 3//缩进时不允许使用Tab键,只允许使用空格
    DAY: 9//缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
//用vim新建一个test2.yaml文件,内容如下
#example
- A
- B
- C

- NAME: zhangchen

- name: ZhangChen

- DATA:
    YEAR: 2019
    MONTH: 3
    DAY: 9
//test.py内容如下
import yaml


f1 = open("test1.yaml", "r")
y1 = yaml.load(f1)
f2 = open("test2.yaml", "r")
y2 = yaml.load(f2)
print(y1)
print(y2)
//执行test.py,打印结果如下
{'DATA': {'YEAR': 2019, 'DAY': 9, 'MONTH': 3}, 'NAME': 'zhangchen', 'name': 'ZhangChen'}
//可见test1.yaml中格式yaml.load返回的类型是一个字典
['A', 'B', 'C', {'NAME': 'zhangchen'}, {'name': 'ZhangChen'}, {'DATA': {'YEAR': 2019, 'DAY': 9, 'MONTH': 3}}]
//可见test2.yaml中格式yaml.load返回的类型是一个列表
//将test1.yaml改成如下内容
#example
NAME: zhangchen
---
name: ZhangChen
---
DATA:
    YEAR: 2019
    MONTH: 3
    DAY: 9
//将test.py改成如下内容
import yaml


f1 = open("test1.yaml", "r")
y1 = yaml.load_all(f1)
f2 = open("test2.yaml", "r")
y2 = yaml.load(f2)
for data in y1:
    print(data)
print(y2)
//执行test.py,打印结果如下
{'NAME': 'zhangchen'}
{'name': 'ZhangChen'}
{'DATA': {'YEAR': 2019, 'DAY': 9, 'MONTH': 3}}
['A', 'B', 'C', {'NAME': 'zhangchen'}, {'name': 'ZhangChen'}, {'DATA': {'YEAR': 2019, 'DAY': 9, 'MONTH': 3}}]
//可见load_all的作用
yaml.dump 
//将一个python对象生成为yaml文档,两个参数
//第一个参数是字典或列表
//第二个参数是一个打开的文本文件或二进制文件,yaml.dump会把生成的yaml文档写到文件里

argparse

[Those things] argparse Python modules - command line parsing

logging

[Those things] Python logging module - Log Processing

pprint

pprint is python comes with standard libraries, generally do not require additional installation. With pip install will prompt already exists.
The main role is to pprint lists, tuples, dictionaries, easy to read by and consistent with the rules of grammar in the form of python print or save.

import pprint


data = [(1,2,3,4,5,6,7),{1:"a",2:"b",3:"c"},{4:["D","E"],5:"F",6:["G","H","I","J"],7:"K"}]
pprint.pprint(data)
//终端打印结果如下
[(1, 2, 3, 4, 5, 6, 7),
 {1: 'a', 2: 'b', 3: 'c'},
 {4: ['D', 'E'], 5: 'F', 6: ['G', 'H', 'I', 'J'], 7: 'K'}]
//可见pprint.pprint()是将列表、元组、字典等按易于阅读又符合python语法规则的形式打印
import pprint


data = [(1,2,3,4,5,6,7),{1:"a",2:"b",3:"c"},{4:["D","E"],5:"F",6:["G","H","I","J"],7:"K"}]
pprint.pformat(data)
//终端打印结果如下
"[(1, 2, 3, 4, 5, 6, 7),\n {1: 'a', 2: 'b', 3: 'c'},\n {4: ['D', 'E'], 5: 'F', 6: ['G', 'H', 'I', 'J'], 7: 'K'}]"
//可见pprint.pformat()是将列表、元组、字典等按易于阅读又符合python语法规则的形式保存成字符串
import pprint


data = [(1,2,3,4,5,6,7),{1:"a",2:"b",3:"c"},{4:["D","E"],5:"F",6:["G","H","I","J"],7:"K"}]
result=pprint.pformat(data)
for each in result.split("\n"):
	print(each)
//终端打印结果如下
[(1, 2, 3, 4, 5, 6, 7),
 {1: 'a', 2: 'b', 3: 'c'},
 {4: ['D', 'E'], 5: 'F', 6: ['G', 'H', 'I', 'J'], 7: 'K'}]

pyautogui

[Python] pyautogui-- those things programmatically control the mouse and keyboard

virtualenv和virtualenvwrapper

[Python] those things on linux virtual environment

* Args and ** kwargs parameters

Python [those things] * args and ** kwargs parameters

Class

Use [those things] Python classes in python

Epilogue

If you have amendments or questions, please leave a message or contact me by mail.
Hand very hard, if my article helpful to you, please indicate the source.

Guess you like

Origin blog.csdn.net/Zhang_Chen_/article/details/88364137