Python common modules

Source: Internet
Author: User

Yield
    • Yield of the statement form yield 1

    • The expression form of yield X=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
    1. X=yield

    2. G.send (' 111 '), first pass 111 to yield, the yield is assigned to X

    3. 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
    1. Create a new namespace

    2. Executes the code in the module with a new namespace as the global namespace

    3. 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
    • Find---> Built-in modules from memory first---> Sys.path path

Common Modules Time
  • Timestamp  print (Time.localtime (). Tm_year)

  • Formatted outputprint(time.strtime(‘%Y-&m-%d %H:%M:%S‘))

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
  1. Serialize import json dic={ ‘go‘:‘die‘, ‘hehe‘:‘哈哈‘, } Json.dump (Dic,open (' B.json ', ' W ')) #直接打开文件写入

  2. 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
  1. CRITICAL = 50

  2. FATAL = CRITICAL

  3. ERROR = 40

  4. WARNING = 30

  5. WARN = WARNING

  6. INFO = 20

  7. DEBUG = 10

  8. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.