4.0 KiB
[PyDev] Project 5 : Use public data of Open Food Facts project
Last version of this document is available on github.
Approach
Introduction
Search substitute for food product in the Open Food Facts DB and displays products informations
The user can:
- interact with the system in the terminal
- save products for later retrieval
This project was created for studying purpose, to train these tools :
- manipulating Open Food Facts
REST API
withRequests
- connecting a
MySQL
DB withPyMySQL
- using a
MariaDB
server with aMySQL
CLI (mycli
) - creating an interactive script
t purpose. It can evolve beyond for training with PyMySQL, Requests and OpenFF API.
The whole exercise description is available on OpenClassrooms site.
The project is hosted on github.
Workflow
- 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
- write code
Code construction
To build the script, I followed this approach :
- take in hand the necessary tools
- get started with some the basic features/tools
- finally add more specific detailed functionalities
Concretely :
- 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
- use
PyMySQL
in a basic way - create an interface object with the DB
- create the script interfacing the API and the DB
- create the DB CLI used by the user
Code organization
config.py
: configurationcli.py
: user interface scriptdb.py
: DB class used by all scriptsfunction.py
: files with functions used by all scriptspopulate-db.py
: back-office script used to populate DB using APIcreate-db-loff.sql
: back-office script used to creates local (empty) DBdoc
: documentation folderlegacy-sql
: first SQL manipulations (just for memories)sample
: Open Food Facts category & product JSON files (to work offline and lower API usage)
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():
Tests are not required in this project but, using doctest
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»?
Stay in the scope
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.