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

refactor: fha calculations #1414

Merged
merged 5 commits into from
Oct 12, 2024
Merged

refactor: fha calculations #1414

merged 5 commits into from
Oct 12, 2024

Conversation

weibullguy
Copy link
Collaborator

@weibullguy weibullguy commented Oct 11, 2024

Does this PR introduce a breaking change?

  • Yes
  • No

Describe the purpose of this pull request.

Removed redundant key assignments in the set_user_defined* 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

    • Code is following code style guidelines.
  • Static Checks

    • Failing static checks are only applicable to code outside the scope of
      this PR.
  • Tests

    • At least one test for all newly created functions/methods?
  • Chores

    • Issue(s) have been raised for problem areas outside the scope of
      this PR. These problem areas have been decorated with an ISSUE: # comment.

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:

  • Refactor the calculate_user_defined function to use loops instead of repetitive code, improving code conciseness and maintainability.
  • Improve error handling in the calculate_hri function by raising a more descriptive OutOfRangeError for unknown hazard probabilities or severities.
  • Introduce a validation mechanism for equations in user-defined functions to ensure they contain only valid variables.
  • Simplify the set_user_defined_* functions by removing redundant key assignments and directly using list indices.

Documentation:

  • Update all docstrings to follow the Sphinx format, providing proper descriptions for parameters and return types.

Tests:

  • Add tests to verify the default behavior of empty equations in calculate_user_defined.
  • Add tests to ensure set_user_defined_floats and set_user_defined_ints handle empty lists and single values correctly.
  • Add tests to validate error handling for invalid equations in user-defined functions.

Copy link
Contributor

sourcery-ai bot commented Oct 11, 2024

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"
Loading

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_user_defined* 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 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

@github-actions github-actions bot added the type: refactor Issue or PR dealing with refactoring of RAMSTK code. label Oct 11, 2024
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @weibullguy - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider the implications of changing from KeyError to OutOfRangeError. This might affect existing error handling code elsewhere in the project.
  • When using sympify with user input, add a comment about potential security implications and ensure proper input validation is in place.
Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

src/ramstk/analyses/fha.py Outdated Show resolved Hide resolved
src/ramstk/analyses/fha.py Outdated Show resolved Hide resolved
src/ramstk/analyses/fha.py Show resolved Hide resolved
tests/analyses/test_fha.py Show resolved Hide resolved
src/ramstk/analyses/fha.py Outdated Show resolved Hide resolved
@weibullguy weibullguy added priority: low Issue or PR is low priority. status: inprogress Issue or PR is open, milestoned, and assigned. type: style Issue or PR affects code quality (e.g., coding standards). and removed type: style Issue or PR affects code quality (e.g., coding standards). labels Oct 12, 2024
@weibullguy weibullguy merged commit f503600 into master Oct 12, 2024
18 of 19 checks passed
@weibullguy weibullguy deleted the refactor/fha branch October 12, 2024 16:43
@trafico-bot trafico-bot bot added the endgame: merged Pull Request has been merged successfully label Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
endgame: merged Pull Request has been merged successfully priority: low Issue or PR is low priority. status: inprogress Issue or PR is open, milestoned, and assigned. type: refactor Issue or PR dealing with refactoring of RAMSTK code.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant