-
Notifications
You must be signed in to change notification settings - Fork 0
Exercice 2 : Revue de revue #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from model import LocalDatabaseModel | ||
| from view import ConsoleView | ||
| from presenter import Presenter | ||
|
|
||
| Presenter(LocalDatabaseModel(), ConsoleView()).run() |
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,20 @@ | ||||||
| import json | ||||||
|
|
||||||
| class LocalDatabaseModel: | ||||||
| """Modèle utilisant une base de donnée locale en JSON""" | ||||||
| def __init__(self): | ||||||
| # `utf-8` pour être sûr que le fichier est lu correctement quel que soit l'OS | ||||||
| with open("dsonames.json", "r+", encoding="utf-8") as f: | ||||||
| self.values = json.load(f) | ||||||
| sorted(self.values) | ||||||
|
|
||||||
| def names(self): | ||||||
| """Obtient la liste des noms, en anglais, des objets disponibles""" | ||||||
| result = [] | ||||||
| for _, planet in self.values.items(): | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| result.append(planet["name"]) | ||||||
| return result | ||||||
|
|
||||||
| def translated_names(self, language): | ||||||
| """Obtient une correspondance entre noms et noms traduits dans la langue donnée.""" | ||||||
| return [(p["name"], p[language]) for p in self.values.values() if language in p] | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Est-ce normal que certaines planètes n'ont pas de nom traduit ? Devrions-nous le documenter ? |
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,35 @@ | ||||||
| class Presenter: | ||||||
| """Presenter dans l'architecture MVP de l'app, qui gère les commandes utilisateur.""" | ||||||
|
|
||||||
| def __init__(self, model, view): | ||||||
| """Crée un Presenter avec un modèle et une view donnés.""" | ||||||
| self.model = model | ||||||
| self.view = view | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
|
|
||||||
| def _show_list(self, lst): | ||||||
| """Affiche la liste donnée via la view.""" | ||||||
| self.view.show("\n".join(["- " + str(s) for s in lst])) | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Plutôt |
||||||
|
|
||||||
| def onInput(self, command): | ||||||
| """Gère l'entrée utilisateur d'une commande donnée.""" | ||||||
| if command == "liste": | ||||||
| self._show_list(self.model.names()) | ||||||
| return | ||||||
|
|
||||||
| parts = command.split(' ') | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| if parts[0] == "reccerche": | ||||||
| # TODO: Recherche partielle (ou peut-être expressions régulières ?) | ||||||
| names = self.model.names() | ||||||
| self._show_list([n for n in names if parts[0] in n]) | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| return | ||||||
|
|
||||||
| if parts[0] == "traduire": | ||||||
| self._show_list(self.model.translated_names(parts[1])) | ||||||
| return | ||||||
|
|
||||||
| self.view.show("Commande inconnue") | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pas génial |
||||||
|
|
||||||
| def run(self): | ||||||
| """Gère l'entrée utilisateur d'une commande donnée.""" | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ce commentaire de doc est dupliqué avec la méthode |
||||||
| self.view.show("Bonjour! Cette application vous donne des informations sur les groupes d'étoiles.") | ||||||
| self.view.run(self, "Écrivez 'liste', 'recherche <texte>', ou 'traduire <langue>' (ou Ctrl+C): ") | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| from unittest import TestCase | ||
|
|
||
| from model import LocalDatabaseModel | ||
| from presenter import Presenter | ||
|
|
||
| # Vérifions que les données soient bien chargées | ||
| class LocalDatabaseModelTests(TestCase): | ||
| def test_names(self): | ||
| m = LocalDatabaseModel() | ||
| self.assertIn("Sextans Dwarf Spheroidal Galaxy", m.names()) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Devrions-nous tester l'ordre des planètes ? |
||
|
|
||
| def test_translate(self): | ||
| m = LocalDatabaseModel() | ||
| self.assertIn(("Sextans Dwarf Spheroidal Galaxy", "Galaxie naine sphéroïdale du Sextant"), m.translated_names("fr")) | ||
|
|
||
|
|
||
| class FakeModel: | ||
| def names(self): | ||
| return ["X", "Y", "Z"] | ||
|
|
||
| def translated_names(self, language): | ||
| return [("X", "A"), ("Y", language), ("Z", "C")] | ||
|
|
||
| class FakeView: | ||
| def __init__(self, inputs): | ||
| self._inputs = inputs | ||
| self.outputs = [] | ||
|
|
||
| def show(self, text): | ||
| self.outputs.append(text) | ||
|
|
||
| def run(self, presenter, prompt): | ||
| if len(self._inputs) > 0: | ||
| presenter.onInput(self._inputs[0]) | ||
| self._inputs = self._inputs[1:] | ||
|
|
||
| class PresenterTests(TestCase): | ||
| def test_liste(self): | ||
| v = FakeView(["liste"]) | ||
| p = Presenter(FakeModel(), v) | ||
| self.assertEqual([], v.outputs) | ||
| p.run() | ||
| self.assertEqual(["Bonjour! Cette application vous donne des informations sur les groupes d'étoiles.", "- X\n- Y\n- Z"], v.outputs) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Il existe des méthodes plus spécialisées que |
||
|
|
||
| def test_traduire(self): | ||
| v = FakeView(["traduire fr"]) | ||
| p = Presenter(FakeModel(), v) | ||
| self.assertEqual([], v.outputs) | ||
| p.run() | ||
| self.assertEqual(["Bonjour! Cette application vous donne des informations sur les groupes d'étoiles.", "- ('X', 'A')\n- ('Y', 'fr')\n- ('Z', 'C')"], v.outputs) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| class ConsoleView: | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Documentation ? |
||
| def show(self, text): | ||
| # Affiche `text` sur la console | ||
| print(text) | ||
|
|
||
| def run(self, presenter, prompt): | ||
| while True: | ||
| command = input(prompt) | ||
| presenter.onInput(command) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pourquoi ouvrir le fichier avec la permission
r+, qui signifie lecture + écriture ?r, lecture seule, devrait être suffisant