Skip to content

Commit

Permalink
Merge pull request #305 from swelborn/remove-version-kwarg
Browse files Browse the repository at this point in the history
try/except instead of requiring version kwarg
  • Loading branch information
swelborn authored Nov 30, 2023
2 parents c4c7bdb + f7e01fb commit f4eb7d2
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions python/stempy/contrib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ def get_scan_path(
scan_num: Optional[int] = None,
scan_id: Optional[int] = None,
th: Optional[int] = None,
version: int = 0,
file_suffix: FileSuffix = FileSuffix.STANDARD,
version: Optional[int] = None,
) -> Tuple[Path, Optional[int], Optional[int]]:
"""Get the file path for a 4D Camera scan on NERSC using the scan number,
the Distiller scan id, and/or threshold. scan_id should always
Expand All @@ -169,26 +169,39 @@ def get_scan_path(
th : float, optional
The threshold for counting. This was added to the filename in older files.
version : int, optional
Version number for file name. 0 -- data_scan... ; 1 -- FOURD_...
Version number for file name. If None, tries version 1 then 0.
Returns
-------
: tuple
The tuple contains the file that matches the input information and the
scan_num and scan_id as a tuple.
"""
if version == 0:
return get_scan_path_version_0(

versions_to_try = [1, 0] if version is None else [version]
version_functions = {
0: lambda: get_scan_path_version_0(
directory,
scan_num=scan_num,
scan_id=scan_id,
th=th,
file_suffix=file_suffix,
)
elif version == 1:
return get_scan_path_version_1(
directory, scan_num, scan_id, file_suffix=file_suffix
)

else:
raise NotImplementedError("Please enter version 0 or 1.")
),
1: lambda: get_scan_path_version_1(
directory, scan_num=scan_num, scan_id=scan_id, file_suffix=file_suffix
),
}

for ver in versions_to_try:
try:
if ver in version_functions:
return version_functions[ver]()
else:
raise NotImplementedError("Version 0 and 1 are implemented.")
except FileNotFoundError:
continue

raise FileNotFoundError(
"No file with those parameters can be found for any version."
)

0 comments on commit f4eb7d2

Please sign in to comment.