2018-10-19 KK日記、Markはいくつかpythonの面白いコードを少し見ます


いくつかの面白い命令を記録します
  • 端末ウィンドウを開き、コマンドを実行します.コードは次のとおりです.
  •  #bash shell  
     gnome-terminal -e 'bash -c "./ccurl.py;exec bash"' 
     
     #python   
    
    #! /usr/bin/env python
    # -*-coding=utf-8 -*-
    import os
    if __name__=='__main__':
        os.system("gnome-terminal -e 'bash -c \"ls;exec bash \"'")
    
  • python呼び出しcx_oracle同時接続オプション
  • conn=cx_Oracle.connect('username','password','Tnsname',threaded=True)
    or
    conn=cx_Oracle.connect('username/password@host/service_name',threaded=True)
    
    
  • python threadingのクラス関数
  • を呼び出す
    threading.currentThread() #     ID
    
  • python呼び出しosコマンドのいくつかの書き方
  • import os 
    os.system('ls') #         0,  1,      
    stdout=os.popen('ls') #  file      ,        
    
    
    import commands # linux shell   
    stdout=commands.getoutput(command) #    stdout string,     
    
    commands.getstatusoutput('shell command')
      shell  ,          tuple(status, result),status int  ,result string  
    
  • pythonの時間フォーマットは、直接演算可能な
  • である.
    from datetime import *
    startTime=datetime.now()
    endTime=datetime.now()
    duringTime=(endTime-startTime).seconds
    
    
  • オペレーティングシステムバージョン
  • を判断
    #!/bin/python
    #
    import platform
    
    def TestPlatform():
        print ("----------Operation System--------------------------")
        #Windows will be : (32bit, WindowsPE)
        #Linux will be : (32bit, ELF)
        print(platform.architecture())
    
        #Windows will be : Windows-XP-5.1.2600-SP3 or Windows-post2008Server-6.1.7600
        #Linux will be : Linux-2.6.18-128.el5-i686-with-redhat-5.3-Final
        print(platform.platform())
    
        #Windows will be : Windows
        #Linux will be : Linux
        print(platform.system())
    
        print ("--------------Python Version-------------------------")
        #Windows and Linux will be : 3.1.1 or 3.1.3
        print(platform.python_version())
    
    def UsePlatform():
      sysstr = platform.system()
      if(sysstr =="Windows"):
        print ("Call Windows tasks")
      elif(sysstr == "Linux"):
        print ("Call Linux tasks")
      else:
        print ("Other System tasks"