def do_update(self, line): """Update a given instance of a model""" try: tokens = split(line) except ValueError: return None if len(tokens) < 1: print("** class name missing **") else: objects = models.storage.all() cls = models.getmodel(tokens[0]) if cls is None: print("** class doesn't exist **") elif len(tokens) < 2: print("** instance id missing **") elif ".".join(tokens[:2]) not in objects: print("** no instance found **") elif len(tokens) < 3: print("** attribute name missing **") elif len(tokens) < 4: print("** value missing **") else: obj = objects[".".join(tokens[:2])] for key, value in zip(tokens[2::2], tokens[3::2]): try: setattr(obj, key, int(value)) except ValueError: try: setattr(obj, key, float(value)) except ValueError: try: setattr(obj, key, str(value)) except ValueError: pass obj.save()
def reload(self): """ Load the dictionary of saved model instances from the filesystem """ try: with open(self.__class__.__file_path, "r") as ifile: objects = load(ifile) for key, val in objects.items(): cls = models.getmodel(key.split(".")[0]) if cls: self.__class__.__objects[key] = cls(**val) except FileNotFoundError: pass
def do_create(self, line): """Instantiate a given model""" try: tokens = split(line) except ValueError: return None if len(tokens) < 1: print("** class name missing **") else: cls = models.getmodel(tokens[0]) if cls is None: print("** class doesn't exist **") else: instance = cls() models.storage.save() print(instance.id)
def do_all(self, line): """Show all instances of a given model or if unspecified, all models""" try: tokens = split(line) except ValueError: return if len(tokens) < 1: objects = models.storage.all() print([str(obj) for obj in objects.values()]) else: cls = models.getmodel(tokens[0]) if cls is None: print("** class doesn't exist **") return objects = models.storage.all() print([str(obj) for obj in objects.values() if type(obj) is cls])
def do_count(self, line): """Count the instances of a given model""" try: tokens = split(line) except ValueError: return None objects = models.storage.all() if len(tokens) < 1: print("** class name missing **") else: cls = models.getmodel(tokens[0]) if cls is None: print("** class doesn't exist **") else: matches = 0 for obj in objects.values(): if type(obj) is cls: matches += 1 print(matches)
def do_show(self, line): """Show a given instance of a model""" try: tokens = split(line) except ValueError: return None if len(tokens) < 1: print("** class name missing **") else: objects = models.storage.all() cls = models.getmodel(tokens[0]) if cls is None: print("** class doesn't exist **") elif len(tokens) < 2: print("** instance id missing **") elif ".".join(tokens[:2]) not in objects: print("** no instance found **") else: print(objects[".".join(tokens[:2])])
def do_destroy(self, line): """Delete a given instance of a model""" try: tokens = split(line) except ValueError: return if len(tokens) < 1: print("** class name missing **") return cls = models.getmodel(tokens[0]) if cls is None: print("** class doesn't exist **") return if len(tokens) < 2: print("** instance id missing **") return objects = models.storage.all() key = ".".join(tokens[0:2]) if key not in objects: print("** no instance found **") return del objects[key] models.storage.save()
dataLoaderTrain = DataLoader(dataset=datasetTrain, batch_size=trBatchSize, shuffle=True, num_workers=24, pin_memory=True) dataLoaderVal = DataLoader(dataset=datasetValid, batch_size=trBatchSize, shuffle=False, num_workers=24, pin_memory=True) dataLoaderTest = DataLoader(dataset=datasetTest, num_workers=24, pin_memory=True) model = mod.getmodel(modelName, nnClassCount) model = torch.nn.DataParallel(model) if action == "train": # train the model timestampTime = time.strftime("%H%M%S") timestampDate = time.strftime("%d%m%Y") timestampLaunch = timestampDate + '-' + timestampTime batch, losst, losse = CheXpertTrainer.train(CheXpertTrainer, model, dataLoaderTrain, dataLoaderVal, nnClassCount, trMaxEpoch, timestampLaunch,
dataLoaderTrain = DataLoader(dataset=datasetTrain, batch_size=trBatchSize, shuffle=True, num_workers=24, pin_memory=True) dataLoaderVal = DataLoader(dataset=datasetValid, batch_size=trBatchSize, shuffle=False, num_workers=24, pin_memory=True) dataLoaderTest = DataLoader(dataset=datasetTest, num_workers=24, pin_memory=True) # initialize and load the model model = mod.getmodel(modelName, nnClassCount) model = torch.nn.DataParallel(model).cuda() if action == "train": # train the model timestampTime = time.strftime("%H%M%S") timestampDate = time.strftime("%d%m%Y") timestampLaunch = timestampDate + '-' + timestampTime batch, losst, losse = CheXpertTrainer.train(CheXpertTrainer, model, dataLoaderTrain, dataLoaderVal, nnClassCount, trMaxEpoch, timestampLaunch,