ReliaQualAssociates / ramstk

Reliability, Availability, Maintainability, Safety (RAMS) analysis program.
https://www.reliaqual.com/rtk/
BSD 3-Clause "New" or "Revised" License
46 stars 15 forks source link

refactor: fha calculations #1414

Closed weibullguy closed 1 month ago

weibullguy commented 1 month ago

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

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:

Documentation:

Tests:

sourcery-ai[bot] commented 1 month ago

Reviewer's Guide by Sourcery

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.

Class diagram for refactored FHA calculations

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"

File-Level Changes

Change Details Files
Refactored redundant code in FHA calculations
  • Replaced repetitive code with loops in calculate_user_defined function
  • Removed redundant _key assignments in set_userdefined* functions
  • Implemented a loop to handle equation calculations in calculate_user_defined
  • Added validation for equations in calculate_user_defined
src/ramstk/analyses/fha.py
Improved error handling and messages
  • Changed KeyError handling to raise a more descriptive OutOfRangeError in calculate_hri function
  • Added try-except blocks to handle out-of-range index access, defaulting to 0.0 or 0 when necessary
  • Implemented equation validation in _do_validate_equation function
src/ramstk/analyses/fha.py
Updated documentation and type hints
  • Updated all docstrings to follow the Sphinx format
  • Added proper descriptions for parameters and return types in docstrings
  • Applied Dict[str, Any] and List as types for dictionary and list arguments where appropriate
src/ramstk/analyses/fha.py
Modified and added unit tests
  • Updated existing tests to accommodate refactored code
  • Added new tests for empty equations, invalid equations, and edge cases
  • Implemented pytest.approx() for floating-point comparisons in tests
tests/analyses/test_fha.py
tests/models/programdb/hazard/hazard_unit_test.py

Tips and commands #### Interacting with Sourcery - **Trigger a new review:** Comment `@sourcery-ai review` on the pull request. - **Continue discussions:** Reply directly to Sourcery's review comments. - **Generate a GitHub issue from a review comment:** Ask Sourcery to create an issue from a review comment by replying to it. - **Generate a pull request title:** Write `@sourcery-ai` anywhere in the pull request title to generate a title at any time. - **Generate a pull request summary:** Write `@sourcery-ai summary` anywhere in the pull request body to generate a PR summary at any time. You can also use this command to specify where the summary should be inserted. #### Customizing Your Experience Access your [dashboard](https://app.sourcery.ai) to: - Enable or disable review features such as the Sourcery-generated pull request summary, the reviewer's guide, and others. - Change the review language. - Add, remove or edit custom review instructions. - Adjust other review settings. #### Getting Help - [Contact our support team](mailto:support@sourcery.ai) for questions or feedback. - Visit our [documentation](https://docs.sourcery.ai) for detailed guides and information. - Keep in touch with the Sourcery team by following us on [X/Twitter](https://x.com/SourceryAI), [LinkedIn](https://www.linkedin.com/company/sourcery-ai/) or [GitHub](https://github.com/sourcery-ai).