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

Add task 2 #3700

Open
wants to merge 1 commit 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: 2 additions & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tariq79253@gmail.com>
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# 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...)
* Update attributes of an object
* Destroy an object

## Table of Content

* [Environment](#environment)
* [Installation](#installation)
* [File Descriptions](#file-descriptions)
Expand All @@ -28,16 +31,16 @@ This project is interpreted/tested on Ubuntu 14.04 LTS using python3 (version 3.
* Run hbnb(non-interactively): `echo "<command>" | ./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
* `<emptyline>` - 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
Expand Down Expand Up @@ -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.
Binary file added __pycache__/console.cpython-38.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions file.json
Original file line number Diff line number Diff line change
@@ -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"}}
Binary file added models/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file added models/__pycache__/amenity.cpython-38.pyc
Binary file not shown.
Binary file added models/__pycache__/base_model.cpython-38.pyc
Binary file not shown.
Binary file added models/__pycache__/city.cpython-38.pyc
Binary file not shown.
Binary file added models/__pycache__/place.cpython-38.pyc
Binary file not shown.
Binary file added models/__pycache__/review.cpython-38.pyc
Binary file not shown.
Binary file added models/__pycache__/state.cpython-38.pyc
Binary file not shown.
Binary file added models/__pycache__/user.cpython-38.pyc
Binary file not shown.
Binary file added models/engine/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
40 changes: 40 additions & 0 deletions models/engine/db_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
42 changes: 41 additions & 1 deletion models/engine/file_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def reload(self):
pass

def delete(self, obj=None):
"""delete obj from __objects if its 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:
Expand All @@ -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)
11 changes: 11 additions & 0 deletions test_get_count.py
Original file line number Diff line number Diff line change
@@ -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)))
Binary file added tests/__pycache__/test_console.cpython-38.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.