Skip to content

Commit 871bebf

Browse files
author
Jan Sebastian Rothe
committed
Some improvements were mistakenly removed
1 parent e893791 commit 871bebf

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/fagfunksjoner/paths/versions.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def get_file_name(filepath: str) -> str:
6868
return base_file_name
6969

7070

71-
def get_latest_fileversions(glob_list_path: list[str]) -> list[str]:
71+
def get_latest_fileversions(glob_list_path: list[str] | str) -> list[str]:
7272
"""Receives a list of filenames with multiple versions and returns the latest versions of the files.
7373
7474
Recommend using glob operation to create the input list.
@@ -77,28 +77,38 @@ def get_latest_fileversions(glob_list_path: list[str]) -> list[str]:
7777
- Locally: https://docs.python.org/3/library/glob.html
7878
7979
Args:
80-
glob_list_path: List of strings that represents a filepath.
80+
glob_list_path: List of strings or single string that represents a filepath.
8181
Recommend that the list is created with glob operation.
8282
8383
Returns:
8484
list[str]: List of strings with unique filepaths and their latest versions.
8585
86+
Raises:
87+
TypeError: If parameter does not fit with type-narrowing to list of strings.
88+
8689
Example::
8790
8891
import dapla as dp
8992
fs = dp.FileClient.get_gcs_file_system()
9093
all_files = fs.glob("gs://dir/statdata_v*.parquet")
9194
latest_files = get_latest_fileversions(all_files)
9295
"""
96+
if isinstance(glob_list_path, str):
97+
infiles = [glob_list_path]
98+
elif isinstance(glob_list_path, list):
99+
infiles = glob_list_path
100+
else:
101+
raise TypeError("Expecting glob_list_path to be a str or a list of str.")
102+
93103
# Extract unique base names by splitting before the version part
94-
uniques = list(dict.fromkeys([file.rsplit("_v", 1)[0] for file in glob_list_path]))
104+
uniques = list(dict.fromkeys([file.rsplit("_v", 1)[0] for file in infiles]))
95105
result = []
96106

97107
for unique in uniques:
98108
# Collect all entries that match the current unique base name
99109
entries = [
100110
x
101-
for x in glob_list_path
111+
for x in infiles
102112
if x.startswith(unique + "_v")
103113
and x.rsplit(".", 1)[0][len(unique + "_v") :].isdigit()
104114
] # Characters after match is only digits

0 commit comments

Comments
 (0)