forked from alexaorrico/AirBnB_clone_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_storage.py
executable file
·130 lines (109 loc) · 3.97 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/python3
"""
Contains the FileStorage class
"""
import json
from models.amenity import Amenity
from models.base_model import BaseModel
from models.city import City
from models.place import Place
from models.review import Review
from models.state import State
from models.user import User
classes = {"Amenity": Amenity, "BaseModel": BaseModel, "City": City,
"Place": Place, "Review": Review, "State": State, "User": User}
class FileStorage:
"""serializes instances to a JSON file & deserializes back to instances"""
# string - path to the JSON file
__file_path = "file.json"
# dictionary - empty but will store all objects by <class name>.id
__objects = {}
def all(self, cls=None):
"""returns the dictionary __objects"""
if cls is not None:
new_dict = {}
for key, value in self.__objects.items():
if cls == value.__class__ or cls == value.__class__.__name__:
new_dict[key] = value
return new_dict
return self.__objects
'''
def all(self, cls=None):
"""Returns a dictionary of models currently in storage"""
if cls is None:
return self.__objects
cls_name = cls.__name__
dct = {}
for key in self.__objects.keys():
if key.split('.')[0] == cls_name:
dct[key] = self.__objects[key]
return dct
'''
def new(self, obj):
"""sets in __objects the obj with key <obj class name>.id"""
if obj is not None:
key = obj.__class__.__name__ + "." + obj.id
self.__objects[key] = obj
def save(self):
"""serializes __objects to the JSON file (path: __file_path)"""
json_objects = {}
for key in self.__objects:
json_objects[key] = self.__objects[key].to_dict()
with open(self.__file_path, 'w') as f:
json.dump(json_objects, f)
def reload(self):
"""deserializes the JSON file to __objects"""
try:
with open(self.__file_path, 'r') as f:
jo = json.load(f)
for key in jo:
self.__objects[key] = classes[jo[key]["__class__"]](**jo[key])
except Exception as e:
pass
def delete(self, obj=None):
"""delete obj from __objects if it’s inside"""
if obj is not None:
key = obj.__class__.__name__ + '.' + obj.id
if key in self.__objects:
del self.__objects[key]
def close(self):
"""call reload() method for deserializing the JSON file to objects"""
self.reload()
def get(self, cls, id):
"""retrieves and returns an object based on ID
Or None if not found
cls: class
id: string representation of object ID"""
dct = {}
# Checks if id has been provided
if not id:
return None
# Checks if class exists
if cls.__name__ not in classes.keys():
return None
try:
for key, value in self.__objects.items():
if value.__class__ == cls and value.id == id:
return value
except Exception as e:
pass
return None
def count(self, cls=None):
"""
A method to count the number of objects in storage
Returns the number of objects in storage matching the given class.
If no class is passed, returns the count of all objects in storage.
"""
# Initialize counter to zero
count = 0
# If class name is not defined, count all objects in storage
if cls is None:
for objs in self.__objects:
return len(self.__objects)
# Else, iterate and check class name of individual objects and compare
else:
for key, value in self.__objects.items():
objs_cls = key.split(".")[0]
if cls.__name__ == objs_cls:
count += 1
return count