1. 实际应用场景描述
场景:
小王家里和车库各有一个灭火器,但他经常忘记检查压力表指针是否在绿色区域,也记不清灭火器的生产日期和有效期。某次厨房小火灾时,发现灭火器已经过期,险些酿成大祸。
目标:
通过一个简单的工具,录入灭火器信息(位置、有效期),设置每月检查提醒(压力、外观),并在到期前提醒更换,确保灭火器始终可用。
2. 痛点引入
- 遗忘检查:灭火器需要每月检查压力、外观,但容易忘记。
- 有效期不明:不清楚灭火器何时到期,可能超期使用。
- 位置分散:多个灭火器分布在家中不同位置,管理混乱。
- 紧急情况失效:检查时发现压力不足或过期,但为时已晚。
3. 核心逻辑讲解
1. 数据录入:用户添加灭火器信息(位置、生产日期、有效期月数)。
2. 计算到期日:生产日期 + 有效期 = 到期日。
3. 检查提醒:
- 每月固定日期提醒检查压力、外观。
- 提前 N 天提醒更换即将到期的灭火器。
4. 数据存储:使用 JSON 文件保存灭火器列表,方便持久化。
5. 模块化设计:
-
"Extinguisher" 类:灭火器实体
-
"ReminderManager" 类:提醒管理
-
"Storage" 类:数据读写
6. 命令行交互:简单易用,适合家庭用户。
4. 模块化代码(Python)
# fire_extinguisher_reminder.py
import json
import os
from datetime import datetime, timedelta
class Extinguisher:
"""
灭火器类
"""
def __init__(self, location, production_date, valid_months):
self.location = location
self.production_date = datetime.strptime(production_date, "%Y-%m-%d")
self.valid_months = valid_months
self.expiry_date = self.production_date + timedelta(days=valid_months*30)
def to_dict(self):
return {
"location": self.location,
"production_date": self.production_date.strftime("%Y-%m-%d"),
"valid_months": self.valid_months
}
@staticmethod
def from_dict(data):
return Extinguisher(data["location"], data["production_date"], data["valid_months"])
class Storage:
"""
数据持久化类
"""
FILE_NAME = "extinguishers.json"
@staticmethod
def load():
if not os.path.exists(Storage.FILE_NAME):
return []
with open(Storage.FILE_NAME, "r", encoding="utf-8") as f:
data = json.load(f)
return [Extinguisher.from_dict(item) for item in data]
@staticmethod
def save(extinguishers):
with open(Storage.FILE_NAME, "w", encoding="utf-8") as f:
json.dump([e.to_dict() for e in extinguishers], f, ensure_ascii=False, indent=2)
class ReminderManager:
"""
提醒管理类
"""
def __init__(self):
self.extinguishers = Storage.load()
def add_extinguisher(self, location, production_date, valid_months):
ext = Extinguisher(location, production_date, valid_months)
self.extinguishers.append(ext)
Storage.save(self.extinguishers)
print(f"已添加灭火器:{location},到期日:{ext.expiry_date.strftime('%Y-%m-%d')}")
def list_extinguishers(self):
if not self.extinguishers:
print("暂无灭火器记录。")
return
print("\n=== 灭火器列表 ===")
today = datetime.today()
for ext in self.extinguishers:
status = "已过期" if today > ext.expiry_date else f"剩余 {(ext.expiry_date - today).days} 天"
print(f"位置:{ext.location},到期日:{ext.expiry_date.strftime('%Y-%m-%d')},状态:{status}")
def check_reminders(self):
today = datetime.today()
print("\n=== 检查提醒 ===")
for ext in self.extinguishers:
# 每月1号提醒检查
if today.day == 1:
print(f"[检查提醒] {ext.location}:请检查压力表指针(绿色区域)和外观(无锈蚀、损坏)")
# 提前30天提醒更换
if (ext.expiry_date - today).days <= 30 and (ext.expiry_date - today).days > 0:
print(f"[更换提醒] {ext.location} 将在 {(ext.expiry_date - today).days} 天后到期,请及时更换!")
if today > ext.expiry_date:
print(f"[紧急] {ext.location} 已过期,请立即更换!")
def main():
manager = ReminderManager()
while True:
print("\n=== 家用灭火器检查提醒工具 ===")
print("1. 添加灭火器")
print("2. 查看灭火器列表")
print("3. 检查提醒")
print("4. 退出")
choice = input("请选择操作:").strip()
if choice == "1":
location = input("请输入灭火器位置:")
production_date = input("请输入生产日期(YYYY-MM-DD):")
valid_months = int(input("请输入有效期(月数):"))
manager.add_extinguisher(location, production_date, valid_months)
elif choice == "2":
manager.list_extinguishers()
elif choice == "3":
manager.check_reminders()
elif choice == "4":
print("再见!")
break
else:
print("无效选择,请重新输入。")
if __name__ == "__main__":
main()
5. README 与使用说明
README.md
# 家用灭火器检查提醒工具
一个基于 Python 的工具,帮助家庭记录灭火器位置、有效期,并设置每月检查提醒(压力、外观)以及到期更换提醒,确保灭火器始终可用。
## 功能
- 添加灭火器(位置、生产日期、有效期)
- 查看灭火器列表及状态
- 每月检查提醒
- 到期前更换提醒
## 使用方法
1. 安装 Python 3.x
2. 下载 `fire_extinguisher_reminder.py`
3. 运行:
bash
python fire_extinguisher_reminder.py
4. 按菜单选择操作
## 数据存储
- 数据保存在 `extinguishers.json` 文件中
## 示例
添加:
位置:厨房
生产日期:2022-05-01
有效期:60
检查提醒:
[检查提醒] 厨房:请检查压力表指针(绿色区域)和外观(无锈蚀、损坏)
[更换提醒] 厨房 将在 15 天后到期,请及时更换!
## 许可证
MIT
6. 核心知识点卡片
知识点 说明
面向对象设计
"Extinguisher"、
"Storage"、
"ReminderManager" 分工明确
JSON 持久化 使用
"json" 模块存储数据,简单易用
日期处理
"datetime" 计算到期日、剩余天数
模块化 数据、逻辑、存储分离,便于维护
提醒逻辑 每月固定日检查 + 提前 N 天更换提醒
用户体验 命令行菜单交互,适合非技术用户
7. 总结
这个工具的核心价值:
- 安全保障:确保灭火器在紧急情况下可用。
- 自动化提醒:减少人为遗忘,养成定期检查习惯。
- 简单易用:适合家庭用户,无需复杂配置。
下一步建议:
1. 增加 GUI 界面(Tkinter 或 PyQt)。
2. 接入 手机推送(如 Pushbullet、Telegram Bot)。
3. 增加 图片记录功能,拍照存档灭火器状态。
如果你愿意,可以把这个工具升级成 带图形界面的桌面应用,并加上 手机通知功能,让它真正成为智能时代的家庭安全助手。
利用AI解决实际问题,如果你觉得这个工具好用,欢迎关注长安牧笛!