發表文章

目前顯示的是有「example」標籤的文章

[Python] 快速使用Logging的範例

Python logging看起來很簡單,但是其實他的設定還蠻多的,以下提供一個快速可以套用的Log class給大家參考,我把它寫一個sigleton的Class,原因是一般開發程式會有很多個檔案,我希望在同一個地方把logging設定好,其他地方只要呼叫Log.Info('Hello')就可以輕易地使用。 import logging from logging.handlers import TimedRotatingFileHandler from pathlib import Path class Log: _instance = None def __new__ ( cls , *args , **kw): if not cls ._instance: cls ._instance = super (Log , cls ). __new__ ( cls , *args , **kw) return cls ._instance def __init__ ( self ): self .logger = logging.getLogger( "Log" ) self .logger.setLevel(logging.DEBUG) formatter = logging.Formatter( '%(asctime)s - %(levelname)s : %(message)s' ) log_path = 'logs/' Path(log_path).mkdir( parents = True, exist_ok = True ) handler = TimedRotatingFileHandler(log_path + 'example.log' , when = 'd' , backupCount = 10 ) handler.setFormatter(formatter) self .logger....

[Python] 使用 Pyinstaller打包成執行檔

圖片
   最近用Python寫了一個簡單選擇權交易模擬程式,想要給朋友試用看看,但是還要幫朋友的電腦安裝Python的環境,還有一大堆套件才能順利執行,後來發現Pyinstaller這個好東西,可以將Python程式打包成執行檔,實在是太棒了,今天就來寫個HelloWorld範例打包一下。 1.  安裝Pyinstaller             pip install pyinstaller         2.   準備好 HelloWorld.py                                                   3.   準備打包       pyinstaller  -F  HelloWorld.py         就會產生build和dist兩個資料夾跟一個設定檔HelloWorld.spec, build的資料夾裡面是一些工作檔案和紀錄,HelloWorld.spec裡面是一些打包的設定(之後再詳細說明),最重要的執行檔就產生在dist裡面了。 4.  執行HelloWorld      cd dist       ./HelloWorld              就會印出HelloWorld!!囉, 之後再來介紹比較複雜的程式打包。