timmy / MACDCalculator 類
0 likes
0 forks
1 files
Last active 1 year 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 |
Last active 1 year 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 1 year ago
此程式提供一系列函數,方便處理日期與時間,包括取得月份、季度範圍、時間增減與計算日期範圍等,適用於財務分析、報表生成與時間管理應用。
| 1 | import calendar # 匯入 calendar 模組 |
| 2 | import math |
| 3 | from datetime import date, datetime, time |
| 4 | |
| 5 | import pytz |
| 6 | from dateutil.relativedelta import relativedelta |
| 7 | |
| 8 | |
| 9 | def date_type(value): |
| 10 | return (datetime if isinstance(value, datetime) else date) # 根據值的類型返回 datetime 或 date # fmt: skip |
Last active 1 year 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 1 year 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 1 year 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'} |
timmy / Python Mixin 類別設計
1 likes
0 forks
1 files
Last active 1 year ago
這段程式碼展示了如何使用 Mixin 類別來提供額外功能。GraphicMixin 提供 draw() 方法,而 ColorMixin 提供 set_color() 方法,ColoredGraphic 繼承這兩個 Mixin,使其具備繪圖與設定顏色的功能。這種設計可以在不影響主要類別結構的情況下,為不同類別添加額外行為。
| 1 | class GraphicMixin: |
| 2 | def draw(self): |
| 3 | print("Drawing the graphic") |
| 4 | |
| 5 | class ColorMixin: |
| 6 | def set_color(self, color): |
| 7 | self.color = color |
| 8 | print(f"Setting color to {color}") |
| 9 | |
| 10 | class ColoredGraphic(ColorMixin, GraphicMixin): |
timmy / Python 類別與方法
0 likes
0 forks
1 files
Last active 1 year ago
這段程式碼定義了一個 GreetingClass 類別,內含兩個方法 say_hello() 和 say_goodbye(),用來回傳問候語。透過建立 greeter 物件,可以呼叫這些方法,並測試它們是否符合預期的輸出,適合作為 Python 類別與方法的基礎學習示例。
| 1 | class GreetingClass: |
| 2 | name = "user" |
| 3 | |
| 4 | def say_hello(self): |
| 5 | """Class method.""" |
| 6 | return "Hello " + self.name |
| 7 | |
| 8 | def say_goodbye(self): |
| 9 | """Class method.""" |
| 10 | return "Goodbye " + self.name |
timmy / 投資報酬率 (ROI) 計算
0 likes
0 forks
1 files
Last active 1 year ago
這個 Python 函數計算投資報酬率 (ROI),衡量投資的盈利能力。ROI 公式為 (投資收益 - 投資成本) / 投資成本 * 100,用於評估投資回報的效率,適用於個人理財、企業投資和專案決策。
| 1 | def calculate_roi(investment_cost, investment_gain): |
| 2 | """ |
| 3 | 計算投資報酬率 (ROI) 的函數。 |
| 4 | |
| 5 | :param investment_cost: 投資成本 |
| 6 | :param investment_gain: 投資收益 |
| 7 | :return: 投資報酬率 (ROI) |
| 8 | """ |
| 9 | net_profit = investment_gain - investment_cost |
| 10 | roi = (net_profit / investment_cost) * 100 |
Last active 1 year ago
這兩個 Python 程式分別計算全年投資與不足一年投資的年化報酬率。calculate_annualized_return.py 適用於完整投資年度,而 calculate_annualized_return_for_part_years_investment.py 則適用於短期投資,允許投資期限以小數表示(如 6 個月 = 0.5 年)。這些函數可用於評估投資績效,幫助投資者比較不同投資期間的回報率。
| 1 | def calculate_annualized_return(initial_investment, final_value, investment_period): |
| 2 | """ |
| 3 | 計算年化報酬率的函數。 |
| 4 | |
| 5 | :param initial_investment: 初始投資金額 |
| 6 | :param final_value: 投資終值 |
| 7 | :param investment_period: 投資期限(年) |
| 8 | :return: 年化報酬率 |
| 9 | """ |
| 10 | annualized_return = ((final_value / initial_investment) ** (1 / investment_period)) - 1 |