Repository navigation
Add pure Python package tutorial in _tutoriels/python1 #31
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| python1 — package Python pur | ||
| ============================== | ||
|
|
||
| Ce tutoriel montre comment structurer un **package Python pur** avec : | ||
|
|
||
| * une fonction documentée, | ||
| * des tests unitaires. | ||
|
|
||
| Structure | ||
| --------- | ||
|
|
||
| .. code-block:: text | ||
|
|
||
| python1/ | ||
| ├── python1/ # le package | ||
| │ ├── __init__.py | ||
| │ └── operations.py | ||
| ├── test_python1.py # tests unitaires | ||
| └── setup.py | ||
|
|
||
| Installation | ||
| ------------ | ||
|
|
||
| Aucune compilation nécessaire (package Python pur) : | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| pip install -e . | ||
|
|
||
| Utilisation | ||
| ----------- | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| from python1 import running_mean | ||
|
|
||
| data = [1, 2, 3, 4, 5] | ||
| print(running_mean(data)) | ||
| # [1.0, 1.5, 2.0, 2.5, 3.0] | ||
|
|
||
| Lancer les tests | ||
| ---------------- | ||
|
|
||
| .. code-block:: bash | ||
|
|
||
| python -m unittest test_python1 -v | ||
|
|
||
| Fonction disponible | ||
| ------------------- | ||
|
|
||
| ``running_mean(data)`` | ||
| Calcule la moyenne cumulée d'une séquence de nombres. | ||
| Retourne une liste de flottants de même longueur que *data*. | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| >>> from python1.operations import running_mean | ||
| >>> running_mean([1, 2, 3, 4]) | ||
| [1.0, 1.5, 2.0, 2.5] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| """ | ||
| Package python1 — exemples de fonctions Python pures avec documentation. | ||
| """ | ||
|
|
||
| from .operations import running_mean | ||
|
|
||
| __all__ = ["running_mean"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| """ | ||
| Fonctions de calcul numérique en Python pur. | ||
| """ | ||
|
|
||
| from typing import List, Union | ||
|
|
||
| Number = Union[int, float] | ||
|
|
||
|
|
||
| def running_mean(data: List[Number]) -> List[float]: | ||
| """ | ||
| Calcule la moyenne cumulée d'une séquence de nombres. | ||
|
|
||
| Pour chaque position ``i``, la valeur retournée est la moyenne | ||
| des éléments ``data[0], data[1], ..., data[i]``. | ||
|
|
||
| :param data: séquence de nombres (liste d'entiers ou de flottants). | ||
| :return: liste de flottants de même longueur que *data* contenant | ||
| les moyennes cumulées. | ||
| :raises ValueError: si *data* est vide. | ||
|
|
||
| Exemple:: | ||
|
|
||
| >>> from python1.operations import running_mean | ||
| >>> running_mean([1, 2, 3, 4]) | ||
| [1.0, 1.5, 2.0, 2.5] | ||
| """ | ||
| if len(data) == 0: | ||
| raise ValueError("data ne doit pas être vide.") | ||
| result: List[float] = [] | ||
| total = 0.0 | ||
| for i, value in enumerate(data): | ||
| total += value | ||
| result.append(total / (i + 1)) | ||
| return result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| from setuptools import setup, find_packages | ||
|
|
||
| setup( | ||
| name="python1", | ||
| version="0.1", | ||
| author="Xavier Dupré", | ||
| description="Exemple de package Python pur avec documentation et tests", | ||
| packages=find_packages(), | ||
| python_requires=">=3.9", | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import unittest | ||
| from python1.operations import running_mean | ||
|
|
||
|
|
||
| class TestRunningMean(unittest.TestCase): | ||
| def test_single_element(self): | ||
| self.assertEqual(running_mean([5]), [5.0]) | ||
|
|
||
| def test_integers(self): | ||
| result = running_mean([1, 2, 3, 4]) | ||
| self.assertEqual(result, [1.0, 1.5, 2.0, 2.5]) | ||
|
|
||
| def test_floats(self): | ||
| result = running_mean([0.0, 1.0, 2.0]) | ||
| self.assertAlmostEqual(result[0], 0.0) | ||
| self.assertAlmostEqual(result[1], 0.5) | ||
| self.assertAlmostEqual(result[2], 1.0) | ||
|
|
||
| def test_negative_values(self): | ||
| result = running_mean([-1, -2, -3]) | ||
| self.assertAlmostEqual(result[0], -1.0) | ||
| self.assertAlmostEqual(result[1], -1.5) | ||
| self.assertAlmostEqual(result[2], -2.0) | ||
|
|
||
| def test_length_preserved(self): | ||
| data = list(range(10)) | ||
| self.assertEqual(len(running_mean(data)), len(data)) | ||
|
|
||
| def test_empty_raises(self): | ||
| with self.assertRaises(ValueError): | ||
| running_mean([]) | ||
|
|
||
| def test_last_value_is_mean(self): | ||
| data = [1, 3, 5, 7, 9] | ||
| result = running_mean(data) | ||
| expected_last = sum(data) / len(data) | ||
| self.assertAlmostEqual(result[-1], expected_last) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main(verbosity=2) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot renomme python1 autrechose