Use stringtools.natural_sorter.

master
voussoir 2021-06-07 22:49:26 -07:00
parent 40a3e9d1a0
commit 53127e59ad
No known key found for this signature in database
GPG Key ID: 5F7554F8C26DACCB
2 changed files with 3 additions and 14 deletions

View File

@ -328,18 +328,6 @@ def is_xor(*args):
'''
return [bool(a) for a in args].count(True) == 1
def natural_sorter(x):
'''
Used for sorting files in 'natural' order instead of lexicographic order,
so that you get 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
instead of 1 10 11 12 13 2 3 4 5 ...
Thank you Mark Byers
http://stackoverflow.com/a/11150413
'''
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
return alphanum_key(x)
def now(timestamp=True):
'''
Return the current UTC timestamp or datetime object.

View File

@ -17,6 +17,7 @@ from voussoirkit import pathclass
from voussoirkit import ratelimiter
from voussoirkit import spinal
from voussoirkit import sqlhelpers
from voussoirkit import stringtools
from voussoirkit import vlogging
from . import constants
@ -1569,7 +1570,7 @@ class PDBUtilMixin:
same order that the files are listed when natural sorted. This is
essentially an aesthetic preference, that when you are viewing the
photos sorted by timestamp they are also natural sorted.
See helpers.natural_sorter.
See stringtools.natural_sorter.
new_photo_kwargs:
A dict of kwargs to pass into every call of new_photo.
@ -1747,7 +1748,7 @@ class PDBUtilMixin:
for (current_directory, subdirectories, files) in walk_generator:
if natural_sort:
files = sorted(files, key=lambda f: helpers.natural_sorter(f.basename))
files = sorted(files, key=lambda f: stringtools.natural_sorter(f.basename))
photos = [create_or_fetch_photo(file) for file in files]