-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.py
More file actions
45 lines (39 loc) · 1.51 KB
/
classes.py
File metadata and controls
45 lines (39 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# classes.py
from pathlib import Path
from typing import Dict
class User:
def __init__(self, name: str = '', tasks: int = 0):
self.name = name
self.tasks = tasks
self.config_path = Path("config.txt")
def incr_task(self) -> None:
"""Increment the task count and update the configuration."""
self.tasks += 1
self.update_config()
def decr_task(self) -> None:
"""Decrement the task count and update the configuration."""
self.tasks -= 1
self.update_config()
if self.tasks < 0:
self.tasks = 0
self.update_config()
def update_config(self) -> None:
"""Update the configuration file with current user data."""
config = self.read_config()
config['name'] = self.name
config['tasks'] = str(self.tasks)
self.write_config(config)
def read_config(self) -> Dict[str, str]:
"""Read the configuration file and return as a dictionary."""
config = {}
if self.config_path.exists():
with self.config_path.open('r') as file:
for line in file:
key, value = line.strip().split(" = ")
config[key] = value.strip("'")
return config
def write_config(self, config: Dict[str, str]) -> None:
"""Write the configuration dictionary to the config file."""
with self.config_path.open('w') as file:
for key, value in config.items():
file.write(f"{key} = '{value}'\n")