-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: Python CLI Reader Bounding Box (#946)
Signed-off-by: Joey Kleingers <[email protected]>
- Loading branch information
1 parent
9c17e33
commit ba22f21
Showing
4 changed files
with
234 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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
59 changes: 59 additions & 0 deletions
59
wrapping/python/plugins/DataAnalysisToolkit/common/Result.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from typing import Generic, TypeVar, List | ||
import simplnx as nx | ||
|
||
T = TypeVar('T') | ||
|
||
class Result(Generic[T]): | ||
"""A class to represent a result that might be a value or an error.""" | ||
def __init__(self, value: T = None, errors: List[nx.Error] = None, warnings: List[nx.Warning] = None): | ||
if errors and value is not None: | ||
raise ValueError("Cannot create a Result with both errors and a value.") | ||
|
||
self.value = value | ||
self.errors = errors if errors else [] | ||
self.warnings = warnings if warnings else [] | ||
|
||
def valid(self) -> bool: | ||
"""Check if the Result is valid (i.e., has no errors).""" | ||
return not self.errors | ||
|
||
def invalid(self) -> bool: | ||
"""Check if the Result is invalid (i.e., has errors).""" | ||
return self.errors | ||
|
||
def make_error_result(code: int, message: str) -> Result: | ||
return Result(errors=[nx.Error(code, message)]) | ||
|
||
def make_warning_result(code: int, message: str) -> Result: | ||
return Result(warnings=[nx.Warning(code, message)]) | ||
|
||
def convert_result_to_void(result: Result) -> Result[None]: | ||
"""Convert a Result of any type to a Result of type None, preserving errors and warnings.""" | ||
return Result(None, result.errors, result.warnings) | ||
|
||
|
||
def merge_results(first: Result, second: Result) -> Result: | ||
"""Merge two Results into one, combining their errors and warnings.""" | ||
merged_errors = first.errors + second.errors | ||
merged_warnings = first.warnings + second.warnings | ||
# Assuming we're merging results without values; adjust as needed for your use case | ||
return Result(None, merged_errors, merged_warnings) | ||
|
||
|
||
# Example usage | ||
if __name__ == "__main__": | ||
# Create an error and a warning | ||
error = nx.Error(1, "An error occurred") | ||
warning = nx.Warning(1, "This is a warning") | ||
|
||
# Create a valid result and an invalid one | ||
valid_result = Result(value="Success") | ||
invalid_result = Result(errors=[error]) | ||
|
||
# Check if results are valid | ||
print("Valid result is valid:", valid_result.valid()) | ||
print("Invalid result is valid:", invalid_result.valid()) | ||
|
||
# Merge results | ||
merged_result = merge_results(valid_result, invalid_result) | ||
print("Merged result has", len(merged_result.errors), "errors and", len(merged_result.warnings), "warnings.") |
This file contains 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
Oops, something went wrong.