Hello pytest.

This commit is contained in:
Julien Palard 2023-12-12 16:15:53 +01:00
parent 3f2ea69abd
commit be8b24da14
Signed by: mdk
GPG Key ID: 0EFC1AC1006886F8
3 changed files with 61 additions and 36 deletions

View File

@ -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

14
point.py Normal file
View File

@ -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

32
tests/test_point.py Normal file
View File

@ -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)