First, Ctrip functions and recursion
#用法: # def Eater (name): # Print ('%s start line'%name) # food_list=[]# whiletrue:# Food=yieldfood_list# food_list.append (food) #['GG','cc']# Print ('%s Eat%s'%(Name,food)) # # a=eater ('Alex'# #第一阶段: Initialize # next (aa_g) #等同于a_g. Send (None) # print ('===========>') # # #第二阶段: Give yield value # print (Aa_g.send ('GG')) #1Pass the yield of the currently paused position to the bone2continue execution until yield is hit again, then pause and take the return value after yield as the return value of this call # # print ('===========>') # Print (Aa_g.send ('cc') # Print (Aa_g.send ('GGCC'))
Second, process-oriented programming
#面向过程: The core is the process of the word, the process is to solve the problem steps, based on the process to design the program is like in the design # an industrial assembly line, is a mechanical way of thinking # Advantages: The program structure is clear, can be complex problem simplification, process # disadvantage: poor scalability, A streamline just to solve a problem # application scenario: Linux kernel, Git,httpd,shell script
Three, the package
Create a folder Filepackage in the Filepackage folder __init__.py with __init__.py, Filepackage is a package, otherwise it is just a normal folder.
When importing a module, the interpreter looks for the module in the current package, if it is not found, and then finds it in the built-in built-in module, the corresponding module file (module name. py) is found in the path given by Sys.path.
The initial value of the Sys.path comes from the following places:
Contains the current path of the script, the current path
PYTHONPATH
Default installation path
Sys.path can be changed after initialization is complete
Compiled python file:. pyc file
To organize modules, multiple modules are divided into a single package. The package is the directory where the Python module file resides, and the file must exist in that directory __init__.py . The common package structure is as follows:
Package_a├──__init__.py├──module_a1.py└──module_a2.pypackage_b├──__init__.py├──module_b1.py└──module_ b2.pymain.py
- If you
main.py want to reference packagea a module module in a1 , you can use:
From package_a import Module_a1import package_a.module_a1
- If
packagea a module a1 reference is required in packageb , Python is not package found by default b . We can use that in sys.path.append(‘../‘) packagea The __init__.py add this sentence, and then the package under all module will be added * import __init _ .
Iv. common modules and development specifications
1import logginglogging.debug ('This isdebug message') Logging.info ('This isinfo message') logging.warning (' This is warning message' is warning message
By default, logging prints the log to the screen with a log level of warning;
Log level size relationships are: CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET, and of course you can define the log level yourself.
2. Configure the output format and mode of the log by Logging.basicconfig function
Import logging
Logging.basicconfig (level=logging. DEBUG,
format= '% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s ',
Datefmt= '%a,%d%b%Y%h:%m:%s ',
Filename= ' Myapp.log ',
Filemode= ' W ')
Logging.debug (' This is Debug message ')
Logging.info (' This is Info message ')
Logging.warning (' This is warning message ')
3.logging Log Rollback
|
import logging
from logging.handlers import RotatingFileHandler
#################################################################################################
#定义一个RotatingFileHandler,最多备份5个日志文件,每个日志文件最大10M
Rthandler = RotatingFileHandler(‘myapp.log‘, maxBytes=10*1024*1024,backupCount=5)
Rthandler.setLevel(logging.INFO)
formatter = logging.Formatter(‘%(name)-12s: %(levelname)-8s %(message)s‘)
Rthandler.setFormatter(formatter)
logging.getLogger(‘‘).addHandler(Rthandler)
################################################################################################
|
As can be seen from the above example and in this example, logging has a main object of log processing, and other processing methods are added through AddHandler.
Several handle methods of logging are as follows:
|
Logging. Streamhandler: Log output to stream, can be sys.stderr, sys.stdout, or file
Logging. Filehandler: Log output to File
Log rollback method, using Rotatingfilehandler and Timedrotatingfilehandler when actually used
Logging.handlers.BaseRotatingHandler
Logging.handlers.RotatingFileHandler
Logging.handlers.TimedRotatingFileHandler
Logging.handlers.SocketHandler: Remote output log to TCP/IP sockets
Logging.handlers.DatagramHandler: Remote output log to UDP sockets
Logging.handlers.SMTPHandler: Remote output log to e-mail address
Logging.handlers.SysLogHandler: Log Output to Syslog
Logging.handlers.NTEventLogHandler: Remote output log to Windows NT/2000/XP event log
Logging.handlers.MemoryHandler: Log output to in-memory development buffer
Logging.handlers.HTTPHandler: Remote output to HTTP server via "GET" or "POST" |
Because Streamhandler and filehandler are common log processing methods, they are included directly in the logging module, while others are included in the Logging.handlers module.
Please refer to the python2.5 manual for the use of the other processing methods described above.
python--function continued