python software development specification documents for the latter part of code division & efficient management

According to my study, organize and supplemented in accordance with the understanding knowledge of python modules, hoping for some python lovers need to know there is help!

First, the software development specification - sub-file

When the code is present in a document py:

1. not easy to manage (modify, increase)
2. poor readability
3. Load Slow

Django-- prototype (commonly known as convention)

1. Start the file to launch Interface
2. Public documents we need to function
3. profile (static file) variable
4. The main logic core
The user account and password data and other documents
6. The main message logging, recording the behavior of developers

High cohesion

1566564722045

Two, sys

sys python interpreter to do interactive

import sys

print(sys.path)     # 模块查找的顺序

print(sys.argv)     # 只能在终端执行,第一个元素是本身路径,列表形式

print(sys.modules)      # 查看加载到内存的模块

print(sys.platform)     # 查看当前操作系统平台mac - darwin    win - win32
print(sys.version)      # 查看python当前版本信息


扩展知识:
mac -- linux 查看 ls          苹果系统
win          查看 dir       Windows系统
可以用来判断,已达到系统操作兼容

Third, serialization

1.json

4 Group 2

dumps loads - for network transmission
dump load - for file storage

Converting a string data type (serialization), the string will be converted to the original data type (deserialize)

You can sequence: after dictionaries, lists, tuples sequence becomes list

import json
dic = {"key":1}
a = json.dumps(dic)
print(a, type(a))   #将数据类型转换成字符串(序列化)
结果:{"key": 1} <class 'str'>

a1 = json.loads(a)
print(a1, type(a1))     #将字符串重新转为原数据类型(反序列化)
结果:{'key': 1} <class 'dict'>

f = open("a", "a", encoding="utf-8")
a = json.dump(dic, f)   # 将原数据类型转换成字符串,写入到文件
print(a,type(a))
结果:{"key": 1} <class 'str'>

f = open("a", "r", encoding="utf-8")
a1 = json.load(f)   #将文件中的字符串转换成原数据类型
print(a1,type(a1))      #a1["key"] = 1,a1为字典的键
结果:{'key': 1} <class 'dict'>

If the file is written in multiple lines - "\ n"

import json
dic = {"key": 1}
f = open("a", "a", encoding="utf-8")
f.write(json.dumps(dic) + "\n")     #字符串 + "\n"
f.write(json.dumps(dic) + "\n")
f.write(json.dumps(dic) + "\n")
结果:{"key": 1}       #写入文件的字符串
     {"key": 1}
     {"key": 1}

2.pickle

There are only python, python sequence in almost all data types, functions can not be anonymous sequences, can function
import pickle 
def func(): 
    print(1)
import pickle
a = pickle.dumps(func)  #将原数据类型转换成类似字节的内容
结果:b'\x80\x03c__main__\nfunc\nq\x00.'

print(pickle.loads(a))  #将类似字节的内容转换成原数据类型
结果:<function func at 0x000001BBFF7060D0>

Four, os (folders file path)

import os   #os是和操作系统做交互,给操作发指令

print(os.getcwd())  # 获取当前文件工作的路径
os.chdir(" ")   #路径切换
os.curdir  返回当前目录: ('.')  **
os.pardir  获取当前目录的父目录字符串名:('..') **

Folder related

os.mkdir("a2")  # 创建文件夹 a2为要创建的文件夹

os.rmdir("a2")  # 删除文件夹

os.makedirs("a1/a2/a3") # 递归创建多级文件夹

os.removedirs("a1/a2/a3")   # 递归删除文件夹

print(os.listdir(r"路径"))    #查看当前文件下的所有内容

Related documents

os.remove(r"路径")    #删除文件,彻底删除  找不回来

os.rename() #重命名
os.stat('path/filename')  获取文件/目录信息 **

Related path

path

os.path.abspath("py文件") # 返回的是绝对路径

os.path.split(r"路径")    #将路径分割成一个路径和一个文件名

os.path.dirname(r"路径")  # 获取到父目录

os.path.basename(r"路径") #获取文件名

os.path.join()  # 路径拼接(非常重要)

Judge

os.path.exists()    #判断当前路径是否存在

os.path.isabs() #判断是不是绝对路径

os.path.isdir() #判断是不是文件夹

os.path.isfile()    #判断是不是文件

os.path.getsize()   #获取文件大小
os.path.getatime(path)  返回path所指向的文件或者目录的最后访问时间  **
os.path.getmtime(path)  返回path所指向的文件或者目录的最后修改时间  **
os.path.getsize(path) 返回path的大小 ***

Operating system-related (to know)

os.sep    输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/" *
os.linesep    输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n" 
os.pathsep    输出用于分割文件路径的字符串 win下为;,Linux下为: *
os.name    输出字符串指示当前使用平台。win->'nt'; Linux->'posix' *
# 和执行系统命令相关
os.system("bash command")  运行shell命令,直接显示  **
os.popen("bash command).read()  运行shell命令,获取执行结果  **
os.environ  获取系统环境变量  **

os.stat ( 'path / filename') acquires file / directory structure description information (Learn)

stat 结构:
st_mode: inode 保护模式
st_ino: inode 节点号。
st_dev: inode 驻留的设备。
st_nlink: inode 的链接数。
st_uid: 所有者的用户ID。
st_gid: 所有者的组ID。
st_size: 普通文件以字节为单位的大小;包含等待某些特殊文件的数据。
st_atime: 上次访问的时间。
st_mtime: 最后一次修改的时间。
st_ctime: 由操作系统报告的"ctime"。在某些系统上(如Unix)是最新的元数据更改的时间,在其它系统上(如Windows)是创建时间(详细信息参见平台的文档)。

Guess you like

Origin www.cnblogs.com/shenhongbo/p/11403075.html