Last active 10 months ago
此 Python 程式透過 LoggingMixin 提供日誌功能,TimestampMixin 管理時間標記,並應用於 User 類別,使其具備自動記錄變更歷史的能力,適用於用戶管理、數據追蹤等應用。
1 import datetime
2
3 class LoggingMixin:
4 """ 提供日誌功能的 Mixin 類別 """
5
6 def log(self, message):
7 """ 簡單的日誌紀錄方法 """
8 timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
9 print(f"[{timestamp}] {self.__class__.__name__}: {message}")

timmy / LoggingMixin 日誌功能擴展

0 likes
0 forks
1 files
Last active 10 months ago
此 Mixin 類別為 Python 類別提供簡單的日誌紀錄功能,可用於追蹤物件的行為變化,適用於擴展任何需要日誌記錄的類別,如用戶管理、系統監控等應用。
1 import datetime
2
3 class LoggingMixin:
4 """ 提供日誌功能的 Mixin 類別 """
5
6 def log(self, message):
7 """ 簡單的日誌紀錄方法 """
8 timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
9 print(f"[{timestamp}] {self.__class__.__name__}: {message}")

timmy / MACDCalculator 類

0 likes
0 forks
1 files
Last active 10 months ago
用於計算股票的 MACD 指標,並識別黃金交叉和死亡交叉點,以協助投資者分析市場趨勢和決策。
1 # import shutil
2
3 # import matplotlib
4
5 import matplotlib.pyplot as plt
6 import numpy as np
7 import pandas as pd
8 from matplotlib import rcParams
9
10 import error_printer

timmy / 錯誤與除錯輸出配置

1 likes
0 forks
2 files
Last active 10 months ago
此程式使用 pretty_errors 改善錯誤輸出,並使用 icecream (ic) 進行簡潔的除錯訊息輸出,方便開發人員快速定位錯誤與分析變數內容,提高程式除錯效率。
1 import pretty_errors
2 from icecream import ic
3
4
5 def configure_pretty_errors() -> None:
6 """
7 配置 pretty_errors 庫,用於更好的錯誤輸出。
8
9 不需要任何參數。
10 """

timmy / 星座判定程式

0 likes
0 forks
1 files
Last active 10 months ago
此程式根據輸入的出生日期,判定並返回對應的星座名稱。它使用 Python 的 datetime 來解析日期,並比對星座日期範圍。可應用於星座分析、個性測試或相關娛樂應用。
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 constellation.py: Description of what this script does.
6
7 Author: Timmy
8 Copyright: Copyright 2022, Timmy
9 License: MIT
10 Version: 1.0

timmy / Python 字典與運算符應用

0 likes
0 forks
1 files
Last active 10 months ago
這段程式碼示範如何使用 Python 字典來映射數字到星期與月份名稱,並利用 _operators 列表定義一組比較運算符,以 apply_operator 函數動態執行不同類型的比較操作。適用於日期處理、數據過濾和條件判斷等應用場景。
1 # 定義一個字典 days,將數字對應到星期幾的名稱
2 days = {
3 "1": "Monday",
4 "2": "Tuesday",
5 "3": "Wednesday",
6 "4": "Thursday",
7 "5": "Friday",
8 "6": "Saturday",
9 "7": "Sunday",
10 }

timmy / Python 自訂字典類別

0 likes
0 forks
1 files
Last active 10 months ago
這段程式碼定義了一個 CustomDictionary 類別,繼承 Python 內建的 dict,並添加了一個 custom_property 屬性。該屬性返回自身,使得 custom_dict.custom_property 能夠訪問完整的字典內容。這種設計可用於擴展字典功能,提供自訂方法或屬性,而不影響原始字典行為。
1 class CustomDictionary(dict):
2 def __init__(self, dictionary):
3 super().__init__(dictionary)
4
5 @property
6 def custom_property(self):
7 return self
8
9 # 建立一個普通字典
10 my_dict = {'key1': 'value1', 'key2': 'value2'}