python software development standard, the module, the sequence of essays

 

1. Software development specifications

First of all:

When the code is stored in a file will cause py

1. not easy to manage, modify, add

2. poor readability

3. Load Slow

Divided file
1. Start the file (Start Interface) - starts put the bin file file
2. Public documents (we need to function) --- put the lib folder
3. profile (static file) variable - put conf file folder
4. The main logic (core) --- function, class, etc., src.py-- folder put core
5. The user data - documents like account passwords register-- discharge db folder
6. log ---- major record information, recording the behavior of the developer --- logg.log-- put the log file folder
high cohesion ---- previously defined module name, call
regist = r "module address"

 

2. Serialization and pickle json

json

Converting the data type into a string serialization json.dumps ---

The string into the original data type - deserialization json loads

Can sequence: dictionaries, lists, (after a tuple sequence becomes list)

 

dumps loads ---- for network transmission
dump load for file storage
dumps loads ------
DIC = '{ "Key":}. 1'
Import JSON
S = json.dumps (DIC)
Print (S, type (S)) ---- translated into strings
D = json.loads (S)
Print (D, type (D))

= List [1,2,3,4]
Import JSON
S = json.dumps (List) - sequence
l = json.loads (s) deserialization
Print (L, type (L))
the dump Load ----- -
DIC = { "Key":}. 1
Print (the json.dump (DIC, Open ( "A", "A", encoding = "UTF-. 8"))) to convert the source data into a character string type, write file to
print (json.load (open ( "a ", "r", encoding = "utf-8")) [ 'key']) in the file string converted into the source data type

dic = { "key" :. 1}
F = Open ( "A", "A", encoding = "UTF-. 8")
f.write (json.dumps (DIC) + "\ n-")
F1 = Open ( "A", "R & lt" , encoding = "UTF-. 8")
for I in F1:
Print (the json.load (I), type (json.loads (I)))
DIC = { "Key": "yuan Bao"}
Print (json.dumps (dic,ensure_ascii=False))
print(json.dumps(dic,ensure_ascii=False,sort_keys=True))排序

 

pickle:

--- the pickle python has almost all data types in python sequence, the sequence can not be anonymous function
Import the pickle
Print (the pickle.dumps ((1,2,3,4,5)))

FUNC DEF ():
Print (. 1)
A = the pickle.dumps (FUNC) to convert the source data into a type similar to the bytes of content
print (pickle.loads (a)) is similar to convert raw data into byte type

 

os folder, the file path

Work path:
Import os --- and do interact with the operating system, the operating system to send instructions to
print (os.getcwd ()) --- Get the current working directory file
os.chdir ( "absolute path") --- path switching
print (os.getcwd)

Folder
os.mkedir ( "name") create a folder
os.rmdir ( "name") to delete the specified folder
os.makedirs ( 'a / b / c ') recursively create folders
os.removedirs ( 'a1 / a2 / a3 ') recursively delete a folder
print (os.listdir (r' absolute path ')) to view the current folder and all contents (list display)

File
os.remove (r "absolute path") delete files completely deleted, can not find
os.rename () Rename
os.path.getsize (r "absolute path") Get File Size

Path
os.path.abspath ( "relative path") returns the absolute path ***
os.path.split (r "absolute path") divides the path into a path and a file name
os.path.dirname (r " absolute path ") to get the parent directory ***
os.path.basename (r" absolute path ") to get the name of the path
os.path.join (" D: \ Python " ," day12 "," test ") path splice****

Judgment
os.path.exists ( "blog") to determine whether there is a path
os.path.isabs (r "") Whether it exists or not, determine whether there is an absolute path
os.path.isdir (r "") to determine whether the folder
os.path.isfile (r "") is not a file is determined

 

Guess you like

Origin www.cnblogs.com/yecanglan/p/11401860.html