Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I added some changes on my branch. Please Review #3717

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added models/engine/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
20 changes: 20 additions & 0 deletions models/engine/db_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ def all(self, cls=None):
new_dict[key] = obj
return (new_dict)

def get(self, cls, id):
"""Returns the object based on the class and its ID"""
if cls is None:
return None

if cls in classes.values():
obj = self.__session.query(cls).filter_by(id=id).first()
return obj
return None

def count(self, cls=None):
"""count the number of objects in storage"""
if cls is None:
total_count = 0
for clss in classes.values():
total_count += self.__session.query(clss).count()
return total_count
else:
return self.__session.query(cls).count()

def new(self, obj):
"""add the object to the current database session"""
self.__session.add(obj)
Expand Down
22 changes: 21 additions & 1 deletion models/engine/file_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ def all(self, cls=None):
return new_dict
return self.__objects

def get(self, cls, id):
"""Returns the object based on the class and its ID"""
if cls is None:
return None
for key, value in self.__objects.items():
if cls == value.__class__ and id == value.id:
return value
return None

def count(self, cls=None):
"""count the number of objects in storage"""

if cls is None:
return len(self.__objects)
else:
return sum(
1 for k in self.__objects.keys()
if eval(cls) in k
)

def new(self, obj):
"""sets in __objects the obj with key <obj class name>.id"""
if obj is not None:
Expand All @@ -55,7 +75,7 @@ def reload(self):
jo = json.load(f)
for key in jo:
self.__objects[key] = classes[jo[key]["__class__"]](**jo[key])
except:
except Exception:
pass

def delete(self, obj=None):
Expand Down