def add_new_image(name: str, id: int, new_name, dirs: List[str], axis: int = 0) -> None: """ Adds new image with all the directories for given original image in meta :param name: Name of image :param id: Id of image :param new_name: New name of which image should be generated :param dirs: List of directories to the new images :param axis: 0 for horizontal and 1 for vertical :return: None """ name: str = name.lower() data: Dict[str, any] = load_json(DB_DIR) if name not in data.keys(): return if id not in range(0, len(data[name])): return if new_name not in data[name][id]['new_imgs'].keys(): data[name][id]['new_imgs'][new_name]: Dict[str, any] = { "new_name": new_name, "dirs": { "axis": [[], []] } } data[name][id]['new_imgs'][new_name]['dirs']['axis'][axis]: List[ str] = dirs save_json(DB_DIR, data)
def create_db(directory: str) -> None: """ Creates empty json object representing a database :param directory: Directory of json file :return: None """ save_json(directory, {})
def drop_db(directory: str) -> None: """ Drops whole meta and leaves it empty. Database needs to be created again. :param directory: Directory of db :return: None """ save_json(directory, None)
def test(): n: int = int(100000) result = throw_n_dices(n) odds = dict({ strategy: get_success_rate(guess_n_times(n, result, strategy), result) for strategy in get_strategy_ideas() }) path = "results/" file = "%s%i.json" % (path, len(listdir(path))) save_json(file, {"n": n, "strategy": odds})
def delete_original_image(name: str, id: int) -> None: """ Deletes an original image in meta based on given name and id. Does nothing, if image does not exist. :param name: Name of image :param id: Id of image :return: None """ name: str = name.lower() data: Dict[str, any] = load_json(DB_DIR) if name not in data.keys(): return if id not in range(0, len(data[name])): return data[name].pop(id) if len(data[name]) == 0: data.pop(name) save_json(DB_DIR, data)
def add_original_image(name: str, filename: Optional[str] = None) -> Dict[str, any]: """ Adds new original image in meta file with additional directories and returns created object. :param name: Name of image :param filename: Filename :return: Created new object """ name: str = name.lower() data: Dict[str, any] = load_json(DB_DIR) if name not in data.keys(): data[name]: List[Dict[str, any]] = [] id: int = len(data[name]) filename: str = name + '.' + filename.split('.')[-1] directory: str = IMAGE_DIR + "%i-%s" % (id, filename) new_obj: Dict[str, any] = {'name': name, 'dir': directory, 'new_imgs': {}} data[name].append(new_obj) save_json(DB_DIR, data) return new_obj