Python natsort.natsorted() Examples
The following are 30
code examples of natsort.natsorted().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
natsort
, or try the search function
.
Example #1
Source File: FileList.py From ClearMap with GNU General Public License v3.0 | 6 votes |
def readFileList(filename): """Returns list of files that match the regular expression Arguments: filename (str): file name as regular expression Returns: str, list: path of files, file names that match the regular expression """ #get path (fpath, fname) = os.path.split(filename) fnames = os.listdir(fpath); #fnames = [os.path.join(fpath, x) for x in fnames]; searchRegex = re.compile(fname).search fl = [ l for l in fnames for m in (searchRegex(l),) if m] if fl == []: raise RuntimeError('no files found in ' + fpath + ' match ' + fname + ' !'); #fl.sort(); return fpath, natsort.natsorted(fl);
Example #2
Source File: utils.py From deep-smoke-machine with BSD 3-Clause "New" or "Revised" License | 6 votes |
def folder_pathes(path, is_nat_sort=False): if not os.path.exists(path): exp_msg = 'Sorry, folder path does not exist: %s' % (path) raise Exception(exp_msg) names = os.walk(path).next()[1] if is_nat_sort: names = natsort.natsorted(names) pathes = ['%s/%s' % (path, n) for n in names] return pathes # endregion # region Normalization
Example #3
Source File: banlist.py From yugioh-game with MIT License | 6 votes |
def __list_cards(self, cards, pl, currently_allowed = None, previously_allowed = None): # getting all data data = pl.cdb.execute('SELECT id, name FROM texts WHERE id IN ({0})'.format(', '.join([str(c) for c in cards]))).fetchall() if len(data) < len(cards): # not all cards are available in the player's language # fill up with english ones already_present = set([d[0] for d in data]) remaining = cards - already_present data_r = globals.language_handler.primary_database.execute('SELECT id, name FROM texts WHERE id IN ({0})'.format(', '.join([str(c) for c in remaining]))).fetchall() data = data + data_r for d in natsort.natsorted(data, key = lambda d: d[1]): if currently_allowed and previously_allowed: pl.notify("\t" + pl._("{0} (now at {1}, previously at {2})").format(d[1], currently_allowed[d[0]], previously_allowed[d[0]])) elif previously_allowed: pl.notify("\t" + pl._("{0}, (previously at {1})").format(d[1], previously_allowed[d[0]])) else: pl.notify("\t" + d[1])
Example #4
Source File: web.py From calibre-web with GNU General Public License v3.0 | 6 votes |
def series_list(): if current_user.check_visibility(constants.SIDEBAR_SERIES): if current_user.series_view == 'list': entries = calibre_db.session.query(db.Series, func.count('books_series_link.book').label('count')) \ .join(db.books_series_link).join(db.Books).filter(calibre_db.common_filters()) \ .group_by(text('books_series_link.series')).order_by(db.Series.sort).all() charlist = calibre_db.session.query(func.upper(func.substr(db.Series.sort, 1, 1)).label('char')) \ .join(db.books_series_link).join(db.Books).filter(calibre_db.common_filters()) \ .group_by(func.upper(func.substr(db.Series.sort, 1, 1))).all() return render_title_template('list.html', entries=entries, folder='web.books_list', charlist=charlist, title=_(u"Series"), page="serieslist", data="series") else: entries = calibre_db.session.query(db.Books, func.count('books_series_link').label('count')) \ .join(db.books_series_link).join(db.Series).filter(calibre_db.common_filters()) \ .group_by(text('books_series_link.series')).order_by(db.Series.sort).all() charlist = calibre_db.session.query(func.upper(func.substr(db.Series.sort, 1, 1)).label('char')) \ .join(db.books_series_link).join(db.Books).filter(calibre_db.common_filters()) \ .group_by(func.upper(func.substr(db.Series.sort, 1, 1))).all() return render_title_template('grid.html', entries=entries, folder='web.books_list', charlist=charlist, title=_(u"Series"), page="serieslist", data="series", bodyClass="grid-view") else: abort(404)
Example #5
Source File: utils.py From timeception with GNU General Public License v3.0 | 6 votes |
def folder_pathes(path, is_nat_sort=False): if not os.path.exists(path): exp_msg = 'Sorry, folder path does not exist: %s' % (path) raise Exception(exp_msg) names = os.walk(path).next()[1] if is_nat_sort: names = natsort.natsorted(names) pathes = ['%s/%s' % (path, n) for n in names] return pathes # endregion # region Normalization
Example #6
Source File: duel.py From yugioh-game with MIT License | 6 votes |
def show_usable(self, pl): summonable = natsort.natsorted([card.get_spec(pl) for card in self.summonable]) spsummon = natsort.natsorted([card.get_spec(pl) for card in self.spsummon]) repos = natsort.natsorted([card.get_spec(pl) for card in self.repos]) mset = natsort.natsorted([card.get_spec(pl) for card in self.idle_mset]) idle_set = natsort.natsorted([card.get_spec(pl) for card in self.idle_set]) idle_activate = natsort.natsorted([card.get_spec(pl) for card in self.idle_activate]) if summonable: pl.notify(pl._("Summonable in attack position: %s") % ", ".join(summonable)) if mset: pl.notify(pl._("Summonable in defense position: %s") % ", ".join(mset)) if spsummon: pl.notify(pl._("Special summonable: %s") % ", ".join(spsummon)) if idle_activate: pl.notify(pl._("Activatable: %s") % ", ".join(idle_activate)) if repos: pl.notify(pl._("Repositionable: %s") % ", ".join(repos)) if idle_set: pl.notify(pl._("Settable: %s") % ", ".join(idle_set))
Example #7
Source File: utils.py From hwrt with MIT License | 6 votes |
def get_latest_folder(folder): """Get the absolute path of a subfolder that comes last with natural sorting in the given folder. """ folders = [ os.path.join(folder, name) for name in os.listdir(folder) if os.path.isdir(os.path.join(folder, name)) ] folders = natsort.natsorted(folders, reverse=True) if len(folders) == 0: # No model folder! logger.error( "You don't have any model folder. I suggest you " "have a look at " "https://github.com/MartinThoma/hwr-experiments and " "http://pythonhosted.org/hwrt/" ) sys.exit(-1) else: return os.path.abspath(folders[0])
Example #8
Source File: utils.py From yugioh-game with MIT License | 6 votes |
def parse_lflist(filename): lst = {} with open(filename, 'r', encoding='utf-8') as fp: for line in fp: line = line.rstrip('\n') if not line or line.startswith('#'): continue elif line.startswith('!'): section = line[1:].lower() lst[section] = Banlist(section) else: code, num_allowed, *extra = line.split(' ', 2) code = int(code) num_allowed = int(num_allowed) lst[section].add(code, num_allowed) return collections.OrderedDict(natsort.natsorted(lst.items(), reverse=True))
Example #9
Source File: shared_cli.py From VeRyPy with MIT License | 6 votes |
def get_a_problem_file_list(problem_paths): files_to_solve = [] for problem_path in problem_paths: if path.isdir(problem_path): for in_fn in natsorted(glob(path.join(problem_path, "*.vrp"))): files_to_solve.append( in_fn ) for in_fn in natsorted(glob(path.join(problem_path, "*.tsp"))): files_to_solve.append( in_fn ) for in_fn in natsorted(glob(path.join(problem_path, "*.pickle"))): files_to_solve.append( in_fn ) elif path.isfile(problem_path) and problem_path[-4:].lower()==".txt": with open(problem_path, 'r') as vrp_list_file: for line in vrp_list_file.readlines(): line = line.strip() if path.isfile(line): files_to_solve.append(line) elif path.isfile(problem_path) and (problem_path[-4:].lower()==".vrp" or problem_path[-4:].lower()==".tsp" or problem_path[-7:].lower()==".pickle"): files_to_solve.append( problem_path ) else: print(problem_path, "is not a .vrp file, folder, or text file", file=sys.stderr) return files_to_solve
Example #10
Source File: _transformer.py From pepy with MIT License | 6 votes |
def transform_project_v2(project: Project, all_data: bool) -> Dict: day_downloads = defaultdict(lambda: defaultdict(int)) if all_data: last_downloads = project.last_downloads() else: month_ago = datetime.now().date() - timedelta(days=30) last_downloads = project.last_downloads(month_ago) for d in last_downloads: day_downloads[d.date.isoformat()][d.version] = d.downloads.value return { "id": project.name.name, "total_downloads": project.total_downloads.value, "versions": natsorted(list(project.versions())), "downloads": day_downloads, }
Example #11
Source File: mangascrapper.py From MangaScrapper with Apache License 2.0 | 6 votes |
def _create_pdf_(chap_save_loc): img_list = natsorted(os.listdir(chap_save_loc)) pdf_save_loc = chap_save_loc + ".pdf" doc = SimpleDocTemplate(pdf_save_loc, pagesize=A2) parts = [Image(os.path.join(chap_save_loc, img)) for img in img_list] try: doc.build(parts) except PermissionError: logging.error("Missing Permission to write. File open in system editor or missing " "write permissions.")
Example #12
Source File: filekeeper.py From hostthedocs with MIT License | 6 votes |
def parse_docfiles(docfiles_dir, link_root): """ Create the list of the projects. The list of projects is computed by walking the `docfiles_dir` and searching for project paths (<project-name>/<version>/index.html) """ if not os.path.exists(docfiles_dir): return {} projects = list() for folder in natsort.natsorted(os.listdir(docfiles_dir), key=str.lower): if not os.path.isdir(os.path.join(docfiles_dir, folder)): continue project = _get_proj_dict(docfiles_dir, folder, link_root) if project is not None: projects.append(project) return projects
Example #13
Source File: FontEffectsdataset.py From FET-GAN with MIT License | 6 votes |
def __init__(self, opt): """Initialize this dataset class. Parameters: opt (option dicts) -- stores all the experiment options; """ self.opt = opt self.K = opt['K'] self.fonteffects_dir = opt['fonteffects_dir'] self.num_cls = opt['num_cls'] self.clsdict = {} for i,source_cls in enumerate(source_cls for source_cls in natsorted(os.listdir(self.fonteffects_dir)) if not source_cls.startswith('.')): self.clsdict[str(source_cls)] = i if i >= self.num_cls-1: break self.transform_fonteffects = get_transform(self.opt)
Example #14
Source File: species.py From funannotate with BSD 2-Clause "Simplified" License | 6 votes |
def showAll(dir): Table = [] TableHeader = ['Species', 'Augustus', 'GeneMark', 'Snap', 'GlimmerHMM', 'CodingQuarry', 'Date'] for f in os.listdir(dir): ff = os.path.join(dir, f) if os.path.isdir(ff) and lib.checkannotations(os.path.join(ff, 'info.json')): with open(os.path.join(ff, 'info.json')) as infile: data = json.load(infile) sources = [f] for x in ['augustus', 'genemark', 'snap', 'glimmerhmm', 'codingquarry']: if x in data: if len(data[x][0]) < 1: sources.append('None') else: sourceFile = data[x][0]['source'] if ': ' in sourceFile: sourceFile = sourceFile.split(':')[0] sources.append(sourceFile) sources.append(data['augustus'][0]['date']) Table.append(sources) Table = natsorted(Table, key=lambda x: x[0]) Table.insert(0, TableHeader) lib.print_table(Table, max_col_width=40)
Example #15
Source File: utils.py From grouped-ssd-pytorch with MIT License | 6 votes |
def read_dicom_series(directory, filepattern="P_*"): """ Reads a DICOM Series files in the given directory. Only filesnames matching filepattern will be considered""" if not os.path.exists(directory) or not os.path.isdir(directory): raise ValueError("Given directory does not exist or is a file : " + str(directory)) # print('\tRead Dicom', directory) lstFilesDCM = natsort.natsorted(glob.glob(os.path.join(directory, filepattern))) # print('\tLength dicom series', len(lstFilesDCM)) # Get ref file RefDs = dicom.read_file(lstFilesDCM[0]) # Load dimensions based on the number of rows, columns, and slices (along the Z axis) ConstPixelDims = (int(RefDs.Rows), int(RefDs.Columns), len(lstFilesDCM)) # The array is sized based on 'ConstPixelDims' ArrayDicom = np.zeros(ConstPixelDims, dtype=RefDs.pixel_array.dtype) # loop through all the DICOM files for filenameDCM in lstFilesDCM: # read the file ds = dicom.read_file(filenameDCM) # store the raw image data ArrayDicom[:, :, lstFilesDCM.index(filenameDCM)] = ds.pixel_array return ArrayDicom
Example #16
Source File: data_checker_year1_extended.py From grouped-ssd-pytorch with MIT License | 6 votes |
def read_dicom_series(directory, filepattern="P_*"): """ Reads a DICOM Series files in the given directory. Only filesnames matching filepattern will be considered""" if not os.path.exists(directory) or not os.path.isdir(directory): raise ValueError("Given directory does not exist or is a file : " + str(directory)) # print('\tRead Dicom', directory) lstFilesDCM = natsort.natsorted(glob.glob(os.path.join(directory, filepattern))) # print('\tLength dicom series', len(lstFilesDCM)) # Get ref file RefDs = dicom.read_file(lstFilesDCM[0]) # Load dimensions based on the number of rows, columns, and slices (along the Z axis) ConstPixelDims = (int(RefDs.Rows), int(RefDs.Columns), len(lstFilesDCM)) # The array is sized based on 'ConstPixelDims' ArrayDicom = np.zeros(ConstPixelDims, dtype=RefDs.pixel_array.dtype) # loop through all the DICOM files for filenameDCM in lstFilesDCM: # read the file ds = dicom.read_file(filenameDCM) # store the raw image data ArrayDicom[:, :, lstFilesDCM.index(filenameDCM)] = ds.pixel_array return ArrayDicom
Example #17
Source File: ct_to_jpg.py From grouped-ssd-pytorch with MIT License | 6 votes |
def read_dicom_series(directory, filepattern="P_*"): """ Reads a DICOM Series files in the given directory. Only filesnames matching filepattern will be considered""" if not os.path.exists(directory) or not os.path.isdir(directory): raise ValueError("Given directory does not exist or is a file : " + str(directory)) # print('\tRead Dicom', directory) lstFilesDCM = natsort.natsorted(glob.glob(os.path.join(directory, filepattern))) # print('\tLength dicom series', len(lstFilesDCM)) # Get ref file RefDs = dicom.read_file(lstFilesDCM[0]) # Load dimensions based on the number of rows, columns, and slices (along the Z axis) ConstPixelDims = (int(RefDs.Rows), int(RefDs.Columns), len(lstFilesDCM)) # The array is sized based on 'ConstPixelDims' ArrayDicom = np.zeros(ConstPixelDims, dtype=RefDs.pixel_array.dtype) # loop through all the DICOM files for filenameDCM in lstFilesDCM: # read the file ds = dicom.read_file(filenameDCM) # store the raw image data ArrayDicom[:, :, lstFilesDCM.index(filenameDCM)] = ds.pixel_array return ArrayDicom
Example #18
Source File: files.py From pyVSR with GNU General Public License v3.0 | 6 votes |
def request_files(dataset_dir, protocol='speaker_independent', speaker_id=None, view_id='1', utterance_types='dst', content='video'): if protocol == 'single_speaker': train, test = _preload_files_single_volunteer(dataset_dir, speaker_id, view_id, utterance_types) elif protocol == 'speaker_independent': train, test = _preload_files_speaker_independent(dataset_dir, view_id, utterance_types) if content == 'audio': import re train = [re.sub('_v\d_', '_', file) for file in train] train = [re.sub('.mp4', '.wav', file) for file in train] test = [re.sub('_v\d_', '_', file) for file in test] test = [re.sub('.mp4', '.wav', file) for file in test] else: raise Exception('undefined dataset split protocol') return natsorted(train), natsorted(test)
Example #19
Source File: image_stack.py From muDIC with MIT License | 6 votes |
def find_file_names(path, type=".png"): """ Finds all files with the given extension in the folder path. Parameters ---------- path : str The path to the folder containing the files of interest type : str The file postfix such as ".png", ".bmp" etc. Returns ------- List of filenames """ return natsorted([os.path.join(path, file) for file in os.listdir(path) if file.endswith(type)])
Example #20
Source File: test_filekeeper.py From hostthedocs with MIT License | 5 votes |
def test_sorts(self): vers = [ '1.1', '1.2alpha', '1.2beta1', '1.2beta2', '1.2rc1', '1.2', '1.2.1', '1.3' ] vers = [dict(version=v) for v in vers] randvers = list(vers) random.shuffle(randvers) self.assertNotEqual(vers, randvers) randvers = natsort.natsorted(randvers, key=fk.sort_by_version) self.assertEqual(vers, randvers)
Example #21
Source File: duel_parser.py From yugioh-game with MIT License | 5 votes |
def show_watchers(caller): watchers = [w for w in caller.connection.player.duel.watchers if w.watching is True] if len(watchers) == 0: caller.connection.notify(caller.connection._("No one is watching this duel.")) else: caller.connection.notify(caller.connection._("People watching this duel:")) for pl in natsort.natsorted(watchers, key=lambda x: x.nickname): caller.connection.notify(pl.nickname)
Example #22
Source File: announce_race.py From yugioh-game with MIT License | 5 votes |
def announce_race(self, player, count, avail): pl = self.players[player] racemap = {pl.strings['system'][RACES_OFFSET+i]: (1<<i) for i in range(AMOUNT_RACES)} avail_races = {k: v for k, v in racemap.items() if avail & v} avail_races_keys = natsort.natsorted(list(avail_races.keys())) def prompt(): pl.notify("Type %d races separated by spaces." % count) for i, s in enumerate(avail_races_keys): pl.notify("%d: %s" % (i+1, s)) pl.notify(DuelReader, r, no_abort="Invalid entry.", restore_parser=DuelParser) def error(text): pl.notify(text) pl.notify(DuelReader, r, no_abort="Invalid entry.", restore_parser=DuelParser) def r(caller): ints = [] try: for i in caller.text.split(): ints.append(int(i) - 1) except ValueError: return error("Invalid value.") if len(ints) != count: return error("%d items required." % count) if len(ints) != len(set(ints)): return error("Duplicate values not allowed.") if any(i > len(avail_races) - 1 or i < 0 for i in ints): return error("Invalid value.") result = 0 for i in ints: result |= avail_races[avail_races_keys[i]] self.set_responsei(result) reactor.callLater(0, process_duel, self) prompt()
Example #23
Source File: extract_3dircadb.py From grouped-ssd-pytorch with MIT License | 5 votes |
def read_dicom_series(directory, filepattern="image_*"): """ Reads a DICOM Series files in the given directory. Only filesnames matching filepattern will be considered""" if not os.path.exists(directory) or not os.path.isdir(directory): raise ValueError("Given directory does not exist or is a file : " + str(directory)) print '\tRead Dicom', directory lstFilesDCM = natsort.natsorted(glob.glob(os.path.join(directory, filepattern))) print '\tLength dicom series', len(lstFilesDCM) # Get ref file RefDs = dicom.read_file(lstFilesDCM[0]) # Load dimensions based on the number of rows, columns, and slices (along the Z axis) ConstPixelDims = (int(RefDs.Rows), int(RefDs.Columns), len(lstFilesDCM)) # The array is sized based on 'ConstPixelDims' ArrayDicom = np.zeros(ConstPixelDims, dtype=RefDs.pixel_array.dtype) # loop through all the DICOM files for filenameDCM in lstFilesDCM: # read the file ds = dicom.read_file(filenameDCM) # store the raw image data ArrayDicom[:, :, lstFilesDCM.index(filenameDCM)] = ds.pixel_array return ArrayDicom
Example #24
Source File: utils.py From bootcamp with Apache License 2.0 | 5 votes |
def load_best_checkpoint(checkpoint_dir): checkpoints = natsorted(glob(os.path.join(checkpoint_dir, '*.h5'))) if len(checkpoints) != 0: return checkpoints[-1] return None
Example #25
Source File: utilities.py From scGAN with MIT License | 5 votes |
def save_generated_cells(fake_cells, file_name, fake_labels=None): """ Functions that writes a gene expression matrix and the associated cluster indices into a file. Check the AnnData documentation of the write method to check the supported formats. Parameters ---------- fake_cells : 2-D array A matrix (cells x genes) containing the expression levels. It can be dense or sparse. It will be encoded in a sparse format. file_name : str Path of the file to write to. fake_labels : array an array containing the cluster indices of the corresponding cells. Default is None. Returns ------- """ s_gen_mat = sp_sparse.csr_matrix(fake_cells) sc_fake = sc.AnnData(s_gen_mat) if fake_labels is not None: groups = fake_labels.astype('U') unique_groups = np.unique(groups) sc_fake.obs['cluster'] = pd.Categorical( values=groups, categories=natsorted(unique_groups)) sc_fake.obs_names = np.repeat('fake', sc_fake.shape[0]) sc_fake.obs_names_make_unique() sc_fake.write(file_name)
Example #26
Source File: user_manager.py From INGInious with GNU Affero General Public License v3.0 | 5 votes |
def get_course_groups(self, course): """ Returns a list of the course groups""" return natsorted(list(self._database.groups.find({"courseid": course.get_id()})), key=lambda x: x["description"])
Example #27
Source File: utils.py From bootcamp with Apache License 2.0 | 5 votes |
def delete_older_checkpoints(checkpoint_dir, max_to_keep=5): assert max_to_keep > 0 checkpoints = natsorted(glob(os.path.join(checkpoint_dir, '*.h5'))) checkpoints_to_keep = checkpoints[-max_to_keep:] for checkpoint in checkpoints: if checkpoint not in checkpoints_to_keep: os.remove(checkpoint)
Example #28
Source File: user_manager.py From INGInious with GNU Affero General Public License v3.0 | 5 votes |
def get_course_audiences(self, course): """ Returns a list of the course audiences""" return natsorted(list(self._database.audiences.find({"courseid": course.get_id()})), key=lambda x: x["description"])
Example #29
Source File: deck_editor.py From yugioh-game with MIT License | 5 votes |
def list_public_decks(self): pl = self.player session = pl.connection.session decks = list(session.query(models.Deck).filter_by(public = True)) accs = {} for deck in decks: accs[deck.account.name + "/" + deck.name] = deck accs = OrderedDict(natsort.natsorted(accs.items())) pl.notify(pl._("{0} public decks available:").format(len(decks))) for acc in accs.keys(): d = accs[acc] banlist_text = pl._("compatible with no banlist") for b in globals.banlists.values(): content = json.loads(d.content) if len(b.check(content.get('cards', []) + content.get('side', []))) == 0: banlist_text = pl._("compatible with {0} banlist").format(b.name) break pl.notify(pl._("{deckname} ({banlist})").format(deckname = acc, banlist = banlist_text))
Example #30
Source File: utils.py From epiScanpy with BSD 3-Clause "New" or "Revised" License | 5 votes |
def unique_categories(categories): """Pass array-like categories, return sorted cleaned unique categories.""" categories = np.unique(categories) categories = np.setdiff1d(categories, np.array(settings.categories_to_ignore)) categories = np.array(natsorted(categories, key=lambda v: v.upper())) return categories