examples
This commit is contained in:
parent
a4b8b1e379
commit
5c769cac77
|
@ -0,0 +1,171 @@
|
||||||
|
# Une classe sert à stocker des attributs ensemble.
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from statistics import mean
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Point:
|
||||||
|
x: float
|
||||||
|
y: float
|
||||||
|
|
||||||
|
|
||||||
|
from random import randint
|
||||||
|
|
||||||
|
|
||||||
|
class Dice:
|
||||||
|
def __init__(self, value=6):
|
||||||
|
self.value = value
|
||||||
|
|
||||||
|
def throw(self):
|
||||||
|
self.value = randint(1, 6)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return " ⚀⚁⚂⚃⚄⚅"[self.value]
|
||||||
|
|
||||||
|
|
||||||
|
class DiceCup:
|
||||||
|
def __init__(self, dices):
|
||||||
|
self.dices = dices
|
||||||
|
|
||||||
|
def shake(self):
|
||||||
|
for dice in self.dices:
|
||||||
|
dice.throw()
|
||||||
|
|
||||||
|
def total(self):
|
||||||
|
return sum([dice.value for dice in self.dices])
|
||||||
|
|
||||||
|
def mean(self):
|
||||||
|
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
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
def dist_from_origin(x, y):
|
||||||
|
"""Compute distance between a point and the origin."""
|
||||||
|
return (x**2 + y**2) ** 0.5
|
||||||
|
|
||||||
|
|
||||||
|
def dist(x1, y1, x2, y2):
|
||||||
|
"""Compute distance between two points."""
|
||||||
|
return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5
|
||||||
|
|
||||||
|
|
||||||
|
def dist_from_origin(point):
|
||||||
|
"""Compute distance between a point and the origin."""
|
||||||
|
return (point["x"] ** 2 + point["y"] ** 2) ** 0.5
|
||||||
|
|
||||||
|
|
||||||
|
def dist(p1, p2):
|
||||||
|
"""Compute distance between two points."""
|
||||||
|
return ((p1["x"] - p2["x"]) ** 2 + (p1["y"] - p2["y"]) ** 2) ** 0.5
|
||||||
|
|
||||||
|
|
||||||
|
def dist_from_origin(point):
|
||||||
|
"""Compute distance between a point and the origin."""
|
||||||
|
return (point.x**2 + point.y**2) ** 0.5
|
||||||
|
|
||||||
|
|
||||||
|
def dist(p1, p2):
|
||||||
|
"""Compute distance between two points."""
|
||||||
|
return ((p1.x - p2.x) ** 2 + (p1.y - p2.y) ** 2) ** 0.5
|
||||||
|
|
||||||
|
|
||||||
|
import datetime as dt
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Recette:
|
||||||
|
nom: str
|
||||||
|
ingrédients: dict
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Report:
|
||||||
|
date: dt.datetime
|
||||||
|
nb_last_month: int
|
||||||
|
nb_current_month: int
|
||||||
|
total_values: float
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
...
|
||||||
|
|
||||||
|
def save(self, path):
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
def load_report(path):
|
||||||
|
return Report(dt.date.today(), 0, 0, 0)
|
||||||
|
|
||||||
|
|
||||||
|
def update_report(report):
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
# def compare_reports(
|
||||||
|
# date,
|
||||||
|
# nb_last_month,
|
||||||
|
# nb_current_month,
|
||||||
|
# total_values,
|
||||||
|
# other_date,
|
||||||
|
# other_nb_last_month,
|
||||||
|
# other_nb_current_month,
|
||||||
|
# other_total_values,
|
||||||
|
# ):
|
||||||
|
# ...
|
||||||
|
|
||||||
|
|
||||||
|
def compare_reports(report, other_report):
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
def save_report(path, report):
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
daily_report = load_report("daily.xls")
|
||||||
|
monthly_report = load_report("monthly.xls")
|
||||||
|
update_report(daily_report)
|
||||||
|
update_report(monthly_report)
|
||||||
|
save_report("daily.xls", daily_report)
|
||||||
|
save_report("monthly.xls", monthly_report)
|
||||||
|
|
||||||
|
|
||||||
|
daily_report = load_report("daily.xls")
|
||||||
|
monthly_report = load_report("monthly.xls")
|
||||||
|
daily_report.update()
|
||||||
|
monthly_report.update()
|
||||||
|
daily_report.save("daily.xls")
|
||||||
|
monthly_report.save("monthly.xls")
|
|
@ -0,0 +1,12 @@
|
||||||
|
# import argparse
|
||||||
|
#
|
||||||
|
# parser = argparse.ArgumentParser()
|
||||||
|
# parser.add_argument("--version", "-v", action="store_true")
|
||||||
|
# parser.add_argument("input_file")
|
||||||
|
# parser.add_argument("output_file")
|
||||||
|
#
|
||||||
|
# args = parser.parse_args()
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
|
print(sys.argv)
|
|
@ -6,6 +6,7 @@ def is_even(n):
|
||||||
"""
|
"""
|
||||||
return n % 2 == 0
|
return n % 2 == 0
|
||||||
|
|
||||||
|
|
||||||
def is_odd(n):
|
def is_odd(n):
|
||||||
"""Returns True if n is odd.
|
"""Returns True if n is odd.
|
||||||
|
|
||||||
|
@ -14,6 +15,7 @@ def is_odd(n):
|
||||||
"""
|
"""
|
||||||
return not is_even(n)
|
return not is_even(n)
|
||||||
|
|
||||||
|
|
||||||
for i in range(10):
|
for i in range(10):
|
||||||
if is_even(i):
|
if is_even(i):
|
||||||
print(f"{i} est pair")
|
print(f"{i} est pair")
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
# Liste en compréhension
|
||||||
|
# Liste en intension (!= extension)
|
||||||
|
|
||||||
|
[1, 2, 3]
|
||||||
|
print([i for i in range(0, 100) if i % 3 == 0 or i % 5 == 0])
|
||||||
|
|
||||||
|
ensemble_en_extension = {1, 2, 3}
|
||||||
|
ensemble_en_intension = {i for i in range(0, 100) if i % 3 == 0 or i % 5 == 0}
|
||||||
|
|
||||||
|
{1: 1, 2: 2, 3: 3}
|
||||||
|
print({i: i**2 for i in range(0, 100) if i % 3 == 0 or i % 5 == 0})
|
||||||
|
|
||||||
|
|
||||||
|
durées = [recette["durée"] for recette in recettes]
|
Loading…
Reference in New Issue