Closed weibullguy closed 1 month ago
This pull request refactors the FHA (Functional Hazard Assessment) calculations in the RAMSTK project. The changes focus on improving code efficiency, readability, and error handling. Key modifications include removing redundant code, implementing loops for repetitive tasks, enhancing error messages, updating docstrings to follow Sphinx format, and adding type hints.
classDiagram
class FHA {
+Dict[str, Any] calculate_user_defined(Dict[str, Any] fha)
+Dict[str, Any] set_user_defined_floats(Dict[str, Any] fha, List[float] floats)
+Dict[str, Any] set_user_defined_ints(Dict[str, Any] fha, List[int] ints)
+Dict[str, Any] set_user_defined_functions(Dict[str, Any] fha, List[str] functions)
+Dict[str, Any] set_user_defined_results(Dict[str, Any] fha, List[float] results)
+int calculate_hri(str probability, str severity)
+void _do_validate_equation(str equation)
}
note for FHA "Refactored to use loops and improved error handling"
Change | Details | Files |
---|---|---|
Refactored redundant code in FHA calculations |
|
src/ramstk/analyses/fha.py |
Improved error handling and messages |
|
src/ramstk/analyses/fha.py |
Updated documentation and type hints |
|
src/ramstk/analyses/fha.py |
Modified and added unit tests |
|
tests/analyses/test_fha.py tests/models/programdb/hazard/hazard_unit_test.py |
Does this PR introduce a breaking change?
Describe the purpose of this pull request.
Removed redundant _key assignments in the set_userdefined* functions by directly using list(fha.keys()). Used loops instead of repetitive code to make the calculate_user_defined function and others more concise.
In the calculate_hri function, I changed the KeyError handling to raise a more descriptive OutOfRangeError. Safeguards against out-of-range index access are handled with the try-except mechanism, defaulting values to 0.0 or 0 when necessary.
Updated all docstrings to follow the Sphinx format with proper descriptions for parameters and return types. Added meaningful descriptions for each function and its arguments.
Applied Dict[str, Any] and List as the types for dictionary and list arguments where appropriate.
Describe how this was implemented.
Ran code through ChatGPT.
Describe any particular area(s) reviewers should focus on.
None
Provide any other pertinent information.
Pull Request Checklist
Code Style
Static Checks
Tests
Chores
Summary by Sourcery
Refactor FHA calculations to improve code conciseness and error handling. Enhance validation for user-defined equations and update docstrings to Sphinx format. Add comprehensive tests to cover new behaviors and error scenarios.
Enhancements:
calculate_user_defined
function to use loops instead of repetitive code, improving code conciseness and maintainability.calculate_hri
function by raising a more descriptiveOutOfRangeError
for unknown hazard probabilities or severities.set_user_defined_*
functions by removing redundant key assignments and directly using list indices.Documentation:
Tests:
calculate_user_defined
.set_user_defined_floats
andset_user_defined_ints
handle empty lists and single values correctly.