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

Storage get count #3682

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
Jennifer Huang <[email protected]>
Alexa Orrico <[email protected]>
Joann Vuong <[email protected]>
Uzor Solomon <[email protected]>
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ No known bugs at this time.
## Authors
Alexa Orrico - [Github](https://github.com/alexaorrico) / [Twitter](https://twitter.com/alexa_orrico)
Jennifer Huang - [Github](https://github.com/jhuang10123) / [Twitter](https://twitter.com/earthtojhuang)
Uzor Solomon

Second part of Airbnb: Joann Vuong
## License
Public Domain. No copy write protection.
Public Domain. No copy write protection.


Binary file added __pycache__/console.cpython-310.pyc
Binary file not shown.
9 changes: 5 additions & 4 deletions console.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ def _key_value_parser(self, args):
else:
try:
value = int(value)
except:
except Exception:
try:
value = float(value)
except:
except Exception:
continue
new_dict[key] = value
return new_dict
Expand Down Expand Up @@ -140,12 +140,12 @@ def do_update(self, arg):
if args[2] in integers:
try:
args[3] = int(args[3])
except:
except Exception:
args[3] = 0
elif args[2] in floats:
try:
args[3] = float(args[3])
except:
except Exception:
args[3] = 0.0
setattr(models.storage.all()[k], args[2], args[3])
models.storage.all()[k].save()
Expand All @@ -160,5 +160,6 @@ def do_update(self, arg):
else:
print("** class doesn't exist **")


if __name__ == '__main__':
HBNBCommand().cmdloop()
Binary file added models/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/amenity.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/base_model.cpython-310.pyc
Binary file not shown.
Binary file added models/engine/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file not shown.
25 changes: 25 additions & 0 deletions models/engine/db_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,28 @@ def reload(self):
def close(self):
"""call remove() method on the private session attribute"""
self.__session.remove()

def get(self, cls, id):
"""Get method."""
if cls not in classes.values():
return None

all_cls = models.storage.all(cls)
for value in all_cls.values():
if (value.id == id):
return value

return None

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

if not cls:
count = 0
for clas in all_classes:
count += len(models.storage.all(clas).values())
else:
count += len(models.storage.all(cls).values())

return count
29 changes: 28 additions & 1 deletion models/engine/file_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Contains the FileStorage class
"""

import models
import json
from models.amenity import Amenity
from models.base_model import BaseModel
Expand Down Expand Up @@ -55,7 +56,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 All @@ -68,3 +69,29 @@ def delete(self, obj=None):
def close(self):
"""call reload() method for deserializing the JSON file to objects"""
self.reload()

def get(self, cls, id):
"""Get method."""

if cls not in classes.values():
return None

all_cls = models.storage.all(cls)
for value in all_cls.values():
if (value.id) == id:
return value

return None

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

if not cls:
count = 0
for clas in all_classes:
count += len(models.storage.all(clas).values())
else:
count += len(models.storage.all(cls).values())

return count
Binary file added tests/__pycache__/test_console.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.