Yield
Yield, association function in the form of an expression
Def eater (name): Print ('%s ready to eat '%name) and True:food yield print ('%s start to eat%s '% (nam E,food)) G=eater (' Tom ') #制作生成器next (g) #生成器初始化, when performing a yield pause, quite g.send (None) g.send (' Yellow stew chicken ') #在暂停处继续运行, pass the value to food
Function
-
X=yield
-
G.send (' 111 '), first pass 111 to yield, the yield is assigned to X
-
Then execute it again, knowing that the yield stops again and returns the return value after yield
Importing packages
-
Package is a directory containing the init. py file
-
The purpose of the package: is to organize the module from the directory level
-
The package is equivalent to a module, the nature or to the user
Import module Things to do
-
Create a new namespace
-
Executes the code in the module with a new namespace as the global namespace
-
Get the module name, point to the name space of the module's inferior
Ifname= = 'Main‘
As the script executes, the part that will need to be executed is separated from the part that needs to be imported
Module Search Path
Common Modules Time
Json
-
Storing data from in-memory serialization to disk serialization process: DIC---> res=json.dumps (DIC)--->f.write (res) deserialization process: Res=f.read ()---> Res.json.loads (RES)--->dic=reswith open(‘a.json‘,‘r‘) as f: dic=json.loads(f.read()) print(dic,type(dic))
JSON convenient operation
-
Serialize import json dic={ ‘go‘:‘die‘, ‘hehe‘:‘哈哈‘, } Json.dump (Dic,open (' B.json ', ' W ')) #直接打开文件写入
-
Deserialization
res=json.load(open(‘b.json‘,‘r‘)) #直接打开json文件 print(res,type(res))
Pickle only supports Python for this language OS & SYS
' Import Osprint (OS.GETCWD ()) #当前所在目录print (Os.listdir ()) #当前目录下文件print (os.name) print (Os.path.abspath (")) print ( Os.path.getsize (R ' C:\Users\Administrator\PycharmProjects\17\day6 ')) print (Os.path.dirname (Os.path.dirname ( Os.path.abspath (__file__))) #取出目录名字 ' Import Sys,timeprint (sys.argv) # List form the name of the current absolute path in the form of print (SYS) for I in range (50) : Sys.stdout.write ('%s\r '% (' # ' * i)) #\r for moving to the beginning Sys.stdout.flush () #立即刷新到终端 Time.sleep (0.1)
Logging module log level type, the larger the value, the more serious, from the beginning of the small value match
-
CRITICAL = 50
-
FATAL = CRITICAL
-
ERROR = 40
-
WARNING = 30
-
WARN = WARNING
-
INFO = 20
-
DEBUG = 10
-
NOTSET = 0
Import Logging Logging.basicconfig (filename= ' Access.log ', format= '% (asctime) s-% (name) s-% (LevelName) s-% (module) s: % (message) s ', datefmt= '%y-%m-%d%h:%m:%s%p ', level=40,) logging.debug (' Debug ') logging.info (' info ') logging.warnin G (' Warning ') logging.error (' Error ') logging.critical (' critical ')
This article is from the "11860872" blog, please be sure to keep this source http://11870872.blog.51cto.com/11860872/1934046
Python common modules