Semaine 4, jour 3, matin
This commit is contained in:
@@ -5,6 +5,7 @@ def add_new_book(book_list: list[dict[str, str]], book: dict[str, str]): # On pr
|
|||||||
if book in book_list:
|
if book in book_list:
|
||||||
return f"{book["Title"].upper()} y est déjà."
|
return f"{book["Title"].upper()} y est déjà."
|
||||||
book_list.append(book)
|
book_list.append(book)
|
||||||
|
save(book_list)
|
||||||
return f"{book["Title"].upper()} a été ajouté."
|
return f"{book["Title"].upper()} a été ajouté."
|
||||||
|
|
||||||
def take_out_book(book_list: list[dict[str, str]], title: str):
|
def take_out_book(book_list: list[dict[str, str]], title: str):
|
||||||
@@ -12,6 +13,7 @@ def take_out_book(book_list: list[dict[str, str]], title: str):
|
|||||||
for book in book_list:
|
for book in book_list:
|
||||||
if book["Title"].lower() == title.lower():
|
if book["Title"].lower() == title.lower():
|
||||||
book_list.remove(book)
|
book_list.remove(book)
|
||||||
|
save(book_list)
|
||||||
return f"{title.upper()} a été retiré."
|
return f"{title.upper()} a été retiré."
|
||||||
return f"{title.upper()} n'y est pas."
|
return f"{title.upper()} n'y est pas."
|
||||||
|
|
||||||
@@ -39,7 +41,7 @@ def search_book_by_letter(book_list: list[dict[str, str]], letter: str):
|
|||||||
"""Cherche un livre par lettre de départ"""
|
"""Cherche un livre par lettre de départ"""
|
||||||
results = []
|
results = []
|
||||||
for book in book_list:
|
for book in book_list:
|
||||||
if book["Title"].startswith(letter):
|
if book["Title"].lower().startswith(letter):
|
||||||
results.append(book)
|
results.append(book)
|
||||||
return results if len(results) > 0 else "Pas de résultat."
|
return results if len(results) > 0 else "Pas de résultat."
|
||||||
|
|
||||||
@@ -53,4 +55,4 @@ def save(book_list):
|
|||||||
def load():
|
def load():
|
||||||
"""Récupère le contenu d'un fichier JSON"""
|
"""Récupère le contenu d'un fichier JSON"""
|
||||||
with open("books.json") as json_file:
|
with open("books.json") as json_file:
|
||||||
return json.load(json_file)
|
return json.load(json_file)
|
||||||
|
|||||||
@@ -51,4 +51,11 @@ objet.append({"titre": "Alice aux pays", "auteur": "Caroll"})
|
|||||||
|
|
||||||
with open("livres.json", "w") as fichier_json:
|
with open("livres.json", "w") as fichier_json:
|
||||||
json.dump(objet, fichier_json, indent= 4)
|
json.dump(objet, fichier_json, indent= 4)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Les classes
|
||||||
|
|
||||||
|
### Méthodes "Dunder"
|
||||||
|
|
||||||
|
- __init__
|
||||||
|
- __repr__
|
||||||
1
Semaine_04/ProgOrObj/.python-version
Normal file
1
Semaine_04/ProgOrObj/.python-version
Normal file
@@ -0,0 +1 @@
|
|||||||
|
3.12
|
||||||
0
Semaine_04/ProgOrObj/README.md
Normal file
0
Semaine_04/ProgOrObj/README.md
Normal file
33
Semaine_04/ProgOrObj/fractions.py
Normal file
33
Semaine_04/ProgOrObj/fractions.py
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import fractions
|
||||||
|
|
||||||
|
|
||||||
|
class Fraction:
|
||||||
|
numerator = 1
|
||||||
|
denominator = 1
|
||||||
|
|
||||||
|
def __init__(self, numerator, denominator):
|
||||||
|
self.numerator = numerator
|
||||||
|
self.denominator = denominator
|
||||||
|
|
||||||
|
def quotient(self):
|
||||||
|
"""[Fraction] Chiffre sur Chiffre"""
|
||||||
|
return self.numerator / self.denominator
|
||||||
|
|
||||||
|
def plus(self, fraction):
|
||||||
|
"""[Fraction] Addition de deux fractions"""
|
||||||
|
num = (self.numerator * self.denominator) + (self.denominator + fraction.numerator)
|
||||||
|
den = self.denominator * fraction.denominator
|
||||||
|
return Fraction(numerator= num, denominator= den)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"Fraction({self.numerator}/{self.denominator})"
|
||||||
|
|
||||||
|
tiers = Fraction(numerator= 1, denominator= 3)
|
||||||
|
quart = Fraction(1, 4)
|
||||||
|
|
||||||
|
# print(tiers.__dict__)
|
||||||
|
# print(quart.__dict__)
|
||||||
|
# print(Fraction.__dict__)
|
||||||
|
|
||||||
|
fraction = tiers.plus(quart)
|
||||||
|
print(fraction)
|
||||||
38
Semaine_04/ProgOrObj/fractions_theorie.py
Normal file
38
Semaine_04/ProgOrObj/fractions_theorie.py
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
from pprint import pprint
|
||||||
|
|
||||||
|
prenom = "John" # crée une instance (obj) de type 'str' et l'initialise avec la valeur 'John'
|
||||||
|
nom = str('Doe') # crée une instance (obj) de type 'str' et l'initialise avec la valeur 'Doe'
|
||||||
|
adress = str() # crée une instance (obj) de type 'str'
|
||||||
|
|
||||||
|
# list, dict, tuple, set, bool => types primitives
|
||||||
|
|
||||||
|
class Fraction: # pour se differencier des types primitifs
|
||||||
|
numerateur = 1
|
||||||
|
denominateur = 1
|
||||||
|
|
||||||
|
def quotient(self): # self = tiers /quart
|
||||||
|
return self.numerateur / self.denominateur
|
||||||
|
|
||||||
|
tiers = Fraction() # crée une instance (obj) de type 'Fraction'
|
||||||
|
tiers.numerateur = 1
|
||||||
|
tiers.denominateur = 3
|
||||||
|
quart = Fraction()
|
||||||
|
quart.numerateur = 1
|
||||||
|
quart.denominateur = 4
|
||||||
|
|
||||||
|
print(tiers.__dict__)
|
||||||
|
print(quart.__dict__)
|
||||||
|
|
||||||
|
# Mapping proxy
|
||||||
|
# pprint(Fraction.__dict__)
|
||||||
|
# pprint(tiers.__dict__)
|
||||||
|
# print(tiers.denominateur)
|
||||||
|
|
||||||
|
# 2 points de vue: Fraction (classe) et tiers (objet)
|
||||||
|
print(Fraction.quotient) # <function Fraction.dire_bonjour at 0x100eec720>
|
||||||
|
print(tiers.quotient) # <bound method Fraction.dire_bonjour of <__main__.Fraction object at 0x1028937d0>>
|
||||||
|
# <bound method Fraction.dire_bonjour of tiers>
|
||||||
|
|
||||||
|
print(tiers.quotient()) # TypeError: Fraction.dire_bonjour() takes 0 positional arguments but 1 was given
|
||||||
|
# Les methodes lies aux objets ont besoin de savoir QUI les a appeler
|
||||||
|
print(quart.quotient())
|
||||||
0
Semaine_04/ProgOrObj/main.py
Normal file
0
Semaine_04/ProgOrObj/main.py
Normal file
7
Semaine_04/ProgOrObj/pyproject.toml
Normal file
7
Semaine_04/ProgOrObj/pyproject.toml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
[project]
|
||||||
|
name = "progorobj"
|
||||||
|
version = "0.1.0"
|
||||||
|
description = "Add your description here"
|
||||||
|
readme = "README.md"
|
||||||
|
requires-python = ">=3.12"
|
||||||
|
dependencies = []
|
||||||
8
Semaine_04/ProgOrObj/uv.lock
generated
Normal file
8
Semaine_04/ProgOrObj/uv.lock
generated
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
version = 1
|
||||||
|
revision = 3
|
||||||
|
requires-python = ">=3.12"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "progorobj"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = { virtual = "." }
|
||||||
Reference in New Issue
Block a user