Python Basic grammar grooming (II.)

Source: Internet
Author: User

1, gevent use

#-*-coding:utf-8-*-ImportgeventImportPlatform fromGeventImportsubprocessdef_test_shell (script): Excute_data=[]; Err_data=[]; ifPlatform.system () = ="Windows": Popen=subprocess. Popen (script, Shell=false, Stdout=subprocess. PIPE, stderr=subprocess. PIPE) Excute_data=popen.stdout.readlines () err_data=Popen.stderr.readlines () forMsg_lineinchExcute_data:Print "return:%s"%(Msg_line.strip ()) forErr_lineinchErr_data:Print "err:%s"%(Err_line.strip ())defTalk (msg):Print(msg)def_test_spwan (): Gevent.spawn (talk,"Fredirc")if __name__=='__main__': _test_shell ('java-version') _test_spwan () Gevent.sleep (0.5)        #The above example prints out    #Err:java Version "1.6.0_37"    #Err:java (TM) SE Runtime Environment (build 1.6.0_37-b06)    #Err:java HotSpot (TM) Client VM (build 20.12-b01, mixed mode, sharing)    #Fredirc                

2, logging use

ImportLoggingImportOS fromLoggingImportFilehandlerdefGet_logger (): Logger= Logging.getlogger ('Fredric_file') Log_format='% (asctime) s% (filename) s% (Lineno) d% (levelname) s% (message) s'Formatter=logging. Formatter (Log_format) logfile= Os.path.join (Os.path.dirname (Os.path.abspath (__file__)),'Log','Fredric_log.log') Handler= Filehandler (logfile,"a") Handler.setformatter (formatter) Logger.addhandler (handler) Logger.setlevel (logging. ERROR)returnLoggerdefget_logger_console (): Logger= Logging.getlogger ("Fredric_console") Log_format='% (asctime) s% (filename) s% (Lineno) d% (levelname) s% (message) s'Formatter=logging. Formatter (log_format) Console=logging. Streamhandler () Console.setformatter (Formatter) Logger.addhandler (console) Logger.setlevel (logging. DEBUG)returnLoggerlogger_file=Get_logger () logger_console=Get_logger_console ()if __name__=='__main__': Logger_file.error ("Fiel Error") Logger_console.error ("Console Error") Logger_file.info ("Fiel Error") Logger_console.info ("Console Error")        

3. Imp use (similar to Java reflection)

#-*-coding:utf-8-*-ImportImpif __name__=='__main__':        #Get this modulemodule = Imp.load_module ("Test", *imp.find_module ("Test", ["./"]))        #instantiate the class in this moduleTemp=module.__getattribute__("Test")(); #methods included in the execution classTemp.do_staff ()#Printed: Do_staff:test Class        PrintTemp.__str__()#Printed: To_string:test Class    

Python Basic grammar grooming (II.)

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.