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 |
Last active 1 year ago
此 Python 程式碼定義了一個 Card 類別,用來表示撲克牌的花色 (suit) 和數字 (number)。該類別包含屬性驗證機制,以確保設定的數值符合撲克牌的標準。此外,程式碼還建立了兩張撲克牌,並透過 assert 來驗證對象的變更是否符合預期,可用於模擬撲克牌遊戲的基礎單元。
| 1 | """ |
| 2 | 撲克牌 |
| 3 | """ |
| 4 | |
| 5 | |
| 6 | class Card: # 類別 |
| 7 | """ |
| 8 | 撲克牌 |
| 9 | """ |
timmy / 使用 asyncio 與 threading 進行非同步延遲執行
0 likes
0 forks
1 files
Last active 1 year ago
這段程式碼結合了 Python 的 asyncio 和 threading,透過 loop.run_in_executor 在背景執行同步的 blocking_sleep(),避免阻塞 asyncio 事件迴圈。這允許 async_sleep() 在等待時仍能執行其他非同步任務,適用於需要在 asyncio 應用程式中處理阻塞性 I/O 操作(如 time.sleep())的情境。
| 1 | import asyncio |
| 2 | import threading |
| 3 | import time |
| 4 | |
| 5 | async def delayed_execution(): |
| 6 | # 在這裡執行延遲後的任務 |
| 7 | print("Delayed execution") |
| 8 | |
| 9 | def blocking_sleep(seconds): |
| 10 | time.sleep(seconds) |
timmy / 使用 Threading 進行非同步延遲執行
0 likes
0 forks
1 files
Last active 1 year ago
這段程式碼使用 Python 的 threading 模組來實現非同步延遲執行。當 async_sleep(seconds) 被呼叫時,它會建立一個新的執行緒來執行 sleep() 函式,而不會阻塞主執行緒。這允許主程式繼續執行其他任務,同時在指定時間後執行 delayed_execution()。這對於需要非同步延遲執行的應用場景,如計時器或背景任務,特別有用。
| 1 | import threading |
| 2 | import time |
| 3 | |
| 4 | def delayed_execution(): |
| 5 | # 在這裡執行延遲後的任務 |
| 6 | print("Delayed execution") |
| 7 | |
| 8 | def async_sleep(seconds): |
| 9 | def sleep(): |
| 10 | time.sleep(seconds) |
timmy / 使用 argparse 解析命令列參數
0 likes
0 forks
1 files
Last active 1 year ago
這段程式碼使用 Python 的 argparse 模組來解析命令列參數。它支援必填的位置參數(輸入檔案路徑)、選填的輸出檔案路徑(-o 或 --output),以及一個開關參數(-v 或 --verbose)來啟用詳細模式。這適用於 CLI 工具,使得用戶可以透過命令列提供不同的參數來控制程式行為。
| 1 | import argparse |
| 2 | |
| 3 | # 建立參數解析器物件 |
| 4 | parser = argparse.ArgumentParser(description='這是一個範例程式,使用 argparse 模組解析命令列參數') |
| 5 | |
| 6 | # 添加位置參數 |
| 7 | parser.add_argument('input_file', help='輸入檔案的路徑') |
| 8 | |
| 9 | # 添加選擇性參數 |
| 10 | parser.add_argument('-o', '--output', help='輸出檔案的路徑') |
timmy / Streamlit 整合 Lit Web Component
0 likes
0 forks
1 files
Last active 1 year ago
此範例展示如何在 Streamlit 應用中嵌入 Lit Web Component,而不依賴 CDN。使用 Web Components 技術,可封裝自訂 UI 元件,並透過 JavaScript 互動,適用於建立模組化的前端元件,提升 Web 應用的可維護性與重用性。
| 1 | import streamlit as st |
| 2 | import streamlit.components.v1 as components |
| 3 | |
| 4 | st.title("使用 Lit Web Component (無 CDN)") |
| 5 | |
| 6 | lit_component = """ |
| 7 | <script type="module"> |
| 8 | class MyLitComponent extends HTMLElement { |
| 9 | constructor() { |
| 10 | super(); |
timmy / Streamlit 聊天應用示範
0 likes
0 forks
1 files
Last active 1 year ago
| 1 | import streamlit as st |
| 2 | import random |
| 3 | import time |
| 4 | |
| 5 | st.write("Streamlit loves LLMs! 🤖 [Build your own chat app](https://docs.streamlit.io/develop/tutorials/llms/build-conversational-apps) in minutes, then make it powerful by adding images, dataframes, or even input widgets to the chat.") |
| 6 | |
| 7 | st.caption("Note that this demo app isn't actually connected to any LLMs. Those are expensive ;)") |
| 8 | |
| 9 | # Initialize chat history |
| 10 | if "messages" not in st.session_state: |
timmy / Streamlit 大規模資料表格與互動編輯
0 likes
0 forks
1 files
Last active 1 year ago
此範例展示如何使用 Streamlit 處理大量資料,並提供可視化表格 (dataframe) 和可編輯表格 (data_editor) 來動態調整資料。支援圖片預覽、進度條顯示及類別選擇,適用於資料分析與管理應用。
| 1 | import streamlit as st |
| 2 | import pandas as pd |
| 3 | import numpy as np |
| 4 | |
| 5 | st.write("Got lots of data? Great! Streamlit can show [dataframes](https://docs.streamlit.io/develop/api-reference/data) with hundred thousands of rows, images, sparklines – and even supports editing! ✍️") |
| 6 | |
| 7 | num_rows = st.slider("Number of rows", 1, 10000, 500) |
| 8 | np.random.seed(42) |
| 9 | data = [] |
| 10 | for i in range(num_rows): |
timmy / Streamlit 資料視覺化與互動選擇
0 likes
0 forks
1 files
Last active 1 year ago
此範例展示如何使用 Streamlit 建立互動式數據視覺化,包含多重選擇 (multiselect) 和切換開關 (toggle) 來調整數據顯示,並透過 line_chart 繪製折線圖,讓使用者輕鬆分析數據趨勢。
| 1 | import streamlit as st |
| 2 | import pandas as pd |
| 3 | import numpy as np |
| 4 | |
| 5 | st.write("Streamlit supports a wide range of data visualizations, including [Plotly, Altair, and Bokeh charts](https://docs.streamlit.io/develop/api-reference/charts). 📊 And with over 20 input widgets, you can easily make your data interactive!") |
| 6 | |
| 7 | all_users = ["Alice", "Bob", "Charly"] |
| 8 | with st.container(border=True): |
| 9 | users = st.multiselect("Users", all_users, default=all_users) |
| 10 | rolling_average = st.toggle("Rolling average") |