Development Tools
Development Environment
- Practical exercises and projects completed on a Datalab
- Can also be done on the VM (backup)
Datalab stack
Visual Studio Code: your code editorPython 3.13Git: to manage your repositoriesPostgreSQL: a databasecloudBeaver: for viewing and editing this database
What’s on your school VM?
Python 3.10: Python version chosen by ENSAIVisual Studio CodeGitPostgreSQLDBeaver: database administration tool to consult or edit
Visual Studio Code
- Widely used and known code editor
- Includes a file navigator and a terminal
- Many (many many) extensions
- Customization
Focus on Settings
.vscode/settings.json
{
"workbench.colorTheme": "Dark Modern",
"editor.fontSize": 16,
"[python]": {
"editor.formatOnSave": true,
"editor.insertSpaces": true,
"editor.tabSize": 4,
"editor.defaultFormatter": "charliermarsh.ruff",
},
"ruff.lint.enable": true,
"files.exclude": {
"**/__pycache__": true,
"**/.pytest_cache": true,
}
}- Montrer le fichier settings.json (DEMO)
- icone ⚙️ niveau workspace ou user
- à priori vous n’avez rien à modifier
- vous pouvez intégrer ce fichier dans votre dépôt projet
- Formater et Linter en phase
Formater :
- Ligne trop longue
- Espaces superflux
Code Quality
What is “quality” for code?
- Maintenance: easier to understand, maintain, and change/improve
- Readability: reduces errors and makes collaboration easier
- Performance: works better, faster, with fewer resources
- Security: less vulnerable and susceptible to exploits
Pylint
A static analysis tool for Python that:
- Checks code compliance with style standards
- Detects potential errors
- Identifies bad practices
Commands
pylint src
pylint --output-format=colorized --disable=C0114,C0411,C0415,W0718 $(git ls-files '*.py') --fail-under=7.5- You can disable rules that bother you
- Do this regularly
Repository Structure
🚧
PROJET-INFO-2A
├── .vscode/settings.json
├── data
│ └── init_db.sql
├── doc
│ └── suivi
│ └── YYYY.MM.DD-semaineN.md
├── src
│ ├── business_object
│ │ └── une_classe.py
│ ├── dao
│ │ └── une_classe_dao.py
│ ├── service
│ │ └── une_classe_service.py
│ ├── tests
│ │ ├── dao
│ │ └── service
│ ├── utils
│ └── view
│ └── accueil_view.py
├── .gitignore
├── LICENCE
├── README.md
└── requirements.txtDocumentation
mathematical_operations.py
class MathematicalOperations:
"""Mathematical Operations"""
def divide_five_by(self, number) -> float:
"""Divides the number 5 by a given number.
Parameters
----------
number : float or int
The number by which 5 will be divided.
Returns
-------
float or None
The result of dividing 5 by the given number.
If the number is equal to 0, the method returns None.
"""
if number != 0:
return 5 / number
else:
return None- Document classes
- Attributes
- Document methods
- Brief description
- Parameters
- Return
Logs
What are logs?
Note
Logging is the process of recording events, errors, and information in an application.
A log captures information about what happened at a given time:
- Monitoring
- Debugging
- They allow monitoring the state of a system, unexpected behaviors
- They help identify and resolve issues
Level 1 - Beginner
- You have an error in your code
- But you don’t know exactly where?
- What do you do?
You put “prints” everywhere
Level 2 - Debug Mode
- You add a breakpoint
- You launch in debug mode
- You proceed until the error
Demo
Level 3 - Logs
import logging
logging.basicConfig(
filename='logs/example.log',
level=logging.INFO,
format="%(asctime)s - %(levelname)-8s - %(message)s",
datefmt="%d/%m/%Y %H:%M:%S")It is possible to externalize the configuration in a logging_config.yml file
logger.debug("This is a debug message")
logger.info("This is an info message")
logger.warning("This is a warning message")
logger.error("This is an error message")Log Levels
- DEBUG: useful for debugging during development
- INFO: tracking executed code
- WARNING: unusual but non-blocking situation
- ERROR: affects program logic but doesn’t stop it completely
- CRITICAL: requires urgent intervention
A Good Start
import logging
def my_method(a):
logging.debug("my_method - START")
...
logging.debug("my_method - END")You will have something looks like :
- Method a ➡️ call method b ➡️ call method c, d …
It’s good to know where the code crashed.
Going Further
Use the modules:
- src/utils/log_init.py
- src/utils/log_decorator.py
And add a @Log decorator before each method
Wrapper: will encapsulate the method Demo log_decorator
