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

Reduces code complexity in one part of weather_symbols.py. #1391

Merged
merged 6 commits into from
Dec 17, 2020

Conversation

MoseleyS
Copy link
Contributor

@MoseleyS MoseleyS commented Dec 11, 2020

In an attempt to apply some Python training, I've refactored the "invert_condition" function in WeatherSymbols to remove an if..elif..elif..elif block. The code will no longer allow unrecognised comparators rather than returning them unchanged.

Testing:

  • Ran tests and they passed OK

@MoseleyS MoseleyS self-assigned this Dec 11, 2020
@codecov
Copy link

codecov bot commented Dec 11, 2020

Codecov Report

Merging #1391 (6b7e5c8) into master (8da46d9) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1391      +/-   ##
==========================================
- Coverage   96.34%   96.34%   -0.01%     
==========================================
  Files          89       89              
  Lines        7579     7578       -1     
==========================================
- Hits         7302     7301       -1     
  Misses        277      277              
Impacted Files Coverage Δ
improver/wxcode/weather_symbols.py 98.47% <100.00%> (-0.01%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8da46d9...6b7e5c8. Read the comment docs.

@MoseleyS MoseleyS requested a review from cgsandford December 11, 2020 09:24
">=": "<",
">": "<=",
"OR": "AND",
"": "",
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The simpler conditions don't have a comparator (AND and OR), so we need the ability to return an empty string too. This could be an if test in the calling function instead.

"""Test that the _invert_comparator method raises an error when the condition
cannot be inverted."""
plugin = WeatherSymbols()
possible_inputs = ["==", "!=", "NOT", "XOR"]
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm - but you could have any of these conditions in your dictionary. (Although the test is valid.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My thinking is that if we add any of these to that dictionary, then this test will force us to update the unit-tests. Codecov does not check that every entry in a dictionary is touched, only that every line of code is touched.

Copy link
Contributor

@cgsandford cgsandford left a comment

Choose a reason for hiding this comment

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

This looks like a nicer way of doing this, using a dictionary rather than lots of conditions, and tidies up the call somewhat.

@MoseleyS MoseleyS marked this pull request as ready for review December 11, 2020 09:40
@tjtg tjtg added the good first review This pull request is simpler to review label Dec 15, 2020
Copy link
Contributor

@Katie-Howard Katie-Howard left a comment

Choose a reason for hiding this comment

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

I've looked through these changes and they all look good to me! The tests all run through fine as well.

@MoseleyS MoseleyS merged commit 569ce9d into metoppv:master Dec 17, 2020
@MoseleyS MoseleyS deleted the small_complexity_reduction branch December 17, 2020 08:16
MoseleyS added a commit to MoseleyS/improver that referenced this pull request Aug 22, 2024
)

* Reduces code complexity in one part of weather_symbols.py.

* Refactors the dict definition so it is only performed once.

* Adds unit test for new error message.

* Removes == from list of invertible conditions, as it was wrong and isn't used anyway.

* Proves == raises the error too

* Improves doc-string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first review This pull request is simpler to review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants