-
Notifications
You must be signed in to change notification settings - Fork 11
feat: allow file paths to be passed in as pathlike objects #198
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
Open
kimboleh
wants to merge
3
commits into
tektronix:main
Choose a base branch
from
kimboleh:feature/accept-pathlike-objects
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| """Factory methods to read and write to a generic file using generic data.""" | ||
|
|
||
| import multiprocessing | ||
| import os | ||
|
|
||
| from pathlib import Path | ||
| from typing import List, Optional, TYPE_CHECKING | ||
|
|
||
| from typing_extensions import TypeVar | ||
|
|
@@ -27,7 +27,7 @@ | |
|
|
||
| # pylint: disable=unused-argument | ||
| def write_file( | ||
| path: str, | ||
| file_path: str | Path, | ||
| waveform: Datum, | ||
| product: InstrumentSeries = InstrumentSeries.TEKSCOPE, | ||
| file_format: Optional[CSVFormats] = None, # noqa: ARG001 | ||
|
|
@@ -50,26 +50,29 @@ def write_file( | |
| stored separately. | ||
|
|
||
| Args: | ||
| path: The path file to write to. | ||
| file_path: The path file to write to. | ||
| waveform: The waveform that is being written. | ||
| product: The product being written to. | ||
| file_format: A specialized file format we are writing as. | ||
| """ | ||
| _, path_extension = os.path.splitext(path) | ||
| file_path = Path(file_path) if isinstance(file_path, str) else file_path | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You actually don't need the if/else, just call |
||
|
|
||
| # make sure the extension is supported | ||
| try: | ||
| file_extension = FileExtensions[path_extension.replace(".", "").upper()] | ||
| file_extension = FileExtensions[file_path.suffix.lstrip(".").upper()] | ||
| except KeyError as e: | ||
| msg = f"The {path_extension} extension cannot be written to." | ||
| msg = f"The {file_path.suffix} extension cannot be written to." | ||
| raise IOError(msg) from e | ||
|
|
||
| # find the format based on the waveform extension | ||
| format_class: AbstractedFile = find_class_format(file_extension, type(waveform)) | ||
| # using __init__ for instantiation due to pyright confusion | ||
| format_class = format_class(path, access_type(file_extension, write=True), product) | ||
| format_class = format_class(file_path, access_type(file_extension, write=True), product) | ||
| with format_class as fd: | ||
| fd.write_datum(waveform) | ||
|
|
||
|
|
||
| def read_file(file_path: str) -> DatumAlias: | ||
| def read_file(file_path: str | Path) -> DatumAlias: | ||
| """Read a waveform from a provided file. | ||
|
|
||
| Process Overview: | ||
|
|
@@ -89,12 +92,15 @@ def read_file(file_path: str) -> DatumAlias: | |
| Args: | ||
| file_path: The path file to read from. | ||
| """ | ||
| _, path_extension = os.path.splitext(file_path) | ||
| file_path = Path(file_path) if isinstance(file_path, str) else file_path | ||
|
|
||
| # ensure the extension is supported | ||
| try: | ||
| file_extension = FileExtensions[path_extension.replace(".", "").upper()] | ||
| file_extension = FileExtensions[file_path.suffix.lstrip(".").upper()] | ||
| except KeyError as e: | ||
| msg = f"The {path_extension} extension cannot be read from." | ||
| msg = f"The {file_path.suffix} extension cannot be read from." | ||
| raise IOError(msg) from e | ||
|
|
||
| class_formats: List[AbstractedFile] = find_class_format_list(file_extension) | ||
| for file_format in class_formats: | ||
| with file_format(file_path, access_type(file_extension, write=False)) as fd: | ||
|
|
@@ -104,7 +110,7 @@ def read_file(file_path: str) -> DatumAlias: | |
| raise TypeError(msg) | ||
|
|
||
|
|
||
| def read_analog_file(file_path: str) -> AnalogWaveform: | ||
| def read_analog_file(file_path: str | Path) -> AnalogWaveform: | ||
| """Read a waveform from a provided file, type hinted as an AnalogWaveform. | ||
|
|
||
| Args: | ||
|
|
@@ -113,7 +119,7 @@ def read_analog_file(file_path: str) -> AnalogWaveform: | |
| return read_file(file_path) | ||
|
|
||
|
|
||
| def read_iq_file(file_path: str) -> IQWaveform: | ||
| def read_iq_file(file_path: str | Path) -> IQWaveform: | ||
| """Read a waveform from a provided file, type hinted as an IQWaveform. | ||
|
|
||
| Args: | ||
|
|
@@ -131,7 +137,7 @@ def _write_files( | |
| """Write a list of waveforms to a list of provided files. | ||
|
|
||
| Args: | ||
| file_paths: The path file to write to. | ||
| file_paths: The list of path files to write to. | ||
| datums: The datum that is being written. | ||
| product: The product being written to. | ||
| file_format: A specialized file format we are writing as. | ||
|
|
@@ -141,7 +147,7 @@ def _write_files( | |
|
|
||
|
|
||
| def write_files_in_parallel( | ||
| file_paths: List[str], | ||
| file_paths: List[str] | List[Path], | ||
| datums: List[Datum], | ||
| force_process_count: int = 4, | ||
| product: InstrumentSeries = InstrumentSeries.TEKSCOPE, | ||
|
|
@@ -160,7 +166,7 @@ def write_files_in_parallel( | |
| This method is particularly useful for saving multiple waveform files efficiently. | ||
|
|
||
| Args: | ||
| file_paths: The path file to write to. | ||
| file_paths: The list of path files to write to. | ||
| datums: The datum that is being written. | ||
| force_process_count: The number of processes that will be created. | ||
| product: The product being written to. | ||
|
|
@@ -207,7 +213,9 @@ def _read_files(file_paths: str, file_queue: multiprocessing.Queue) -> None: | |
| file_queue.put((file_path, read_file(file_path))) | ||
|
|
||
|
|
||
| def read_files_in_parallel(file_paths: List[str], force_process_count: int = 4) -> List[Datum]: | ||
| def read_files_in_parallel( | ||
| file_paths: List[str] | List[Path], force_process_count: int = 4 | ||
| ) -> List[Datum]: | ||
| """Read a list of files in parallel. | ||
|
|
||
| This method allows for the parallel reading of multiple waveform files. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.