add setuptools files for example package mentioned in Issue 248#659
add setuptools files for example package mentioned in Issue 248#659sosey wants to merge 2 commits into
Conversation
sneakers-the-rat
left a comment
There was a problem hiding this comment.
looks good! with some minor comments and questions. i'm not sure of the context for this example or what the surrounding info is, so if my questions are covered by other material elsewhere already, feel free to ignore!
|
|
||
| def calc_annual_mean(df: pandas.DataFrame): | ||
| """Function to calculate the mean temperature for each year and the final mean""" | ||
| # TODO: make this a bit more robust so we can write integration test examples?? |
There was a problem hiding this comment.
| # TODO: make this a bit more robust so we can write integration test examples?? |
do we need this todo?
There was a problem hiding this comment.
hyphen in filename, wouldn't import, looks like a duplicate of temporal.py? can this be removed?
There was a problem hiding this comment.
I did this for some other example code and the hyphen was intentional. In sphinx we can use diff-file blocks to show, well diffs of how to modify code to fit best practice, but that involves having both files present in the repo. Also, we want these example files linted and tested to make sure they are high-quality, however the -raw files are often intentionally bad style.
Not sure if that is that is what is going on here, as I don't see any documentation references to this file.
There was a problem hiding this comment.
Ah, OK. Didn't know that, thanks for the context. I didn't diff them but they look the same? Maybe a leftover from another example or something.
There was a problem hiding this comment.
I tried to capture the purpose of examples/ in https://github.com/pyOpenSci/python-package-guide/blob/main/CONTRIBUTING.md#annex, although it looks like I failed to mention anything about diffing...
|
|
||
| def calc_annual_mean(df: pandas.DataFrame): | ||
| """Function to calculate the mean temperature for each year and the final mean""" | ||
| # TODO: make this a bit more robust so we can write integration test examples?? |
There was a problem hiding this comment.
| # TODO: make this a bit more robust so we can write integration test examples?? |
similarly, do we need this todo?
| import math | ||
|
|
||
| from examplePy import temperature | ||
|
|
||
|
|
||
| def test_fahrenheit_to_celsius_positive(): | ||
| """Test F to C calculation for positive values""" | ||
| value = 95 | ||
| expected_result = 35 | ||
| result = temperature.fahrenheit_to_celsius(value) | ||
|
|
||
| assert result == expected_result | ||
|
|
||
|
|
||
| def test_fahrenheit_to_celsius_negative(): | ||
| """Test F to C calculation for negative values""" | ||
| value = -13 | ||
| expected_result = -25 | ||
| result = temperature.fahrenheit_to_celsius(value) | ||
|
|
||
| assert result == expected_result | ||
|
|
||
|
|
||
| def test_fahrenheit_to_celsius_zero(): | ||
| """Test F to C calculation for zero""" | ||
| value = 0 | ||
| expected_result = -17.7778 | ||
| result = temperature.fahrenheit_to_celsius(value) | ||
|
|
||
| # Test that the result is close to the expected value, within tolerances | ||
| assert math.isclose(result, expected_result, abs_tol=0.0001) | ||
|
|
||
|
|
||
| def test_celsius_to_fahrenheit_positive(): | ||
| """Test C to F calculation for positive values""" | ||
| value = 100 | ||
| expected_result = 212 | ||
| result = temperature.celsius_to_fahrenheit(value) | ||
|
|
||
| assert result == expected_result | ||
|
|
||
|
|
||
| def test_celsius_to_fahrenheit_negative(): | ||
| """Test C to F calculation for negative values""" | ||
| value = -20 | ||
| expected_result = -4 | ||
| result = temperature.celsius_to_fahrenheit(value) | ||
|
|
||
| assert result == expected_result | ||
|
|
||
|
|
||
| def test_celsius_to_fahrenheit_zero(): | ||
| """Test C to F calculation for zero""" | ||
| value = 0 | ||
| expected_result = 32 | ||
| result = temperature.celsius_to_fahrenheit(value) | ||
|
|
||
| assert result == expected_result |
There was a problem hiding this comment.
| import math | |
| from examplePy import temperature | |
| def test_fahrenheit_to_celsius_positive(): | |
| """Test F to C calculation for positive values""" | |
| value = 95 | |
| expected_result = 35 | |
| result = temperature.fahrenheit_to_celsius(value) | |
| assert result == expected_result | |
| def test_fahrenheit_to_celsius_negative(): | |
| """Test F to C calculation for negative values""" | |
| value = -13 | |
| expected_result = -25 | |
| result = temperature.fahrenheit_to_celsius(value) | |
| assert result == expected_result | |
| def test_fahrenheit_to_celsius_zero(): | |
| """Test F to C calculation for zero""" | |
| value = 0 | |
| expected_result = -17.7778 | |
| result = temperature.fahrenheit_to_celsius(value) | |
| # Test that the result is close to the expected value, within tolerances | |
| assert math.isclose(result, expected_result, abs_tol=0.0001) | |
| def test_celsius_to_fahrenheit_positive(): | |
| """Test C to F calculation for positive values""" | |
| value = 100 | |
| expected_result = 212 | |
| result = temperature.celsius_to_fahrenheit(value) | |
| assert result == expected_result | |
| def test_celsius_to_fahrenheit_negative(): | |
| """Test C to F calculation for negative values""" | |
| value = -20 | |
| expected_result = -4 | |
| result = temperature.celsius_to_fahrenheit(value) | |
| assert result == expected_result | |
| def test_celsius_to_fahrenheit_zero(): | |
| """Test C to F calculation for zero""" | |
| value = 0 | |
| expected_result = 32 | |
| result = temperature.celsius_to_fahrenheit(value) | |
| assert result == expected_result | |
| import math | |
| import pytest | |
| from examplePy import temperature | |
| @pytest.mark.parametrize( | |
| "value,expected", | |
| [ | |
| pytest.param(95, 35, id="positive"), | |
| pytest.param(-13, -25, id="negative"), | |
| pytest.param(0, 17.7778, id="zero"), | |
| ] | |
| ) | |
| def test_fahrenheit_to_celsius(value, expected): | |
| """Test F to C calculation""" | |
| result = temperature.fahrenheit_to_celsius(value) | |
| assert math.isclose(result, expected, abs_tol=0.0001) | |
| @pytest.mark.parametrize( | |
| "value,expected", | |
| [ | |
| pytest.param(100, 212, id="positive"), | |
| pytest.param(-20, -4, id="negative"), | |
| pytest.param(0, 32, id="zero"), | |
| ] | |
| ) | |
| def test_celsius_to_fahrenheit(value, expected): | |
| """Test C to F calculation""" | |
| result = temperature.celsius_to_fahrenheit(value) | |
| assert math.isclose(result, expected, abs_tol=0.0001) |
just for the sake of demoing how one might write this in a real package/reducing duplication/etc. take or leave, not sure of the context/audience for this example.
There was a problem hiding this comment.
this is fine, but as an aside on more generally how we structure example tests in guides, i've seen one of the major learning hurdles for learning how to write tests be "how to avoid magic number tests that are actually just testing that some value in a file doesn't change rather than function correctness." super common pattern that people will just throw a huge end-to-end thing in a test function and assert that some value that comes out of it remains a hardcoded value in the test body, rather than something where a known input is coupled to an expected output, and then they struggle with how to add additional cases to protect against regressions/having to tiptoe around the test data file to avoid trivial changes that break e.g. string comparisons.
| include-package-data = false | ||
|
|
||
| [tool.setuptools_scm] | ||
| write_to = "src/examplePy/_version.py" |
There was a problem hiding this comment.
maybe some comment on how this is used? like e.g. this might be imported in __init__ to set a __version__ or something?
| zip-safe = true | ||
| include-package-data = false |
There was a problem hiding this comment.
comments on what these do, or links to docs?
sneakers-the-rat
left a comment
There was a problem hiding this comment.
sorry one more thing i am just sort of a big type annotations fan and think it's really good for newcomers to learn because they help you think through your code and let your IDE help you avoid bugs that can be very frustrating. also take or leave.
| @@ -0,0 +1,26 @@ | |||
| def celsius_to_fahrenheit(celsius): | |||
There was a problem hiding this comment.
| def celsius_to_fahrenheit(celsius): | |
| def celsius_to_fahrenheit(celsius: float) -> float: |
| return fahrenheit | ||
|
|
||
|
|
||
| def fahrenheit_to_celsius(fahrenheit): |
There was a problem hiding this comment.
| def fahrenheit_to_celsius(fahrenheit): | |
| def fahrenheit_to_celsius(fahrenheit: float) -> float: |
| from examplePy.temperature import fahrenheit_to_celsius | ||
|
|
||
|
|
||
| def calc_annual_mean(df: pandas.DataFrame): |
There was a problem hiding this comment.
| def calc_annual_mean(df: pandas.DataFrame): | |
| def calc_annual_mean(df: pandas.DataFrame) -> tuple[pandas.DataFrame, pandas.Series]: |
| from typing import Sequence | ||
|
|
There was a problem hiding this comment.
| from typing import Sequence |
appears unused?
ucodery
left a comment
There was a problem hiding this comment.
GitHub is having serious issues loading, well issues, right now so I can't read the original motivation for this issue. I did a general pass on it, but will probably come back to it next week too.
| zip-safe = true | ||
| include-package-data = false | ||
|
|
||
| [tool.setuptools_scm] |
There was a problem hiding this comment.
If this project wants to use setuptools-scm then that needs to be in the build-system.requires
| ] | ||
|
|
||
|
|
||
| [build-system] |
There was a problem hiding this comment.
Why did this section get moved down?
| version_scheme = "release-branch-semver" | ||
|
|
||
|
|
||
| [project.optional-dependencies] |
There was a problem hiding this comment.
I personally like and use optional-dependencies, but the new thing to do here is use [dependency-groups](https://packaging.python.org/en/latest/specifications/dependency-groups/) (not sure if you have a preference based on the tutorials @lwasser ?)
There was a problem hiding this comment.
I did this for some other example code and the hyphen was intentional. In sphinx we can use diff-file blocks to show, well diffs of how to modify code to fit best practice, but that involves having both files present in the repo. Also, we want these example files linted and tested to make sure they are high-quality, however the -raw files are often intentionally bad style.
Not sure if that is that is what is going on here, as I don't see any documentation references to this file.
| def calc_annual_mean(df: pandas.DataFrame): | ||
| """Function to calculate the mean temperature for each year and the final mean""" | ||
| # TODO: make this a bit more robust so we can write integration test examples?? | ||
| # Calculate the mean temperature for each year |
There was a problem hiding this comment.
Maybe this is okay for an example project. But I would block this as over-commented for a deployed library.
This adds example package files for setuptools, as mentioned in #248.
The examplePy package builds and tests pass with pytest.