-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
executable file
·32 lines (23 loc) · 836 Bytes
/
data.py
File metadata and controls
executable file
·32 lines (23 loc) · 836 Bytes
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
import json
def get_data(
file_path: str = "data.json", film_id: int | None = None
) -> list[dict] | dict:
with open(file_path, "r") as fp:
films = json.load(fp)
if film_id != None and film_id < len(films):
return films[film_id]
return films
def add_data(film: dict, file_path: str = "data.json"):
films = get_data(file_path=file_path, film_id=None)
if films:
films.append(film)
with open(file_path, "w") as fp:
json.dump(films, fp, indent=4, ensure_ascii=False)
def get_film_name(name: str, file_path: str = "data.json"):
films = get_data(file_path=file_path)
for category in films:
for x, items in category.items():
if x == name:
return items
if __name__ == "__main__":
print(get_data(film_id=1))