Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try/except instead of requiring version kwarg #305

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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."
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. You could also avoid the if block by having a list of the functions and using the version as the index ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about new version?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, although I was thinking of a list, but a dict works too.

Loading