def do_show(self, args): """Prints the class name and id.""" args = shlex.split(args) if len(args) == 0: print("** class name missing **") return elif args[0] not in model_names: print("** class doesn't exist **") return elif len(args) != 2: print("** instance id missing **") return cls, idx = args[0], args[1] # try: # eval(args[0]) # except NameError: # print("** class doesn't exist **") # return storage = FileStorage() storage.reload() obj_dict = storage.all() key = cls + '.' + idx try: obj = obj_dict[key] print(obj) except KeyError: print("** no instance found **")
def do_update(self, args): """ Usage: update ClassName ID AttributeName AttributeValue """ arg_list = list(args.split()) if len(arg_list) < 1: print('** class name missing **') elif len(arg_list) < 2: print('** instance id missing **') elif len(arg_list) < 3: print('** attribute name missing **') elif len(arg_list) < 4: print('** value missing **') elif arg_list[0] not in classes: print('** class doesn\'t exist **') elif storage.all(): found = None for key in storage.all().keys(): if key == arg_list[1]: my_model = storage.all()[key] setattr(my_model, arg_list[2], arg_list[3]) my_model.save() storage.reload() found = 1 if not found: print('** no instance found **') else: print('** no instance found **')
def do_destroy(self, args): """Deletes the instance indicated and removes it from the JSON file.""" args = shlex.split(args) if len(args) == 0: print("** class name missing **") return elif args[0] not in model_names: print("** class doesn't exist **") return elif len(args) != 2: print("** instance id missing **") return cls, idx = args[0], args[1] storage = FileStorage() storage.reload() obj_dict = storage.all() key = cls + '.' + idx try: del obj_dict[key] except KeyError: print("** no instance found **") storage.save()
def do_show(self, arg): """ Show command to Prints the string representation of an instance based on the class name and id Usage: show <Class_Name> <obj_id> """ args = arg.split() if len(args) == 0: print("** class name missing **") return try: eval(args[0]) except Exception: print("** class doesn't exist **") return if len(args) == 1: print("** instance id missing **") else: storage.reload() container_obj = storage.all() key_id = args[0] + "." + args[1] if key_id in container_obj: value = container_obj[key_id] print(value) else: print("** no instance found **")
def do_destroy(self, arg): """ Destroy command to Deletes an instance based on the class name and id Usage: destroy <Class_Name> <obj_id> """ args = arg.split() container_obj = [] if len(args) == 0: print("** class name missing **") return try: eval(args[0]) except Exception: print("** class doesn't exist **") return if len(args) == 1: print("** instance id missing **") else: storage.reload() container_obj = storage.all() key_id = args[0] + "." + args[1] if key_id in container_obj: del container_obj[key_id] storage.save() else: print("** no instance found **")
def do_show(self, line): """Prints string representation of an instance""" args = split(line) if len(args) == 0: print("** class name missing **") return if len(args) == 1: print("** instance id missing **") return storage = FileStorage() storage.reload() objDict = storage.all() try: eval(args[0]) except NameError: print("** class doesn't exist **") return key = args[0] + "." + args[1] try: value = objDict[key] print(value) except KeyError: print("** no instance found **")
def do_update(self, usr_in): _input = usr_in.split() switch = 0 switch1 = 0 objects = storage.all() if _input[0] not in class_check: print("** class doesn't exist **") elif len(_input) < 2: num_list = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] for letter in _input[0]: if letter in num_list: switch1 = 1 if switch1 == 1: print("** class name missing **") else: print("** instance id missing **") elif len(_input) < 3: print("** attribute name missing **") elif len(_input) < 4: print("** value missing **") elif len(_input) == 4: for key in objects.keys(): if _input[1] == key: objects[key].__dict__[_input[2]] = _input[3] else: objects[key].__dict__ = ({_input[2]: _input[3]}) storage.save() storage.reload()
def do_update(self, arg): """ Updates instance based on cls name/id by attribute, saves to JSON Command syntax: update + [cls nme] + [id] + [attr nme] + [attr val] """ if len(arg) == 0: print("** class name missing **") return if len(arg) == 1: print("** instance id missing **") return if len(arg) == 2: print("** attribute name missing **") return if len(arg) == 3: print("** value missing **") return arg = arg.split() inst_key = arg[0] + "." + arg[1] storage = FileStorage() storage.reload() all_objs = storage.all() try: obj_value = all_objs[inst_key] except KeyError: print("** no instance found**") return setattr(obj_value, arg[2], arg[3]) obj_value.save()
def test_save(self): self.cosito.save() storage.reload() storage.all() self.assertTrue(storage.all(), "Holberton") self.assertIsInstance(self.cosito.updated_at, datetime) self.assertNotEqual(self.cosito.created_at, self.cosito.updated_at)
def do_show(self, args): """ prints string reps of instance based on class name and ID """ args = shlex.split(args) if len(args) == 0: print("** class name missing **") return if len(args) == 1: print("** instance id missing **") return storage = FileStorage() storage.reload() object_dict = storage.all() try: eval(args[0]) except NameError: print("** class doesn't exist **") return keys = args[0] + "." + args[1] keys = args[0] + "." + args[1] try: Value = object_dict[keys] print(Value) except KeyError: print("** no instance found **")
def do_destroy(self, line): """Destroys an instance based on class name and ID""" args = split(line) if len(args) == 0: print("** class name missing **") return elif len(args) == 1: print("** instance id missing **") return clsName = args[0] clsId = args[1] storage = FileStorage() storage.reload() objDict = storage.all() try: eval(clsName) except NameError: print("** class doesn't exist **") return key = clsName + "." + clsId try: del objDict[key] except KeyError: print("** no instance found **") storage.save()
def do_destroy(self, args): """ deletes instance base on class name and id """ args = shlex.split(args) if len(args) == 0: print("** class name missing **") return elif len(args) == 1: print("** instance id missing **") return classN = args[0] classI = args[1] storage = FileStorage() storage.reload() object_dict = storage.all() try: eval(classN) except NameError: print("** class doesn't exist **") return keys = classN + "." + classI try: del object_dict[keys] except KeyError: print("** no instance found **") storage.save()
def test_models_save(self): """ Check if the save function works """ self.b1.name = "Hello" self.storage.save() storage.reload() storage.all() self.assertTrue(storage.all(), "Hello") self.assertTrue(hasattr(self.b1, 'save')) self.assertNotEqual(self.b1.created_at, self.b1.updated_at)
def test_models_save(self): """Method for check if the save function works""" self.my_model.name = "Halo" self.my_model.save() storage.reload() storage.all() self.assertTrue(storage.all(), "Halo") self.assertTrue(hasattr(self.my_model, 'save')) self.assertNotEqual(self.my_model.created_at, self.my_model.updated_at)
def test_5_store_object(self): """tests objetct """ test_1 = BaseModel() test_1.name = "Holberton" test_1.my_number = 89 test_1.save() storage.reload() dic_obj = storage.all() self.assertTrue(dict, dic_obj)
def do_create(self, args): """ creates a new instance of BaseModel """ if not args: print("** class name missing **") elif args in classes.keys(): new = classes[args]() new.save() storage.reload() print(new.id) else: print("** class doesn't exist **")
def test_reload(self): """Test for reload method """ for key, value in TestFileStorage.models.items(): instance = value() tmp_id = instance.id storage.new(instance) storage.reload() key_obj = "{}.{}".format(key, tmp_id) tmp_dic = storage.all() self.assertTrue(key_obj in tmp_dic)
def do_create(self, user_input): """Creates a new instance and returns the unique id""" if not user_input: print("** class name missing **") elif user_input in class_check: _input = user_input.split() new_obj = class_check[_input[0]]() new_obj.save() storage.reload() print(new_obj.id) else: print("** class doesn't exist **")
def do_all(self, arg): """ Prints string representation of all instances based on cls name Command syntax: all + [ENTER] """ new_list = [] new_list2 = [] storage = FileStorage() storage.reload() all_objs = storage.all() for key, value in all_objs.items(): new_list.append(value.__str__()) for i in new_list: new_list2.append(str(i)) print(new_list2)
def do_update(self, line): """Updates an instance based on class name and ID""" storage = FileStorage() storage.reload() args = split(line) if len(args) == 0: print("** class name missing **") return elif len(args) == 1: print("** instance id missing **") return elif len(args) == 2: print("** attribute name missing **") return elif len(args) == 3: print("** value missing **") return try: eval(args[0]) except NameError: print("** class doesn't exist **") return key = args[0] + "." + args[1] objDict = storage.all() try: objVal = objDict[key] except KeyError: print("** no instance found **") return try: attrType = type(getattr(objVal, args[2])) args[3] = attrType(args[3]) except AttributeError: pass setattr(objVal, args[2], args[3]) objVal.save()
def do_all(self, args): """ Prints all string reps of all instances """ object_list = [] storage = FileStorage() storage.reload() objects = storage.all() try: if len(args) != 0: eval(args) except NameError: print("** class doesn't exist **") return for keys, value in objects.items(): if len(args) != 0: if type(value) is eval(args): object_list.append(value.__str__()) else: object_list.append(value.__str__()) print(object_list)
def do_create(self, arg): """ do_create - Creates a new instance of BaseModel, saves it (To the JSON file) and prints the id Ex. create BaseModel """ if len(arg) is 0: print("** class name missing **") return if arg == "BaseModel": new_bmodel = BaseModel() new_bmodel.save() storage.reload() print(new_bmodel.id) else: print("** class doesn't exist **")
def do_update(self, args): """Updates an instance based on the class name and ID by either adding or updating given attribute. Also saves changes to JSON file.""" args = shlex.split(args) storage = FileStorage() storage.reload() if len(args) == 0: print("** class name missing **") return elif args[0] not in model_names: print("** class doesn't exist **") return elif len(args) == 1: print("** instance id missing **") return cls, idx, att = args[0], args[1], args[2] instance = cls + '.' + idx if instance not in obj_dict.keys(): print("** no instance found **") return elif len(args) == 2: print("** attribute name missing **") return elif len(args) == 3: print("** value missing **") return obj_dict = storage.all() try: attr_type = type(getattr(object_value, att)) atty = attr_type(args[3]) except AttributeError: pass obj_v = obj_dict[instance] setattr(obj_v, att, atty) obj_v.save()
def do_count(self, args): """ counts numb of instances """ object_list = [] storage = FileStorage() storage.reload() objects = storage.all() try: if len(args) != 0: eval(args) except NameError: print("** class doesn't exist **") return for keys, value in objects.items(): if len(args) != 0: if type(value) is eval(args): object_list.append(value) else: object_list.append(value) print(len(object_list))
def test_reload(self): """Test reload method. """ for k, v in TestFileStorage.models.items(): new_obj = v() no_id = new_obj.id storage.new(new_obj) storage.reload() inin = "{}.{}".format(k, no_id) self.assertTrue(inin in FileStorage._FileStorage__objects) self.assertIsInstance(storage, FileStorage) with self.assertRaises(TypeError) as err: storage.reload("devil's", "trill") msg = "reload() takes 1 positional argument but 3 were given" self.assertEqual(str(err.exception), msg) with self.assertRaises(TypeError) as err: FileStorage.reload() msg = "reload() missing 1 required positional argument: 'self'" self.assertEqual(str(err.exception), msg)
def do_all(self, args): """Print representation of all instances, or if given an argument, all of that class type.""" obj_list = [] storage = FileStorage() storage.reload() objs = storage.all() if len(args) != 0: if args not in model_names: print("** class doesn't exist **") return for k, v in objs.items(): if len(args) != 0: ka = k.split(".") if ka[0] in model_names: obj_list.append(v.__str__()) else: obj_list.append(v.__str__()) print(obj_list)
def do_all(self, args): """ Shows all objects, or all objects of a class""" obj_list = [] storage = models.storage storage.reload() try: if len(args) != 0: eval(args) if len(args) == 0: objects = storage.all() else: objects = storage.all(args) except NameError: print("** class doesn't exist **") return for key, val in objects.items(): if len(args) != 0: if type(val) is eval(args): obj_list.append(val) else: obj_list.append(val) print(obj_list)
def do_show(self, arg): """ Prints string representation of instance based on cls name/id Command syntax: show + [cls name] + [id] """ if len(arg) == 0: print("** class name missing **") return if len(arg) == 1: print("** instance id missing **") return arg = arg.split() inst_key = arg[0] + "." + arg[1] storage = FileStorage() storage.reload() all_objs = storage.all() try: str_rep = all_objs[inst_key] print(str_rep) except KeyError: print("** no instance found**")
def do_destroy(self, arg): """ Deletes instance based on cls name & instance id, saves change Command syntax: destroy + [cls name] + [id] """ if len(arg) == 0: print("** class name missing **") return if len(arg) == 1: print("** instance id missing **") return arg = arg.split() inst_key = arg[0] + "." + arg[1] storage = FileStorage() storage.reload() all_objs = storage.all() try: del all_objs[inst_key] except KeyError: print("** no instance found**") storage.save()
def do_all(self, line): """Prints all data instances, filtered by class (optional)""" objList = [] storage = FileStorage() storage.reload() objs = storage.all() try: if len(line) != 0: eval(line) except NameError: print("** class doesn't exist **") return for _, value in objs.items(): if len(line) != 0: if type(value) is eval(line): objList.append(value) else: objList.append(value) for i in objList: print(i)