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 #3673

Open
wants to merge 2 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Abnb_env
Abnb_API
__pycache__
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ EOF all create destroy help quit show update
No known bugs at this time.

## Authors
Charis Adu - [Github](https://github.com/Charis04) / [Twitter](https://twitter.com/Charis_sensei)
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)

Expand Down
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 ValueError:
try:
value = float(value)
except:
except ValueError:
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 ValueError:
args[3] = 0
elif args[2] in floats:
try:
args[3] = float(args[3])
except:
except ValueError:
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()
1 change: 1 addition & 0 deletions file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Amenity.6183b006-7ac1-4890-8f0c-e76ad146632d": {"id": "6183b006-7ac1-4890-8f0c-e76ad146632d", "created_at": "2024-08-06T16:37:20.861420", "updated_at": "2024-08-06T16:37:20.861420", "__class__": "Amenity"}, "BaseModel.93237c7f-5a86-4bc9-804c-0eb103924002": {"id": "93237c7f-5a86-4bc9-804c-0eb103924002", "created_at": "2024-08-06T16:37:20.861438", "updated_at": "2024-08-06T16:37:20.861438", "__class__": "BaseModel"}, "City.2302b3af-1977-49ea-9639-978b8f84de9c": {"id": "2302b3af-1977-49ea-9639-978b8f84de9c", "created_at": "2024-08-06T16:37:20.861455", "updated_at": "2024-08-06T16:37:20.861455", "__class__": "City"}, "Place.0cdbbaac-a325-481d-b0d6-83d1ff00fb85": {"id": "0cdbbaac-a325-481d-b0d6-83d1ff00fb85", "created_at": "2024-08-06T16:37:20.861472", "updated_at": "2024-08-06T16:37:20.861472", "__class__": "Place"}, "Review.dad2d123-b8ef-48f7-956d-254c9c447385": {"id": "dad2d123-b8ef-48f7-956d-254c9c447385", "created_at": "2024-08-06T16:37:20.861521", "updated_at": "2024-08-06T16:37:20.861521", "__class__": "Review"}, "State.396114ed-e972-4c6b-a669-4526ecb60716": {"id": "396114ed-e972-4c6b-a669-4526ecb60716", "created_at": "2024-08-06T16:37:20.861542", "updated_at": "2024-08-06T16:37:20.861542", "__class__": "State"}, "User.83081efd-a9b1-415d-9f4b-e43b16c4f02a": {"id": "83081efd-a9b1-415d-9f4b-e43b16c4f02a", "created_at": "2024-08-06T16:37:20.861556", "updated_at": "2024-08-06T16:37:20.861556", "__class__": "User"}}
22 changes: 22 additions & 0 deletions models/engine/db_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,28 @@ def save(self):
"""commit all changes of the current database session"""
self.__session.commit()

def get(self, cls, id):
"""
A method to retrieve one object. Returns the object based on the class
and its ID, or None if not found
"""
objects = self.__session.query(cls).all()
for obj in objects:
if obj.id == id:
return obj
return None

def count(self, cls=None):
"""
Counts and returns the number of objects in a storage matching the given class.
If no class is passed, returns the count of all objects in storage.
"""
if cls == None:
return len(self.__session.query().all())
objects = self.__session.query(cls).all()
return len(objects)


def delete(self, obj=None):
"""delete from the current database session obj if not None"""
if obj is not None:
Expand Down
24 changes: 24 additions & 0 deletions models/engine/file_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ def save(self):
with open(self.__file_path, 'w') as f:
json.dump(json_objects, f)

def get(self, cls, id):
"""
A method to retrieve one object. Returns the object based on the class
and its ID, or None if not found
"""
key = cls.__name__ + "." + id
for keys in self.__objects:
if keys == key:
return self.__objects[keys]
return None

def count(self, cls=None):
"""
Counts and returns the number of objects in a storage matching the given class.
If no class is passed, returns the count of all objects in storage.
"""
if cls is None:
return len(self.__objects)
count = 0
for obj in self.__objects.values():
if obj['__class__'] == cls.__name__:
count += 1
return count

def reload(self):
"""deserializes the JSON file to __objects"""
try:
Expand Down