在这篇博客中,我们将介绍如何使用Python和SQLite数据库创建一个简单的个人记账应用。这个应用程序允许用户记录收入和支出,并生成月度或年度报告。我们将逐步讲解代码的实现,并展示如何运行这个应用程序。
步骤
- 导入库:首先,我们需要导入
sqlite3
和datetime
模块。 - 创建数据库连接和表:创建一个数据库连接,并创建一个名为
transactions
的表,用于存储交易记录。 - 定义函数:定义两个函数,一个用于向数据库中添加交易记录,另一个用于生成月度或年度报告。
- 主程序:提供一个简单的命令行界面,允许用户选择记录收入、记录支出、生成月度报告、生成年度报告或退出程序。
代码解析
- 导入库:
import sqlite3 from datetime import datetime
- 创建数据库连接和表:
# 创建数据库连接 conn = sqlite3.connect('finance.db') c = conn.cursor() # 创建表 c.execute('''CREATE TABLE IF NOT EXISTS transactions (id INTEGER PRIMARY KEY, date TEXT, type TEXT, amount REAL, description TEXT)''') conn.commit()
- 定义函数:
def add_transaction(date, trans_type, amount, description): c.execute("INSERT INTO transactions (date, type, amount, description) VALUES (?, ?, ?, ?)", (date, trans_type, amount, description)) conn.commit() def generate_report(period): if period == 'monthly': c.execute("SELECT strftime('%Y-%m', date) as month, type, SUM(amount) FROM transactions GROUP BY month, type") elif period == 'yearly': c.execute("SELECT strftime('%Y', date) as year, type, SUM(amount) FROM transactions GROUP BY year, type") report = c.fetchall() for row in report: print(row)
- 主程序:
def main(): while True: print("\n个人记账应用") print("1. 记录收入") print("2. 记录支出") print("3. 生成月度报告") print("4. 生成年度报告") print("5. 退出") choice = input("请选择操作: ") if choice == '1': date = input("请输入日期 (YYYY-MM-DD): ") amount = float(input("请输入金额: ")) description = input("请输入描述: ") add_transaction(date, '收入', amount, description) print("收入记录成功!") elif choice == '2': date = input("请输入日期 (YYYY-MM-DD): ") amount = float(input("请输入金额: ")) description = input("请输入描述: ") add_transaction(date, '支出', amount, description) print("支出记录成功!") elif choice == '3': print("月度报告:") generate_report('monthly') elif choice == '4': print("年度报告:") generate_report('yearly') elif choice == '5': break else: print("无效的选择,请重新选择。") if __name__ == "__main__": main() conn.close()
完整代码
import psutil
import time
def get_current_processes():
# 获取当前所有进程的pid和名称
return {p.pid: p.name() for p in psutil.process_iter(['pid', 'name'])}
def print_new_processes(old_processes):
# 获取新的进程
current_processes = get_current_processes()
new_processes = {pid: name for pid, name in current_processes.items() if pid not in old_processes}
# 打印新进程的pid和名称
for pid, name in new_processes.items():
print(f"New process detected: PID={pid}, Name={name}")
# 打印新进程的数量
print(f"Number of new processes: {len(new_processes)}")
return current_processes
if __name__ == "__main__":
processes = get_current_processes()
while True:
processes = print_new_processes(processes)
time.sleep(1) # 每秒检查一次
运行结果
运行该脚本后,程序将提示你输入日期、金额和描述来记录收入或支出。选择生成月度或年度报告来查看汇总的收入和支出。例如:
New process detected: PID=1234, Name=python.exe
Number of new processes: 1
结论
这个脚本使用psutil
库来监控系统中的新进程。通过每秒检查一次系统中的进程,并打印出新创建的进程的PID和名称,我们可以更好地理解系统的行为和进行性能分析。希望这个脚本对你有所帮助!
其他文章推荐
- 使用 Python和moviepy库 将MP4视频 文件转换为GIF动画-CSDN博客
- 在 Python 中以特定格式打印所有 ASCII 字符-CSDN博客
- Python 创建一个简单的在线聊天系统-CSDN博客
- 使用 Python 的 math 库进行基本的数学计算-CSDN博客
- PyQt5 创建个人记账应用-CSDN博客
总结
这个简单的Python脚本可以帮助我们监控新进程的创建,对于理解系统行为和进行性能分析非常有用。希望你喜欢这篇博客,如果有任何问题或建议,欢迎留言讨论。下次再见!