-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_storage.py
45 lines (39 loc) · 1.46 KB
/
file_storage.py
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
#!/usr/bin/python3
""" class FileStorage
serializes instances to a JSON file
and deserializes JSON file to instances """
import json
import uuid
import os
from datetime import datetime
from models.base_model import BaseModel
from models.user import User
from models.state import State
from models.city import City
from models.amenity import Amenity
from models.place import Place
from models.review import Review
class FileStorage:
""" construct """
__file_path = "file.json"
__objects = {}
def all(self):
""" return dictionary objects """
return FileStorage.__objects
def new(self, obj):
""" sets in dictionary the obj with key <obj class name>.id """
FileStorage.__objects[obj.__class__.__name__ + "." + str(obj.id)] = obj
def save(self):
""" serializes objectss to the JSON file (path: __file_path) """
with open(FileStorage.__file_path, 'w', encoding='utf-8') as fname:
new_dict = {key: obj.to_dict() for key, obj in
FileStorage.__objects.items()}
json.dump(new_dict, fname)
def reload(self):
""" Reload the file """
if (os.path.isfile(FileStorage.__file_path)):
with open(FileStorage.__file_path, 'r', encoding="utf-8") as fname:
l_json = json.load(fname)
for key, val in l_json.items():
FileStorage.__objects[key] = eval(
val['__class__'])(**val)