From b74aea922181f747ad37ff739c6aa890a92a024f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 1 Apr 2026 15:57:34 +0000 Subject: [PATCH 1/2] Initial plan From 1d362e306462e112a7e08a89a5682e4c1f309215 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 1 Apr 2026 16:07:10 +0000 Subject: [PATCH 2/2] feat: add python1 tutorial package with function, tests and docs Agent-Logs-Url: https://github.com/sdpython/teachcompute/sessions/48eae982-2f73-49fa-b147-d328b1d14464 Co-authored-by: xadupre <22452781+xadupre@users.noreply.github.com> --- _tutoriels/python1/README.rst | 59 ++++++++++++++++++++++++ _tutoriels/python1/python1/__init__.py | 7 +++ _tutoriels/python1/python1/operations.py | 35 ++++++++++++++ _tutoriels/python1/setup.py | 10 ++++ _tutoriels/python1/test_python1.py | 41 ++++++++++++++++ 5 files changed, 152 insertions(+) create mode 100644 _tutoriels/python1/README.rst create mode 100644 _tutoriels/python1/python1/__init__.py create mode 100644 _tutoriels/python1/python1/operations.py create mode 100644 _tutoriels/python1/setup.py create mode 100644 _tutoriels/python1/test_python1.py diff --git a/_tutoriels/python1/README.rst b/_tutoriels/python1/README.rst new file mode 100644 index 0000000..c025f0a --- /dev/null +++ b/_tutoriels/python1/README.rst @@ -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] diff --git a/_tutoriels/python1/python1/__init__.py b/_tutoriels/python1/python1/__init__.py new file mode 100644 index 0000000..2f72473 --- /dev/null +++ b/_tutoriels/python1/python1/__init__.py @@ -0,0 +1,7 @@ +""" +Package python1 — exemples de fonctions Python pures avec documentation. +""" + +from .operations import running_mean + +__all__ = ["running_mean"] diff --git a/_tutoriels/python1/python1/operations.py b/_tutoriels/python1/python1/operations.py new file mode 100644 index 0000000..73e7bc5 --- /dev/null +++ b/_tutoriels/python1/python1/operations.py @@ -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 diff --git a/_tutoriels/python1/setup.py b/_tutoriels/python1/setup.py new file mode 100644 index 0000000..dc6c1bb --- /dev/null +++ b/_tutoriels/python1/setup.py @@ -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", +) diff --git a/_tutoriels/python1/test_python1.py b/_tutoriels/python1/test_python1.py new file mode 100644 index 0000000..a5812dd --- /dev/null +++ b/_tutoriels/python1/test_python1.py @@ -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)