timmy / 使用 functools 進行函式工具操作
0 喜歡
0 分支
3 檔案
最後活躍 9 months ago
functools 模組提供多種函式工具,如記憶化(lru_cache)、部分應用(partial)和函式包裝(wraps),適用於提升效能、簡化回呼函式與裝飾器開發。
| 1 | import functools |
| 2 | |
| 3 | @functools.lru_cache(maxsize=5) |
| 4 | def fibonacci(n): |
| 5 | """計算費氏數列(具備快取功能)""" |
| 6 | if n < 2: |
| 7 | return n |
| 8 | return fibonacci(n - 1) + fibonacci(n - 2) |
| 9 | |
| 10 | print(fibonacci(10)) # 快速計算 |
timmy / 台灣假日 API 查詢與快取
0 喜歡
0 分支
1 檔案
最後活躍 10 months ago
這段程式碼是一個節假日查詢工具,從指定 API 獲取節假日資料,支援緩存與分頁,並提供按年份篩選節假日、檢查特定日期是否為節假日的功能,同時以 Holiday 類別封裝每個節假日的詳細資訊,便於操作與顯示。
| 1 | import requests |
| 2 | import os |
| 3 | import json |
| 4 | |
| 5 | class HolidayAPI: |
| 6 | def __init__(self, base_url, cache_dir="cache"): |
| 7 | """ |
| 8 | Initialize the HolidayAPI class. |
| 9 | :param base_url: The base URL for the API endpoint. |
| 10 | :param cache_dir: Directory to store cached data. |
上一頁
下一頁