Skip to content

Tips for Python

Vladyslav Moisieienkov edited this page May 20, 2022 · 3 revisions

Contents

  1. Flask
  2. Pytest
  3. SQLAlchemy
  4. Alembic
  5. Click
  6. Black
  7. Imports
  8. Flake8

Flask

We are using Flask web development framework. If you are not familiar with Flask, you can peruse a good tutorial such as Miguel Grinberg's The Flask Mega-Tuturial.

Pytest

We are using pytest framework for unit testing. You may want to peruse its getting started tutorial.

All pytest-related fixtures and helpers are centralised in pytest-reana package.

SQLAlchemy

For persistence, we are using SQLAlchemy. You may want to peruse its tutorial.

Alembic

For database schema and model upgrades, we are using Alembic. You may want to peruse its tutorial.

Click

For creating command-line executables, parsing command-line arguments etc, we are using click.

Black

We are using Black code formatter. The code formatting is checked during CI build

$ pip install --upgrade black

Imports

We are not imposing a particular import order during CI build process, but we try to rely on isort rules gently applied locally.

This means to keep several import sections (separated by empty line) in the following order:

  1. Python built-in modules
  2. External dependency modules
  3. Local imports

Example:

from __future__ import absolute_import, print_function

import logging
import os

from reana_commons.config import REANA_LOG_FORMAT, REANA_LOG_LEVEL
from reana_commons.serial import serial_load
from reana_commons.workflow_engine import create_workflow_engine_command

from .config import CACHE_ENABLED
from .utils import (
    build_job_spec,
    check_cache,
    copy_workspace_from_cache,
    copy_workspace_to_cache,
    get_targeted_workflow_steps,
    poll_job_status,
    publish_cache_copy,
    publish_job_submission,
    publish_job_success,
    publish_workflow_failure,
    publish_workflow_start,
)

Flake8

We are using Flake8 code checker. The Flake8 compliance is checked during CI build.

In order for Flake8 to play nicely with Black, a special configuration is used:

[flake8]
max-line-length = 89
ignore = E203, E231, E266, E501, W503, F403, F401
max-complexity = 18
select = B,C,E,F,W,T4,B9

The REANA components usually contain .flake8 configuration files that are ready to be used. Please respect them and don't modify them individually.