records = 103
batch_size = 20
full_batches, remaining = divmod(records, batch_size)
assert (full_batches, remaining) == (5, 3)
assert -7 // 3 == -3
assert (-7 // 3) * 3 + (-7 % 3) == -7
print(full_batches, remaining)


from decimal import Decimal

assert 10 / 5 == 2.0 and isinstance(10 / 5, float)
assert (10 // 3, 10 % 3) == (3, 1)
assert -7 // 3 == -3 and int(-7 / 3) == -2
assert Decimal("0.1") + Decimal("0.2") == Decimal("0.3")
assert 0.1 + 0.2 != 0.3
from decimal import localcontext
with localcontext() as contexte:
    contexte.prec = 4
    assert Decimal("1") / Decimal("3") == Decimal("0.3333")


try:
    compile("total des recus", "<console>", "exec")
except SyntaxError as erreur:
    assert erreur.lineno == 1 and erreur.filename == "<console>"
    assert erreur.text.strip() == "total des recus" and erreur.offset > 0
else:
    raise AssertionError("La phrase libre n'est pas une instruction Python")
valeur_decimale = 9.5
valeur_avec_virgule = 9, 5
assert isinstance(valeur_decimale, float) and valeur_decimale == 9.5
assert isinstance(valeur_avec_virgule, tuple) and valeur_avec_virgule == (9, 5)
assert 12 + 5 == 17 and 40 - 7 == 33 and 6 * 4 == 24


import math

somme_mesures = 3.11 + 2.08
assert math.isclose(somme_mesures, 5.19, rel_tol=1e-12)
assert (10 // 3) * 3 + (10 % 3) == 10
try:
    10 / 0
except ZeroDivisionError:
    pass
else:
    raise AssertionError("Une division par zéro doit échouer")

assert math.isclose(0.0, 0.000001, abs_tol=0.00001)
assert not math.isclose(0.0, 0.0001, abs_tol=0.00001)

assert 7 // -3 == -3
assert 7 % -3 == -2
assert (-3 * -3) + (-2) == 7
nombre_partitions = 3
assert nombre_partitions > 0
assert 7 % nombre_partitions == 1

import code
from contextlib import redirect_stderr, redirect_stdout
from io import StringIO

console_demo = code.InteractiveConsole()
sortie_console, erreurs_console = StringIO(), StringIO()
with redirect_stdout(sortie_console), redirect_stderr(erreurs_console):
    console_demo.push("lignes_recues = 120")
    assert sortie_console.getvalue() == ""
    console_demo.push("lignes_recues")
    console_demo.push("total des recus")
    console_demo.push("lignes_recues + 5")
assert sortie_console.getvalue().splitlines() == ["120", "125"]
assert "SyntaxError" in erreurs_console.getvalue()

def nombre_de_lots(nombre_lignes, taille_lot):
    if type(nombre_lignes) is not int or type(taille_lot) is not int:
        raise TypeError("Les deux paramètres doivent être des entiers")
    if nombre_lignes < 0 or taille_lot <= 0:
        raise ValueError("Nombre de lignes ou taille de lot invalide")
    complets, reste = divmod(nombre_lignes, taille_lot)
    return complets + (1 if reste else 0)

assert nombre_de_lots(103, 20) == 6
assert nombre_de_lots(100, 20) == 5
assert nombre_de_lots(0, 20) == 0
for lignes, taille in ((-1, 20), (10, 0)):
    try:
        nombre_de_lots(lignes, taille)
    except ValueError:
        pass
    else:
        raise AssertionError("Paramètres invalides acceptés")

from decimal import Decimal

quotient_entier = 10 / 4
quotient_decimal = Decimal("10") / Decimal("4")
assert isinstance(quotient_entier, float) and quotient_entier == 2.5
assert isinstance(quotient_decimal, Decimal) and quotient_decimal == Decimal("2.5")

from decimal import Decimal

assert (-7 // 4, -7 % 4) == (-2, 1)
dividende, diviseur = Decimal("-7"), Decimal("4")
quotient, reste = divmod(dividende, diviseur)
assert (quotient, reste) == (Decimal("-1"), Decimal("-3"))
assert quotient * diviseur + reste == dividende

console_absence = code.InteractiveConsole()
sortie_absence = StringIO()
with redirect_stdout(sortie_absence):
    console_absence.push("None")
    assert sortie_absence.getvalue() == ""
    console_absence.push("0")
    console_absence.push("print(None)")
assert sortie_absence.getvalue().splitlines() == ["0", "None"]
