diff --git a/AUTHORS b/AUTHORS index 64b26acdc14..40cdf59a761 100644 --- a/AUTHORS +++ b/AUTHORS @@ -3,4 +3,5 @@ Jennifer Huang <133@holbertonschool.com> Alexa Orrico <210@holbertonschool.com> -Joann Vuong <130@holbertonschool.com> +Joann Vuong <130@holbertonschool.com> +Tariq Omer diff --git a/README.md b/README.md index f1d72de6355..8983dfc7925 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ # AirBnB Clone - The Console + The console is the first segment of the AirBnB project at Holberton School that will collectively cover fundamental concepts of higher level programming. The goal of AirBnB project is to eventually deploy our server a simple copy of the AirBnB Website(HBnB). A command interpreter is created in this segment to manage objects for the AirBnB(HBnB) website. -#### Functionalities of this command interpreter: +## Functionalities of this command interpreter + * Create a new object (ex: a new User or a new Place) * Retrieve an object from a file, a database etc... * Do operations on objects (count, compute stats, etc...) @@ -9,6 +11,7 @@ The console is the first segment of the AirBnB project at Holberton School that * Destroy an object ## Table of Content + * [Environment](#environment) * [Installation](#installation) * [File Descriptions](#file-descriptions) @@ -28,16 +31,16 @@ This project is interpreted/tested on Ubuntu 14.04 LTS using python3 (version 3. * Run hbnb(non-interactively): `echo "" | ./console.py` ## File Descriptions -[console.py](console.py) - the console contains the entry point of the command interpreter. +[console.py](console.py) - the console contains the entry point of the command interpreter. List of commands this console current supports: -* `EOF` - exits console +* `EOF` - exits console * `quit` - exits console * `` - overwrites default emptyline method and does nothing * `create` - Creates a new instance of`BaseModel`, saves it (to the JSON file) and prints the id -* `destroy` - Deletes an instance based on the class name and id (save the change into the JSON file). +* `destroy` - Deletes an instance based on the class name and id (save the change into the JSON file). * `show` - Prints the string representation of an instance based on the class name and id. -* `all` - Prints all string representation of all instances based or not on the class name. -* `update` - Updates an instance based on the class name and id by adding or updating attribute (save the change into the JSON file). +* `all` - Prints all string representation of all instances based or not on the class name. +* `update` - Updates an instance based on the class name and id by adding or updating attribute (save the change into the JSON file). #### `models/` directory contains classes used for this project: [base_model.py](/models/base_model.py) - The BaseModel class from which future classes will be derived @@ -151,12 +154,13 @@ EOF all create destroy help quit show update ``` ## Bugs -No known bugs at this time. +No known bugs at this time. ## Authors -Alexa Orrico - [Github](https://github.com/alexaorrico) / [Twitter](https://twitter.com/alexa_orrico) +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) +Tariq Omer - [Github](https://github.com/tariq5mo) / [Twitter](https://twitter.com/tariq79252) Second part of Airbnb: Joann Vuong ## License -Public Domain. No copy write protection. +Public Domain. No copy write protection. diff --git a/__pycache__/console.cpython-38.pyc b/__pycache__/console.cpython-38.pyc new file mode 100644 index 00000000000..d1e9a92edbe Binary files /dev/null and b/__pycache__/console.cpython-38.pyc differ diff --git a/file.json b/file.json new file mode 100644 index 00000000000..87f95aaf7cc --- /dev/null +++ b/file.json @@ -0,0 +1 @@ +{"Amenity.1338e3bf-82ee-407e-aeec-9a2d451d3f89": {"id": "1338e3bf-82ee-407e-aeec-9a2d451d3f89", "created_at": "2024-08-21T19:05:46.076190", "updated_at": "2024-08-21T19:05:46.076190", "__class__": "Amenity"}, "BaseModel.819d7082-6c4d-4f61-94d5-7fe45d7c2e8f": {"id": "819d7082-6c4d-4f61-94d5-7fe45d7c2e8f", "created_at": "2024-08-21T19:05:46.076201", "updated_at": "2024-08-21T19:05:46.076201", "__class__": "BaseModel"}, "City.b94f3f91-2c5c-41bc-94f0-d10849d30e64": {"id": "b94f3f91-2c5c-41bc-94f0-d10849d30e64", "created_at": "2024-08-21T19:05:46.076209", "updated_at": "2024-08-21T19:05:46.076209", "__class__": "City"}, "Place.ee575b3d-d466-4c2d-803b-559c5051aa92": {"id": "ee575b3d-d466-4c2d-803b-559c5051aa92", "created_at": "2024-08-21T19:05:46.076216", "updated_at": "2024-08-21T19:05:46.076216", "__class__": "Place"}, "Review.0b6eb505-2c3e-40de-bcfa-8efd8273bc8a": {"id": "0b6eb505-2c3e-40de-bcfa-8efd8273bc8a", "created_at": "2024-08-21T19:05:46.076224", "updated_at": "2024-08-21T19:05:46.076224", "__class__": "Review"}, "State.33fcf67a-6633-40db-aff1-317a1115faff": {"id": "33fcf67a-6633-40db-aff1-317a1115faff", "created_at": "2024-08-21T19:05:46.076232", "updated_at": "2024-08-21T19:05:46.076232", "__class__": "State"}, "User.bce78d24-29eb-4c49-8a0f-3e98fb6f28a8": {"id": "bce78d24-29eb-4c49-8a0f-3e98fb6f28a8", "created_at": "2024-08-21T19:05:46.076239", "updated_at": "2024-08-21T19:05:46.076239", "__class__": "User"}} \ No newline at end of file diff --git a/models/__pycache__/__init__.cpython-38.pyc b/models/__pycache__/__init__.cpython-38.pyc new file mode 100644 index 00000000000..dce734d943c Binary files /dev/null and b/models/__pycache__/__init__.cpython-38.pyc differ diff --git a/models/__pycache__/amenity.cpython-38.pyc b/models/__pycache__/amenity.cpython-38.pyc new file mode 100644 index 00000000000..b27f999d794 Binary files /dev/null and b/models/__pycache__/amenity.cpython-38.pyc differ diff --git a/models/__pycache__/base_model.cpython-38.pyc b/models/__pycache__/base_model.cpython-38.pyc new file mode 100644 index 00000000000..d3bb1a03249 Binary files /dev/null and b/models/__pycache__/base_model.cpython-38.pyc differ diff --git a/models/__pycache__/city.cpython-38.pyc b/models/__pycache__/city.cpython-38.pyc new file mode 100644 index 00000000000..da7ac42c67c Binary files /dev/null and b/models/__pycache__/city.cpython-38.pyc differ diff --git a/models/__pycache__/place.cpython-38.pyc b/models/__pycache__/place.cpython-38.pyc new file mode 100644 index 00000000000..e564cb6ecd0 Binary files /dev/null and b/models/__pycache__/place.cpython-38.pyc differ diff --git a/models/__pycache__/review.cpython-38.pyc b/models/__pycache__/review.cpython-38.pyc new file mode 100644 index 00000000000..c9c9f064776 Binary files /dev/null and b/models/__pycache__/review.cpython-38.pyc differ diff --git a/models/__pycache__/state.cpython-38.pyc b/models/__pycache__/state.cpython-38.pyc new file mode 100644 index 00000000000..4be5fe206fa Binary files /dev/null and b/models/__pycache__/state.cpython-38.pyc differ diff --git a/models/__pycache__/user.cpython-38.pyc b/models/__pycache__/user.cpython-38.pyc new file mode 100644 index 00000000000..f1c1acbd7e0 Binary files /dev/null and b/models/__pycache__/user.cpython-38.pyc differ diff --git a/models/engine/__pycache__/__init__.cpython-38.pyc b/models/engine/__pycache__/__init__.cpython-38.pyc new file mode 100644 index 00000000000..db18f893947 Binary files /dev/null and b/models/engine/__pycache__/__init__.cpython-38.pyc differ diff --git a/models/engine/__pycache__/db_storage.cpython-38.pyc b/models/engine/__pycache__/db_storage.cpython-38.pyc new file mode 100644 index 00000000000..caf230876ee Binary files /dev/null and b/models/engine/__pycache__/db_storage.cpython-38.pyc differ diff --git a/models/engine/__pycache__/file_storage.cpython-38.pyc b/models/engine/__pycache__/file_storage.cpython-38.pyc new file mode 100644 index 00000000000..9b9a64d7ddb Binary files /dev/null and b/models/engine/__pycache__/file_storage.cpython-38.pyc differ diff --git a/models/engine/db_storage.py b/models/engine/db_storage.py index b8e7d291e6f..6d2d99d946e 100755 --- a/models/engine/db_storage.py +++ b/models/engine/db_storage.py @@ -74,3 +74,43 @@ def reload(self): def close(self): """call remove() method on the private session attribute""" self.__session.remove() + + def get(self, cls, id): + """A method to retrieve one object. + + Args: + id (str): string representing the object ID + """ + if cls is None or id is None or not isinstance(id, str): + return None + if isinstance(cls, str): # To convert cls to class object if string. + try: + cls = globals()[cls] + except Exception: + pass + + self.reload() + all_cls_objs = self.all(cls) + if all_cls_objs == {}: + return None + + for obj in all_cls_objs: + class_name = obj.split(".")[0] + try: + class_name = globals()[class_name] + except Exception: + pass + if cls == class_name and id == obj.split(".")[1]: + return all_cls_objs[obj] + + return None + + def count(self, cls=None): + """A method to count the number of objects in storage. + + Args: + cls (class, optional): The class object. Defaults to None. + """ + self.reload() + all_cls_objs = self.all(cls) + return len(all_cls_objs) diff --git a/models/engine/file_storage.py b/models/engine/file_storage.py index c8cb8c1764d..080b66450f4 100755 --- a/models/engine/file_storage.py +++ b/models/engine/file_storage.py @@ -59,7 +59,7 @@ def reload(self): pass def delete(self, obj=None): - """delete obj from __objects if it’s inside""" + """delete obj from __objects if it's inside""" if obj is not None: key = obj.__class__.__name__ + '.' + obj.id if key in self.__objects: @@ -68,3 +68,43 @@ 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): + """A method to retrieve one object. + + Args: + id (str): string representing the object ID + """ + if cls is None or id is None or not isinstance(id, str): + return None + if isinstance(cls, str): # To convert cls to class object if string. + try: + cls = globals()[cls] + except Exception: + pass + + self.reload() + all_cls_objs = self.all(cls) + if all_cls_objs == {}: + return None + + for obj in all_cls_objs: + class_name = obj.split(".")[0] + try: + class_name = globals()[class_name] + except Exception: + pass + if cls == class_name and id == obj.split(".")[1]: + return all_cls_objs[obj] + + return None + + def count(self, cls=None): + """A method to count the number of objects in storage. + + Args: + cls (class, optional): The class object. Defaults to None. + """ + self.reload() + all_cls_objs = self.all(cls) + return len(all_cls_objs) diff --git a/test_get_count.py b/test_get_count.py new file mode 100755 index 00000000000..18194dfce69 --- /dev/null +++ b/test_get_count.py @@ -0,0 +1,11 @@ +#!/usr/bin/python3 +""" Test .get() and .count() methods +""" +from models import storage +from models.state import State + +print("All objects: {}".format(storage.count())) +print("State objects: {}".format(storage.count(State))) + +first_state_id = list(storage.all(State).values())[0].id +print("First state: {}".format(storage.get(State, first_state_id))) diff --git a/tests/__pycache__/test_console.cpython-38.pyc b/tests/__pycache__/test_console.cpython-38.pyc new file mode 100644 index 00000000000..596a49a7103 Binary files /dev/null and b/tests/__pycache__/test_console.cpython-38.pyc differ diff --git a/tests/test_models/__pycache__/__init__.cpython-38.pyc b/tests/test_models/__pycache__/__init__.cpython-38.pyc new file mode 100644 index 00000000000..593393c5a4e Binary files /dev/null and b/tests/test_models/__pycache__/__init__.cpython-38.pyc differ diff --git a/tests/test_models/__pycache__/test_amenity.cpython-38.pyc b/tests/test_models/__pycache__/test_amenity.cpython-38.pyc new file mode 100644 index 00000000000..b2f2b6d7c97 Binary files /dev/null and b/tests/test_models/__pycache__/test_amenity.cpython-38.pyc differ diff --git a/tests/test_models/__pycache__/test_base_model.cpython-38.pyc b/tests/test_models/__pycache__/test_base_model.cpython-38.pyc new file mode 100644 index 00000000000..d56242835b0 Binary files /dev/null and b/tests/test_models/__pycache__/test_base_model.cpython-38.pyc differ diff --git a/tests/test_models/__pycache__/test_city.cpython-38.pyc b/tests/test_models/__pycache__/test_city.cpython-38.pyc new file mode 100644 index 00000000000..b9e0e118c06 Binary files /dev/null and b/tests/test_models/__pycache__/test_city.cpython-38.pyc differ diff --git a/tests/test_models/__pycache__/test_place.cpython-38.pyc b/tests/test_models/__pycache__/test_place.cpython-38.pyc new file mode 100644 index 00000000000..4d0d53f353d Binary files /dev/null and b/tests/test_models/__pycache__/test_place.cpython-38.pyc differ diff --git a/tests/test_models/__pycache__/test_review.cpython-38.pyc b/tests/test_models/__pycache__/test_review.cpython-38.pyc new file mode 100644 index 00000000000..7f1d5d023bd Binary files /dev/null and b/tests/test_models/__pycache__/test_review.cpython-38.pyc differ diff --git a/tests/test_models/__pycache__/test_state.cpython-38.pyc b/tests/test_models/__pycache__/test_state.cpython-38.pyc new file mode 100644 index 00000000000..4658c7b1f9d Binary files /dev/null and b/tests/test_models/__pycache__/test_state.cpython-38.pyc differ diff --git a/tests/test_models/__pycache__/test_user.cpython-38.pyc b/tests/test_models/__pycache__/test_user.cpython-38.pyc new file mode 100644 index 00000000000..b3f005307eb Binary files /dev/null and b/tests/test_models/__pycache__/test_user.cpython-38.pyc differ diff --git a/tests/test_models/test_engine/__pycache__/__init__.cpython-38.pyc b/tests/test_models/test_engine/__pycache__/__init__.cpython-38.pyc new file mode 100644 index 00000000000..16e56835fb0 Binary files /dev/null and b/tests/test_models/test_engine/__pycache__/__init__.cpython-38.pyc differ diff --git a/tests/test_models/test_engine/__pycache__/test_db_storage.cpython-38.pyc b/tests/test_models/test_engine/__pycache__/test_db_storage.cpython-38.pyc new file mode 100644 index 00000000000..a989397cf2c Binary files /dev/null and b/tests/test_models/test_engine/__pycache__/test_db_storage.cpython-38.pyc differ diff --git a/tests/test_models/test_engine/__pycache__/test_file_storage.cpython-38.pyc b/tests/test_models/test_engine/__pycache__/test_file_storage.cpython-38.pyc new file mode 100644 index 00000000000..07d67dfd37b Binary files /dev/null and b/tests/test_models/test_engine/__pycache__/test_file_storage.cpython-38.pyc differ