I am working on some bootcamp assignment and I have reached a point that I feel stuck.
So basically I have built a command line interface for an airbnb clone. and the question i am stuck on is that, I am required to update my cmd interpreter to accept this command.
<class_name>.all()
That retrieves all instances of a class.
In the same project I have built another method that does this but accepts this as the argument.
all <class_name>
This is my code for that
def do_all(self, arg):
"""Print all string representation of all instances."""
args = shlex.split(arg)
if len(args) > 0 and args[0] not in classes:
print("** class doesn't exist **")
else:
obj_dict = []
for obj in models.storage.all().values():
# Checks if class name exists
if len(args) > 0 and args[0] == obj.__class__.__name__:
# Append string representation of object
obj_dict.append(obj.__str__())
elif len(args) == 0: # For all objects
obj_dict.append(obj.__str__())
# print(obj_dict) -- Rollback to this if not working
# Added this Feature to print each object in a new line
for i in range(len(obj_dict)):
print(obj_dict[i])
if i == len(obj_dict) - 1:
break
Let me know if You want to see the whole class.
Any help will be appreciated