ocp5/doc/approach.md

97 lines
4.0 KiB
Markdown
Raw Normal View History

2018-08-08 22:18:36 +00:00
-[_Courses Open Classrooms_][2]-
2018-07-17 17:03:28 +00:00
2018-08-08 22:18:36 +00:00
# [PyDev] Project 5 : Use public data of [Open Food Facts][1] project
2018-08-09 11:17:18 +00:00
_Last version of this document is available on [github][4]._
2018-08-08 22:18:36 +00:00
## Approach
### Introduction
Search substitute for food product in the [Open Food Facts][1] DB and displays products informations
The user can:
2018-08-09 11:17:18 +00:00
- interact with the system in the terminal
- save products for later retrieval
2018-08-08 22:18:36 +00:00
2018-08-09 11:17:18 +00:00
This project was created for studying purpose, to train these tools :
2018-08-08 22:18:36 +00:00
2018-08-09 11:17:18 +00:00
- manipulating [Open Food Facts `REST API`][9] with [`Requests`][6]
- connecting a `MySQL` DB with [`PyMySQL`][7]
- using a [`MariaDB`][8] server with a `MySQL` CLI (`mycli`)
2018-08-08 22:18:36 +00:00
- creating an interactive script
2018-08-09 11:17:18 +00:00
[](https://world.openfoodfacts.org/) t purpose. It can evolve beyond for training with [PyMySQL](), [Requests]() and [OpenFF API](https://en.wiki.openfoodfacts.org/API).
The whole exercise description is available on [OpenClassrooms site][2].
2018-08-08 22:18:36 +00:00
The project is hosted on [github][5].
### Workflow
2018-08-09 11:17:18 +00:00
- plan the work : features, scripts, files, functions tool needed…
- create `features`, grouped in autonomous packages
- organize `features` in a _kanban_ type table
- write documentation, as text & [`doctest`][3]
- write code
2018-08-08 22:18:36 +00:00
### Code construction
To build the script, I followed this approach :
1. take in hand the necessary tools
2. get started with some the basic features/tools
2018-08-09 11:17:18 +00:00
3. finally add more specific detailed functionalities
2018-08-08 22:18:36 +00:00
2018-08-09 11:17:18 +00:00
Concretely :
2018-08-08 22:18:36 +00:00
- familiarize myself with the data stored in the DB (playing with the CSV file)
- use the API in a basic way (see `get_product()` function)
- imagine the main SQL queries
2018-08-09 11:17:18 +00:00
- use `PyMySQL` in a basic way
2018-08-08 22:18:36 +00:00
- create an interface object with the DB
- create the script interfacing the API and the DB
2018-08-09 11:17:18 +00:00
- create the DB CLI used by the user
2018-08-08 22:18:36 +00:00
2018-08-09 11:17:18 +00:00
### Code organization
2018-08-08 22:18:36 +00:00
- `config.py` : configuration
2018-08-09 11:17:18 +00:00
- `cli.py` : user interface script
2018-08-08 22:18:36 +00:00
- `db.py` : DB class used by all scripts
- `function.py` : files with functions used by all scripts
2018-08-09 11:17:18 +00:00
- `populate-db.py` : back-office script used to populate DB using API
- `create-db-loff.sql` : back-office script used to creates local (empty) DB
2018-08-08 22:18:36 +00:00
- `doc` : documentation folder
- `legacy-sql` : first SQL manipulations (just for memories)
2018-08-09 11:17:18 +00:00
- `sample` : _Open Food Facts_ category & product JSON files (to work offline and lower API usage)
2018-08-08 22:18:36 +00:00
### Difficulties encountered
#### How to interact between the API and the DB
Before exchanging with my mentor I think about using the CSV file as a resource for creating the local DB and then the API will allow to keep it up to date afterwards. This version would have required a large amount of time without bringing a gain of interest.
#### Creating class Db():
2018-08-09 11:17:18 +00:00
Tests are not required in this project but, using [`doctest`][3] is a precious help when I code. It was an opportunity for me to use them in a class, dealing with trouble like «where the test are useful»?
2018-08-08 22:18:36 +00:00
#### Stay in the scope
2018-08-09 11:17:18 +00:00
Not easy to frame initial specifications that have a part of interpretation. Similarly for the levels of details, it is easy to slip into a design where they are places for evolution and/or new features.
2018-08-08 22:18:36 +00:00
[1]: https://fr.openfoodfacts.org/ "Open Food Facts répertorie les produits alimentaires du monde entier"
[2]: https://openclassrooms.com/fr/projects/utilisez-les-donnees-publiques-de-lopenfoodfacts "Utilisez les données publiques de l'OpenFoodFact"
[3]: https://docs.python.org/fr/3.6/library/doctest.html "Test interactive Python examples"
[4]: https://github.com/freezed/ocp5/blob/master/doc/approach.md
[5]: https://github.com/freezed/ocp5/projects/1
2018-08-09 11:17:18 +00:00
[6]: https://pypi.org/project/requests/#description "Requests is the only Non-GMO HTTP library for Python, safe for human consumption"
[7]: https://pymysql.readthedocs.io/en/latest/ "Pure Python MySQL Client"
[8]: https://mariadb.org/ "One of the most popular database servers. Made by the original developers of MySQL. Guaranteed to stay open source."
[9]: https://en.wiki.openfoodfacts.org/API "Open Food Facts API"