diff --git a/classes.py b/classes.py index 3ebca7a..212b6f4 100644 --- a/classes.py +++ b/classes.py @@ -1,6 +1,9 @@ # Une classe sert à stocker des attributs ensemble. from dataclasses import dataclass from statistics import mean +from matplotlib import pyplot as plt +import datetime as dt +from random import randint @dataclass @@ -9,7 +12,7 @@ class Point: y: float -from random import randint +origin = Point(0, 0) # origin.x = 0, origin.y = 0 class Dice: @@ -38,39 +41,18 @@ class DiceCup: return mean([dice.value for dice in self.dices]) -cup = DiceCup([Dice() for _ in range(1000)]) -results = {} -for i in range(10000): - cup.shake() - try: - results[cup.total()] = results[cup.total()] + 1 - except KeyError: - results[cup.total()] = 1 +def show_graph(): + cup = DiceCup([Dice() for _ in range(1000)]) + results = {} + for i in range(10000): + cup.shake() + try: + results[cup.total()] = results[cup.total()] + 1 + except KeyError: + results[cup.total()] = 1 -from matplotlib import pyplot as plt - -plt.scatter(results.keys(), results.values()) -plt.show() - - -class Point: - def __init__(self, x, y): # Dunder init "Double underscore init" - self.x = x - self.y = y - - def __repr__(self): - return f"Point(x={self.x}, y={self.y})" - - def dist_from_origin(self): - return (self.x**2 + self.y**2) ** 0.5 - - def dist(self, other): - """Compute distance between two points.""" - return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5 - - -origin = Point(0, 0) # origin.x = 0, origin.y = 0 -print(origin) + plt.scatter(results.keys(), results.values()) + plt.show() def dist_from_origin(x, y): @@ -103,9 +85,6 @@ def dist(p1, p2): return ((p1.x - p2.x) ** 2 + (p1.y - p2.y) ** 2) ** 0.5 -import datetime as dt - - @dataclass class Recette: nom: str diff --git a/point.py b/point.py new file mode 100644 index 0000000..014480d --- /dev/null +++ b/point.py @@ -0,0 +1,14 @@ +class Point: + def __init__(self, x, y): # Dunder init "Double underscore init" + self.x = x + self.y = y + + def __repr__(self): + return f"Point(x={self.x}, y={self.y})" + + def dist_from_origin(self): + return (self.x**2 + self.y**2) ** 0.5 + + def dist(self, other): + """Compute distance between two points.""" + return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5 diff --git a/tests/test_point.py b/tests/test_point.py new file mode 100644 index 0000000..a2769cd --- /dev/null +++ b/tests/test_point.py @@ -0,0 +1,32 @@ +from math import isclose + +from hypothesis import given +from hypothesis.strategies import floats + +from point import Point + + +def test_origin_is_at_zero(): + origin = Point(0, 0) + assert origin.dist_from_origin() == 0 + + +bound = 2**128 + + +@given(floats(allow_nan=False, min_value=-bound, max_value=bound)) +def test_not_origin_is_not_at_zero(x): + if isclose(x, 0, abs_tol=0.001): + return + not_origin = Point(x, 0) + assert not_origin.dist_from_origin() > 0 + + +@given( + floats(allow_nan=False, min_value=-bound, max_value=bound), + floats(allow_nan=False, min_value=-bound, max_value=bound), +) +def test_dist(x, y): + origin = Point(0, 0) + p1 = Point(x, y) + assert origin.dist(p1) == p1.dist(origin)