Semaine 4, jour 2
This commit is contained in:
1
Semaine_04/Bibliotheque/.python-version
Normal file
1
Semaine_04/Bibliotheque/.python-version
Normal file
@@ -0,0 +1 @@
|
||||
3.12
|
||||
0
Semaine_04/Bibliotheque/README.md
Normal file
0
Semaine_04/Bibliotheque/README.md
Normal file
56
Semaine_04/Bibliotheque/functions.py
Normal file
56
Semaine_04/Bibliotheque/functions.py
Normal file
@@ -0,0 +1,56 @@
|
||||
import json
|
||||
|
||||
def add_new_book(book_list: list[dict[str, str]], book: dict[str, str]): # On précise le contenu des clefs et valeurs du dictionnaire
|
||||
"""Ajoute un nouveau livre"""
|
||||
if book in book_list:
|
||||
return f"{book["Title"].upper()} y est déjà."
|
||||
book_list.append(book)
|
||||
return f"{book["Title"].upper()} a été ajouté."
|
||||
|
||||
def take_out_book(book_list: list[dict[str, str]], title: str):
|
||||
"""Retire un livre déjà présent"""
|
||||
for book in book_list:
|
||||
if book["Title"].lower() == title.lower():
|
||||
book_list.remove(book)
|
||||
return f"{title.upper()} a été retiré."
|
||||
return f"{title.upper()} n'y est pas."
|
||||
|
||||
def list_books_in_list(book_list: list[dict]):
|
||||
"""Liste les livres présents"""
|
||||
return book_list
|
||||
|
||||
def sort_books_by_name(book_list: list[dict]):
|
||||
"""Tri les livres par ordre alphabétique"""
|
||||
book_list.sort(key= lambda dict: dict["Title"]) # pour trier par clefs et non plus par objets
|
||||
|
||||
def display_number_of_books(book_list: list) -> int:
|
||||
"""Donne le nombre de livres présents"""
|
||||
return len(book_list)
|
||||
|
||||
def search_book_by_word(book_list: list[dict[str, str]], word: str):
|
||||
"""Cherche un livre par mot de départ"""
|
||||
results = []
|
||||
for book in book_list:
|
||||
if word.lower() in book["Title"].lower():
|
||||
results.append(book)
|
||||
return results if len(results) > 0 else "Pas de résultat."
|
||||
|
||||
def search_book_by_letter(book_list: list[dict[str, str]], letter: str):
|
||||
"""Cherche un livre par lettre de départ"""
|
||||
results = []
|
||||
for book in book_list:
|
||||
if book["Title"].startswith(letter):
|
||||
results.append(book)
|
||||
return results if len(results) > 0 else "Pas de résultat."
|
||||
|
||||
# Sauvegarde des livres dans un JSON pour persistance des données
|
||||
|
||||
def save(book_list):
|
||||
"""Inscris dans un fichier JSON le contenu de la bibliothèque"""
|
||||
with open("books.json", "w") as json_file:
|
||||
json.dump(book_list, json_file, indent= 4)
|
||||
|
||||
def load():
|
||||
"""Récupère le contenu d'un fichier JSON"""
|
||||
with open("books.json") as json_file:
|
||||
return json.load(json_file)
|
||||
36
Semaine_04/Bibliotheque/main.py
Normal file
36
Semaine_04/Bibliotheque/main.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from functions import \
|
||||
add_new_book,\
|
||||
take_out_book,\
|
||||
list_books_in_list,\
|
||||
sort_books_by_name,\
|
||||
display_number_of_books,\
|
||||
search_book_by_word,\
|
||||
search_book_by_letter,\
|
||||
save, load
|
||||
|
||||
books = []
|
||||
|
||||
for i in range(3):
|
||||
new_book_title = input ("Quel livre ajouter ? > ")
|
||||
new_book_author = input ("Qui l'a écrit ? > ")
|
||||
new_book = {
|
||||
"Title": new_book_title,
|
||||
"Author": new_book_author
|
||||
}
|
||||
new_book_added = add_new_book(books, new_book)
|
||||
print (new_book_added)
|
||||
|
||||
#print(take_out_book(books, new_book))
|
||||
|
||||
sort_books_by_name(books)
|
||||
print(list_books_in_list(books))
|
||||
|
||||
book_number = display_number_of_books(books)
|
||||
|
||||
print(f"La bibliothèque contient {book_number} livres.")
|
||||
|
||||
searching_word = input("Mot recherché > ")
|
||||
print(search_book_by_word(books, searching_word))
|
||||
|
||||
searching_letter = input("Lettre recherchée > ")
|
||||
print(search_book_by_letter(books, searching_letter))
|
||||
7
Semaine_04/Bibliotheque/pyproject.toml
Normal file
7
Semaine_04/Bibliotheque/pyproject.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[project]
|
||||
name = "bibliotheque"
|
||||
version = "0.1.0"
|
||||
description = "Add your description here"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.12"
|
||||
dependencies = []
|
||||
8
Semaine_04/Bibliotheque/uv.lock
generated
Normal file
8
Semaine_04/Bibliotheque/uv.lock
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
version = 1
|
||||
revision = 3
|
||||
requires-python = ">=3.12"
|
||||
|
||||
[[package]]
|
||||
name = "bibliotheque"
|
||||
version = "0.1.0"
|
||||
source = { virtual = "." }
|
||||
Reference in New Issue
Block a user