Skip to content

Commit

Permalink
feat: allow files to be sorted by year if a named regex is used
Browse files Browse the repository at this point in the history
  • Loading branch information
pgierz committed Jul 29, 2024
1 parent 0779840 commit 4745539
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/pymorize/gather_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,10 @@ def resolve_symlinks(files: List[pathlib.Path]) -> List[pathlib.Path]:
if not all(isinstance(f, pathlib.Path) for f in files):
raise TypeError("All files must be pathlib.Path objects")
return [f.resolve() if f.is_symlink() else f for f in files]


def sort_by_year(files: List[pathlib.Path], fpattern: re.Pattern) -> List[pathlib.Path]:
"""
Sorts a list of files by the year in their name.
"""
return sorted(files, key=lambda f: int(fpattern.match(f.name).group("year")))
33 changes: 32 additions & 1 deletion tests/test_find_eligible_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from pyfakefs.fake_pathlib import FakePath

from pymorize.gather_inputs import (_input_pattern_from_env,
input_files_in_path, resolve_symlinks)
input_files_in_path, resolve_symlinks,
sort_by_year)

# monkeypatch Path.__eq__ so that pyfakefs FakePaths compare equal to real pathlib.Paths
#
Expand Down Expand Up @@ -229,3 +230,33 @@ def test_resolve_symlinks(fake_filesystem_with_symlinks):
def test_resolve_symlinks_raises_type_error():
with pytest.raises(TypeError):
resolve_symlinks(["not", "paths"])


@pytest.fixture
def fake_filesystem_with_datestamps_years():
with Patcher() as patcher:
fpattern = re.compile(r".*(?P<year>\d{4}).*")
files = [
pathlib.Path(f"/path/to/file_{year}.txt") for year in range(2000, 2010)
]
for file in files:
patcher.fs.create_file(file)
yield patcher


def test_sort_by_year(fake_filesystem_with_datestamps_years):
# Arrange
files = [Path(f"/path/to/file_{year}.txt") for year in range(2000, 2010)]
# Shuffle the list of files
import random

random.shuffle(files)
fpattern = re.compile(r".*(?P<year>\d{4}).*")

# Act
sorted_files = sort_by_year(files, fpattern)

# Assert
assert sorted_files == [
Path(f"/path/to/file_{year}.txt") for year in range(2000, 2010)
]

0 comments on commit 4745539

Please sign in to comment.