Python

Write an awesome doc for Python. A very nice an practical one extracted from Python official documentation.

View on GitHub

Shared Library Layout

Package Identity

The directory, the distribution name, and the import name are all the same string on purpose:

[!IMPORTANT]

Creating a directory called shared inside src is intentional. In fact that is why you can write imports like this:

from shared.db.user.repository import UserRepository

src/shared/db

Everything related to persistence lives here.

./shared/src/shared/db/
    __init__.py     # re-exports Base and imports every entity's model
    base.py         # the single DeclarativeBase every entity's model inherits
    engine.py       # create_db_engine, create_session_factory
    user/
        __init__.py
        model.py        # the User ORM model
        repository.py   # UserRepository

Adding a new Module

A future concern unrelated to persistence (e.g. shared datetime helpers) is a sibling of db/, not something bolted onto it:

shared/src/shared/
    db/
        ...
    datetime_utils.py

imported as from shared.datetime_utils import .... It only graduates into its own subpackage (datetime_utils/) the day it actually needs more than one file, the same rule db/ followed before it grew per-entity subpackages.