Opengist

Explore

  • All gists
  • Topics
  • Users
Give feedback on the new UI Powered by Opengist ⋅ 72ms

user:thomas gists created by user

title:mygist gists with given title

description:sync gists with given description

filename:myfile.txt gists having files with given name

extension:yml gists having files with given extension

language:go gists having files with given language

topic:homelab gists with given topic

all:systemctl search all fields

Login
D

DesignPattern

Recently created Least recently created Recently updated Least recently updated

timmy / Python Mixin 設計模式

Last active 6 hours ago Audit DesignPattern EventHandling ExceptionHandling Logging Mixin OOP Permission Python

Python Mixin 設計模式允許類別透過多重繼承增加功能,如日誌、權限管理、事件驅動、異常處理與審計,提升可維護性與擴展性,適用於模組化應用開發。

0 0 0 1
1 import datetime
2 import json
3 import threading
4 import traceback
5
6 class LoggingMixin:
7 def __init__(self, log_to_file=False, log_file="app.log", *args, **kwargs):
8 self.log_to_file = log_to_file
9 self.log_file = log_file
10 super().__init__(*args, **kwargs)

timmy / Python Mixin 設計模式

Last active 6 hours ago DesignPattern Event Handling Logging Mixin OOP Permission Python

Mixin 讓類別能以組合方式擴展功能,如日誌記錄、時間標記、權限管理、事件驅動等,提高程式的模組化與可維護性。適合用於 Python 物件導向開發。

0 0 0 1
1 import datetime
2 import json
3 import traceback
4 import threading
5
6 class LoggingMixin:
7 """ 提供日誌功能的 Mixin 類別,可選擇寫入控制台或檔案 """
8
9 def __init__(self, log_to_file=False, log_file="app.log", *args, **kwargs):
10 self.log_to_file = log_to_file