diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..f4b12592fd9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +Abnb_env +Abnb_API +__pycache__ diff --git a/README.md b/README.md index f1d72de6355..834ae66da36 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/console.py b/console.py index 4798f9ac76b..7fa6694d5ae 100755 --- a/console.py +++ b/console.py @@ -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 @@ -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() @@ -160,5 +160,6 @@ def do_update(self, arg): else: print("** class doesn't exist **") + if __name__ == '__main__': HBNBCommand().cmdloop() diff --git a/file.json b/file.json new file mode 100644 index 00000000000..22d9b22f400 --- /dev/null +++ b/file.json @@ -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"}} \ No newline at end of file diff --git a/models/engine/db_storage.py b/models/engine/db_storage.py index b8e7d291e6f..4ead3a169cf 100755 --- a/models/engine/db_storage.py +++ b/models/engine/db_storage.py @@ -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: diff --git a/models/engine/file_storage.py b/models/engine/file_storage.py index c8cb8c1764d..84c57d18f5e 100755 --- a/models/engine/file_storage.py +++ b/models/engine/file_storage.py @@ -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: