""" 存储模块使用示例 演示常见的使用场景 """ import sys from pathlib import Path # 添加项目根目录到路径 project_root = Path(__file__).parent.parent sys.path.insert(0, str(project_root)) from src.core.storage import Storage def example_basic_usage(): """基本使用示例""" print("=" * 60) print("示例 1: 基本使用") print("=" * 60) storage = Storage() # 创建几条笔记 storage.create( title="项目会议记录", content="讨论了新功能的设计方案", category="工作", tags=["会议", "重要"] ) storage.create( title="Python 学习笔记", content="今天学习了列表推导式和装饰器", category="学习", tags=["Python", "编程"] ) storage.create( title="周末计划", content="周六去图书馆,周日看电影", category="生活" ) # 查看所有笔记 all_notes = storage.get_all() print(f"\n总共有 {len(all_notes)} 条笔记:") for note in all_notes: print(f" • {note['title']} [{note['category']}]") def example_search(): """搜索示例""" print("\n" + "=" * 60) print("示例 2: 搜索功能") print("=" * 60) storage = Storage() # 搜索包含"Python"的笔记 results = storage.search("Python") print(f"\n搜索 'Python' 找到 {len(results)} 条结果:") for note in results: print(f" • {note['title']}") # 搜索包含"会议"的笔记 results = storage.search("会议") print(f"\n搜索 '会议' 找到 {len(results)} 条结果:") for note in results: print(f" • {note['title']}") def example_category_management(): """分类管理示例""" print("\n" + "=" * 60) print("示例 3: 分类管理") print("=" * 60) storage = Storage() # 查看所有分类 categories = storage.get_categories() print(f"\n所有分类 ({len(categories)} 个):") for category in categories: count = len(storage.get_by_category(category)) print(f" • {category} ({count} 条笔记)") # 按分类查看笔记 print("\n工作分类下的笔记:") work_notes = storage.get_by_category("工作") for note in work_notes: print(f" • {note['title']}") def example_update_and_delete(): """更新和删除示例""" print("\n" + "=" * 60) print("示例 4: 更新和删除") print("=" * 60) storage = Storage() # 获取第一条笔记 all_notes = storage.get_all() if all_notes: first_note = all_notes[0] print(f"\n原始标题: {first_note['title']}") # 更新标题 updated = storage.update( first_note['id'], title=f"{first_note['title']}(已编辑)" ) print(f"更新后标题: {updated['title']}") # 删除最后一条笔记 if len(all_notes) > 1: last_note = all_notes[-1] storage.delete(last_note['id']) print(f"\n已删除: {last_note['title']}") def example_statistics(): """统计信息示例""" print("\n" + "=" * 60) print("示例 5: 统计信息") print("=" * 60) storage = Storage() stats = storage.get_stats() print(f"\n📊 笔记统计:") print(f" • 总记录数: {stats['total_records']}") print(f" • 总分类数: {stats['total_categories']}") print(f"\n各分类记录数:") for category, count in stats['categories'].items(): print(f" • {category}: {count}") def example_backup(): """备份示例""" print("\n" + "=" * 60) print("示例 6: 数据备份") print("=" * 60) storage = Storage() # 导出所有数据 data = storage.export_data() print(f"\n导出 {len(data)} 条记录") # 可以保存到备份文件 backup_file = Path("/tmp/notes_backup.json") import json with open(backup_file, 'w', encoding='utf-8') as f: json.dump(data, f, ensure_ascii=False, indent=2) print(f"备份已保存到: {backup_file}") if __name__ == "__main__": # 运行所有示例 example_basic_usage() example_search() example_category_management() example_update_and_delete() example_statistics() example_backup() print("\n" + "=" * 60) print("所有示例运行完成!") print("=" * 60)