Advertisement
ksieradzinski

Untitled

Mar 19th, 2025
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. # Importuje to co pochodzi z systemu
  2. import asyncio
  3. import json
  4. from pathlib import Path
  5.  
  6. # Importuje to co doinstalowałem
  7. import aiofiles
  8.  
  9. # Importuje moje własne pliki
  10.  
  11. TASKS_FILE = Path("tasks.json")
  12.  
  13. async def load_tasks():
  14. if not TASKS_FILE.exists():
  15. return []
  16.  
  17. async with aiofiles.open(TASKS_FILE, "r", encoding="utf8") as file:
  18. content = await file.read()
  19. return json.loads(content) if content else []
  20.  
  21. async def save_tasks(tasks):
  22. async with aiofiles.open(TASKS_FILE, "w", encoding="utf8") as file:
  23. await file.write(json.dumps(tasks, indent=1))
  24.  
  25. async def add_task(description):
  26. tasks = await load_tasks()
  27. tasks.append({"description": description, "done": False})
  28. await save_tasks(tasks)
  29.  
  30. async def list_tasks():
  31. tasks = await load_tasks()
  32. return tasks
  33.  
  34. async def complete_tasks(index):
  35. tasks = await load_tasks()
  36. if 0 <= index <= len(tasks):
  37. tasks[index]["done"] = True
  38. await save_tasks(tasks)
  39. return True
  40.  
  41. return False
  42.  
  43.  
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement