Skip to content

Feat: Refactor ShipParams for Efficient Array Operations and Update Tests #51

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
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

pankaj-bind
Copy link
Contributor

This update addresses the issue of inefficient and repetitive array operations within the ShipParams class (in WeatherRoutingTool/ship/shipparams.py).

Problem:
The ShipParams class contained highly repetitive code in methods like define_courses, select, flip, expand_axis_for_intermediate, get_element, get_single_object, and get_reduced_2D_object. Each numerical attribute was individually handled for array manipulations, leading to verbose, error-prone, and difficult-to-maintain code.

Solution:
The ShipParams class has been refactored to introduce dedicated class attributes (_numeric_array_attributes and _non_numeric_array_attributes). These lists enable an iterative approach to array operations, replacing the redundant, individual attribute manipulations with a more generic and concise loop-based implementation. This significantly reduces code duplication.

Benefits:

  • Improved Maintainability: Easier to add or modify ship parameters without extensive changes across multiple methods.
  • Enhanced Readability: The code is more concise and the core logic is clearer.
  • Reduced Redundancy: Eliminates repetitive lines of code, making the codebase leaner and less prone to inconsistencies.
  • Reduced Risk of Bugs: Ensures consistent application of array operations to all relevant attributes.

Test Updates:
The tests/test_ship.py file has been updated to align with these changes.

  • The test_shipparams_get_element method's tuple unpacking has been adjusted to match the new order of attributes returned by ShipParams.get_element.
  • A minor syntax error in the message array initialization within both test_shipparams_get_element and test_shipparams_get_single has been corrected.

This refactoring improves the overall quality and robustness of the ShipParams class.

…ests

This update addresses the issue of inefficient and repetitive array operations within the `ShipParams` class (in `WeatherRoutingTool/ship/shipparams.py`).

**Problem:**
The `ShipParams` class contained highly repetitive code in methods like `define_courses`, `select`, `flip`, `expand_axis_for_intermediate`, `get_element`, `get_single_object`, and `get_reduced_2D_object`. Each numerical attribute was individually handled for array manipulations, leading to verbose, error-prone, and difficult-to-maintain code.

**Solution:**
The `ShipParams` class has been refactored to introduce dedicated class attributes (`_numeric_array_attributes` and `_non_numeric_array_attributes`). These lists enable an iterative approach to array operations, replacing the redundant, individual attribute manipulations with a more generic and concise loop-based implementation. This significantly reduces code duplication.

**Benefits:**
* **Improved Maintainability:** Easier to add or modify ship parameters without extensive changes across multiple methods.
* **Enhanced Readability:** The code is more concise and the core logic is clearer.
* **Reduced Redundancy:** Eliminates repetitive lines of code, making the codebase leaner and less prone to inconsistencies.
* **Reduced Risk of Bugs:** Ensures consistent application of array operations to all relevant attributes.

**Test Updates:**
The `tests/test_ship.py` file has been updated to align with these changes.
* The `test_shipparams_get_element` method's tuple unpacking has been adjusted to match the new order of attributes returned by `ShipParams.get_element`.
* A minor syntax error in the `message` array initialization within both `test_shipparams_get_element` and `test_shipparams_get_single` has been corrected.

This refactoring improves the overall quality and robustness of the `ShipParams` class.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant