def do_create(self, args): """ Create an object of any class""" arg = args.split() if not arg[0]: print("** class name missing **") return elif arg[0] not in HBNBCommand.classes: print("** class doesn't exist **") return new_instance = HBNBCommand.classes[arg[0]]() arg = arg[1:] i = 0 while i < len(arg): key = arg[i].split("=")[0] value = arg[i].split("=")[1].replace("_", " ") if '"' in value: value = value.strip('"') else: if "." in value: value = float(value) else: value = int(value) setattr(new_instance, key, value) i += 1 storage.save() print(new_instance.id) new_instance.save()
def do_create(self, ln): """ Create an object of any class""" if not ln: print("** class name missing **") return args = ln.split(" ") if args[0] not in HBNBCommand.classes: print("** class doesn't exist **") return new_instance = HBNBCommand.classes[args[0]]() for nParam in range(1, len(args)): key = args[nParam].split('=')[0] value = args[nParam].split('=')[1].replace('_', ' ') if value[0] == '"': value = value.strip('"') else: value = eval(value) setattr(new_instance, key, value) storage.save() print(new_instance.id) new_instance.save()
def do_create(self, arg): """ Create an object of any class""" if not arg: print("** class name missing **") return args = arg.split() if args[0] not in HBNBCommand.classes: print("** class doesn't exist **") return new_instance = HBNBCommand.classes[args[0]]() for x in args: if "=" not in x: continue temp = x.split("=") key = temp[0] raw = temp[1] value = None if "\"" in raw: value = raw[1:-1] value = value.replace("_", " ") elif "." in raw: value = float(raw) else: value = int(raw) setattr(new_instance, key, value) storage.new(new_instance) print(new_instance.id) storage.save()
def do_destroy(self, line): """Deletes an instance based on the class name and id Exceptions: SyntaxError: when there is no args given NameError: when there is no object taht has the name IndexError: when there is no id given KeyError: when there is no valid id given """ try: if not line: raise SyntaxError() my_list = line.split(" ") if my_list[0] not in self.all_classes: raise NameError() if len(my_list) < 2: raise IndexError() objects = storage.all() key = my_list[0] + '.' + my_list[1] if key in objects: del objects[key] storage.save() else: raise KeyError() except SyntaxError: print("** class name missing **") except NameError: print("** class doesn't exist **") except IndexError: print("** instance id missing **") 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_create(self, args): """ Create an object of any class""" if not args: print("** class name missing **") return list_arguments = args.split() if list_arguments[0] not in HBNBCommand.classes: print("** class doesn't exist **") return arg_dict = {} if len(list_arguments) > 1: for index, argu in enumerate(list_arguments): if index > 0: x = re.search("^[a-zA-Z][\w]*=[\"]?(.*)[\"]?", argu) if not x: return else: key = argu.split("=")[0] value = argu.split("=")[1] if re.search("^[\"](.*[\"])?$", value): arg_dict[key] = value[1:-1].replace("_", " ") else: if "." in value: arg_dict[key] = float(value) else: try: arg_dict[key] = int(value) except ValueError: return new_instance = HBNBCommand.classes[list_arguments[0]]() new_instance.__dict__.update(arg_dict) storage.new(new_instance) storage.save() print(new_instance.id)
def do_create(self, args): """ Create an object of any class""" command = args.split() obj_dict = {} if not args: print("** class name missing **") return elif command[0] not in HBNBCommand.classes: print("** class doesn't exist **") return if len(command) == 1: if command[0] in HBNBCommand.classes: new_instance = HBNBCommand.classes[command[0]]() storage.new(new_instance) storage.save() print(new_instance.id) return else: print("** class doesn't exist **") return else: attributes = command.copy() del attributes[0] try: for att in attributes: p1, p2 = att.split('=') obj_dict[p1] = p2.replace("\"", "").replace("_", " ") new_instance = HBNBCommand.classes[command[0]](**obj_dict) storage.new(new_instance) print(new_instance.id) storage.save() except(AttributeError): pass
def do_create(self, args): """ Create an object of any class""" if not args: print("** class name missing **") return inpt = args.split() if inpt[0] not in HBNBCommand.classes: print("** class doesn't exist **") return else: clsarg = inpt[0] new_instance = HBNBCommand.classes[clsarg]() for i in inpt[1:]: # (starting with the argument after the classname) input_split_eq = i.split('=', 1) # split param by equals sign key = input_split_eq[0] # set key to left of equal sign value = input_split_eq[1] # set value to left of equal sign for i in range(len(value)): # loop through value if value[i] == '_': # if there is an underscore... value = value[:i] + " " + value[i + 1:] # ^ remove "_" and replace with " " if (key[0] == "'" and key[-1] == "'") or (key[0] == "\"" and key[-1] == "\""): key = key[1:-1] # remove the quotes before and after key if (value[0] == "'" and value[-1] == "'") or (value[0] == "\"" and value[-1] == "\""): value = value[1:-1] # remove the quotes before and after value setattr(new_instance, key, value) new_instance.save() print(new_instance.id) storage.save()
def do_update(self, arg): obj_dict = storage.all() args = arg.split(" ") if arg is '': print("** class name missing **") elif args[0] not in HBNBCommand.classes: print("** class doesn't exist **") elif len(args) < 2: print("** instance id missing **") else: for key, value in obj_dict.items(): skey = key.split(".") if skey[0] != args[0]: print("** no instance found **") else: if len(args) < 3: print("** attribute name missing **") elif len(args) < 4: print("** value missing **") else: for key, value in obj_dict.items(): skey = key.split(".") if skey[1] == args[1]: val = args[3] updater = {args[2]: val.replace('"', '')} (obj_dict[key].__dict__).update(updater) storage.save()
def do_update(self, args): 'updates an instance attribute based on class name and id' args = args.split() if not args: print("** class name missing **") return elif len(args) < 2: print("** instance id missing **") return elif len(args) < 3: print("** attribute name missing **") return elif len(args) < 4: print("** value missing **") return if args[0] not in classes: print("** class doesn't exist **") return for k, v in storage.all().items(): if args[1] == v.id: args[3] = args[3].strip('"') try: args[3] = int(args[3]) except: pass setattr(v, args[2], args[3]) storage.save() return print("** no instance found **")
def do_destroy(self, input_line): '''Deletes an instance based on the class name and id (save the change into the JSON file''' splited_input = input_line.split() splited_input_len = len(splited_input) if splited_input_len < 1: print("** class name missing **") return if splited_input[0] not in allowed_classes: print("** class doesn't exist **") return if splited_input_len < 2: print("** instance id missing **") return instances = storage.all() obj_reference = splited_input[0] + "." + splited_input[1] if obj_reference in instances.keys(): del instances[obj_reference] storage.save() else: print("** no instance found **")
def do_create(self, args): """ Create an object of any class""" cls_ls = ('BaseModel', 'User', 'Place', 'State', 'City', 'Amenity', 'Review') arg_list = args.split(" ") # need to get each arg alone if not args: print("** class name missing **") return elif arg_list[0] not in HBNBCommand.classes: # against allowed classes print("** class doesn't exist **") return new_instance = eval("{}()".format(arg_list[0])) # format new instance for param in arg_list[1:]: # after class rest is params <type>=<val> if "=" not in param: continue dic = param.split("=") # parse params key = dic[0] val = dic[1] key = key.strip('"') val = val.replace('_', ' ') # replace underscore with space setattr(new_instance, key, eval(val)) # give new instance # these parses params new_instance.save() # save to storage using basemodel method # print("{}".format(new_instance.id).strip('"')) # output # print("new={}".format(new_instance)) storage.save()
def do_create(self, args): """ Create an object of any class""" if not args: print("** class name missing **") return params = args.split(' ') if params[0] not in HBNBCommand.classes: print("** class doesn't exist **") return key_pairs = {} if len(params) > 1: cls_params = params[1:] for param in cls_params: key, value = param.split("=") """STRING regex for the value string""" str_regex = r'\"\w*\"' """FLOAT regex for the value string""" float_regex = r'^[0-9]*\.[0-9]$' """INTEGE regex for the value string""" integer_regex = r'^[0-9]*$' if re.match(str_regex, value): key_pairs[key] = re.sub(r'_', ' ', value)[1:-1] elif re.match(float_regex, value): key_pairs[key] = float(value) elif re.match(integer_regex, value): key_pairs[key] = int(value) else: pass key_pairs['__class__'] = params[0] new_instance = HBNBCommand.classes[params[0]](**key_pairs) storage.save() print(new_instance.id) storage.save()
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_create(self, args): """ Create an object of any class""" line = args.split(' ') if not args: print("** class name missing **") return elif (line[0] not in HBNBCommand.classes): print("** class doesn't exist **") return new_instance = HBNBCommand.classes[line[0]]() for i in range (1, len(line)): value_split = line[i].split('=') #print(value_split) # line128 if value_split[1][0] == '\"' and value_split[1][len(value_split[1]) - 1] == '\"': value_split[1] = shlex.split(value_split[1])[0].replace('_', ' ') att_string = str(value_split[1]) setattr(new_instance, value_split[0], att_string) elif '.' in value_split[1]: try: att_float = float(value_split[1]) except TypeError: continue setattr(new_instance, value_split[0], att_float) else: try: att_int = int(value_split[1]) except TypeError: continue setattr(new_instance, value_split[0], att_int) storage.save() print(new_instance.id) storage.save()
def do_update(self, args): """ Updates an instance based on the class name and id by adding or updating attribute (save the change into the JSON file)""" args = args.split() size = len(args) objs = storage.all() if not args: print("** class name missing **") elif not args[0] in classes.keys(): print("** class doesn't exist **") elif size < 2: print("** instance id missing **") elif not ".".join([args[0], args[1]]) in objs.keys(): print("** no instance found **") elif size < 3: print("** attribute name missing **") elif size < 4: print("** value missing **") else: try: obj = objs[".".join([args[0], args[1]])] setattr(obj, args[2], args[3]) storage.save() except Exception as e: print(e) print("** Update fail **")
def do_destroy(self, line): """Delete an instance based on the class name and id use - 'destroy [NAME_OBJECT] [ID]' """ # Splits line by the spaces argv = line.split() argc = len(argv) # if no arguments happen if argc == 0: print("** class name missing **") return if argv[0] not in base: print("** class doesn't exist **") return if argc == 1: print("** instance id missing **") return # Get all instances instances = storage.all() key_ref = argv[0] + "." + argv[1] if key_ref in instances.keys(): del instances[key_ref] storage.save() return # if it does not exist 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_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_create(self, args): """ Create an object of any class""" if not args: print("** class name missing **") return list_arg = args.split(" ") if list_arg[0] not in HBNBCommand.classes: print("** class doesn't exist **") return new_instance = HBNBCommand.classes[list_arg[0]]() if len(list_arg) > 1: for i in list_arg[1:]: key = i.split('=')[0] val = i.split('=')[1] if val[0] is '\"' or val[0] is "'": val = val.replace('_', ' ') val = val[1:-1] elif "." in val: val = float(val) else: val = int(val) setattr(new_instance, key, val) storage.new(new_instance) storage.save() print(new_instance.id)
def do_create(self, args): """ Create an object of any class""" input_list = args.split(" ") class_name = input_list[0] if not args: print("** class name missing **") return elif class_name not in HBNBCommand.classes: print("** class doesn't exist **") return new_instance = HBNBCommand.classes[class_name]() if len(input_list) >= 2: for parameter in input_list[1:]: key, value = parameter.split("=") if value[0] == '"': value = value.replace('"', '') value = value.replace('_', ' ') elif value.isdecimal(): value = int(value) else: value = float(value) setattr(new_instance, key, value) print(new_instance.id) storage.save() new_instance.save()
def do_create(self, args): """ Create an object of any class""" if not args: print("** class name missing **") return else: args = args.split(' ') if args[0] not in HBNBCommand.classes: print("** class doesn't exist **") return new_instance = HBNBCommand.classes[args[0]]() for element in args[1:]: atr = element.split('=') if atr[1][0] == '"': atr[1] = atr[1][1:-1].replace('"', '\\"') atr[1] = atr[1].replace("_", " ") elif atr[1].isdecimal is True: atr[1] = Int(atr[1]) else: try: float(atr[1]) except ValueError: pass setattr(new_instance, atr[0], atr[1]) print(new_instance.id) if getenv('HBNB_TYPE_STORAGE') == 'db': storage.new(new_instance) storage.save() else: new_instance.save()
def do_create(self, args): """ Create an object of any class""" if not args: print("** class name missing **") #------ parameters = args.split(" ") if parameters[0] not in HBNBCommand.classes: print("** class doesn't exist **") print(parameters[0]) return new_instance = HBNBCommand.classes[parameters[0]]() kwargs = {} for param in parameters[1:]: key, value = param.split("=") if value[0] == '"': value = value.strip('"').replace("_", " ") setattr(new_instance, key, value) try: float(value) except ValueError: pass try: int(value) except ValueError: pass new_instance.save() storage.save() print(new_instance.id)
def do_create(self, args): """ Create an object of any class""" if not args: print("** class name missing **") return else: arg_lst = args.split() class_name = arg_lst[0] if class_name not in HBNBCommand.classes: print("** class doesn't exist **") return new_instance = HBNBCommand.classes[arg_lst[0]]() if len(arg_lst) > 1: dict_t = {} i = 0 for parameter in arg_lst: if i == 0: i += 1 continue else: str_obj = parameter.split("=") dict_t[str_obj[0]] = str_obj[1] for key, value in dict_t.items(): if value[0] == '"': value = value[1:-1] value = value.replace('"', '\\') value = value.replace('_', ' ') elif "." in value: value = float(value) else: value = int(value) setattr(new_instance, key, value) print(new_instance.id) storage.save() new_instance.save()
def do_destroy(self, usr_in): """Destroys the instance with a specific id""" _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) == 2: for key in objects.keys(): if key == _input[1]: del objects[key] switch = 1 break storage.save() if switch == 0: print("** no instance found **")
def do_destroy(self, arg): ''' Deletes an instance basesd on class name and id ''' new_args = "" class_name = "" class_id = "" try: new_args = arg.split(" ") class_name = new_args[0] class_id = new_args[1] except BaseException: pass if not class_name: print('** class name missing **') elif class_name not in Class_Dict: print("** class doesn't exist **") elif not class_id: print("** instance id missing **") else: new_key = class_name + '.' + class_id try: del (storage._FileStorage__objects[new_key]) storage.save() except KeyError: print("** no instance found **")
def do_create(self, args): """ Create an object of any class""" from os import getenv args = args.split() if not args: print("** class name missing **") return elif args[0] not in HBNBCommand.classes: print("** class doesn't exist **") return new_instance = HBNBCommand.classes[args[0]]() print(new_instance.id) if getenv('HBNB_TYPE_STORAGE') != 'db': new_instance.save() storage.save() if len(args) > 1: dct = ['{'] for i in range(1, len(args)): tmp = args[i].split('=') tmp[1] = tmp[1].replace('_', ' ') dct.append('"' + tmp[0] + '": ' + tmp[1]) if i != len(args) - 1: dct.append(', ') dct.append('}') self.do_update(args[0] + ' ' + new_instance.id + ' ' + "".join(dct)) else: for i in range(1, len(args)): tmp = args[i].split('=') tmp[1] = tmp[1].replace('_', ' ') setattr(new_instance, tmp[0], tmp[1]) new_instance.save() storage.save()
def do_create(self, args): """ Create an object of any class""" n_args = args.split(" ") if not args: print("** class name missing **") return elif n_args[0] not in HBNBCommand.classes: print("** class doesn't exist **") return new_instance = HBNBCommand.classes[n_args[0]]() # update added here # storage.new(new_instance) for i in range(1, len(n_args)): if '=' in n_args[i]: key_value = n_args[i].split("=") if key_value[0] == 'id': key_value[1] = str(key_value[1]) new_instance.__dict__[key_value[0]] = key_value[1] if key_value[1][0] == '"': if '_' in key_value[1]: key_value[1] = key_value[1].replace("_", " ") new_instance.__dict__[key_value[0]] = key_value[1][1:-1] elif '.' in key_value[1]: new_instance.__dict__[key_value[0]] = float(key_value[1]) elif int(key_value[1]): new_instance.__dict__[key_value[0]] = int(key_value[1]) print(new_instance.id) storage.new(new_instance) storage.save()
def do_destroy(self, args): """ Destroys a specified object """ new = args.partition(" ") c_name = new[0] c_id = new[2] if c_id and ' ' in c_id: c_id = c_id.partition(' ')[0] if not c_name: print("** class name missing **") return if c_name not in HBNBCommand.classes: print("** class doesn't exist **") return if not c_id: print("** instance id missing **") return key = c_name + "." + c_id try: del(storage.all()[key]) storage.save() except KeyError: print("** no instance found **")
def do_create(self, args): """ Create an object of any class""" if not args: print("** class name missing **") return args = args.split() if args[0] not in HBNBCommand.classes: print("** class doesn't exist **") return attr = {} if len(args) > 1: for i in range(1, len(args)): key, value = args[i].split("=") if value[0] == '"': value = value.replace('_', ' ') value = value[1:-1] elif '.' in value: value = float(value) else: value = int(value) attr.update({key: value}) new_instance = HBNBCommand.classes[args[0]](**attr) new_instance.save() storage.save() print(new_instance.id) storage.save()