def test_progress_not_precise(): """Unit test for method progress, not precise case.""" test_generator = range(10) test_unpacked = [*test_generator] test_result_generator = FileIO.progress(test_generator, precise=False) test_result_unpacked = FileIO.progress(test_unpacked, precise=False) for t_gen, r_gen in zip(test_generator, test_result_generator): assert t_gen == r_gen for t_unp, r_unp in zip(test_unpacked, test_result_unpacked): assert t_unp == r_unp
def test_progress_precise(): """Unit test for method progress, not precise case.""" def test_gen(): """Closure to test functions which returns generators.""" for value in range(10): yield value test_generator = range(10) test_unpacked = [*test_generator] test_result_generator = FileIO.progress(test_generator, precise=True) test_result_unpacked = FileIO.progress(test_unpacked, precise=True) test_result_func_gen = FileIO.progress(test_gen(), precise=True) for t_gen, r_gen in zip(test_generator, test_result_generator): assert t_gen == r_gen for t_unp, r_unp in zip(test_unpacked, test_result_unpacked): assert t_unp == r_unp for t_fgen, r_fgen in zip(test_generator, test_result_func_gen): assert t_fgen == r_fgen
def export_by_group_by(self, path, top=10, **kwargs): """ Saves images, creating directories, based on their groups. Parameters ---------- path: str Place to create the directories and export images top (optional, default 10): How many similar internal images should be returned position (optional): int Returns the groups based on a specified position. """ for element in FileIO.progress( self.group_by( top=top, position=kwargs.get('position') ) ): if isinstance(element, dict): item = [*element.keys()][0] similars = element[item] elif isinstance(element, list): item = kwargs['position'] similars = element save_path = os.path.join( path, str(item) ) os.makedirs( save_path, exist_ok=True ) try: copyfile( self._image_database.mount_file_name( item, 'jpg' ), os.path.join( save_path, 'group.jpg' ) ) except FileNotFoundError: continue for rank, similar in enumerate(similars): original_file_path = self._image_database.mount_file_name( similar, 'jpg' ) try: copyfile( original_file_path, os.path.join( save_path, f'{rank + 1}.jpg' ) ) except FileNotFoundError: continue