Merge branch 'release-0.2.0' into main
Some checks failed
CI / tests (push) Has been cancelled

This commit is contained in:
Alexander Hess 2020-09-30 12:20:58 +02:00
commit 3393071db3
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
33 changed files with 4645 additions and 223 deletions

4
.gitignore vendored
View file

@ -1,4 +1,6 @@
.cache/
*.egg-info/
**/*.egg-info/
.env
**/.ipynb_checkpoints/
.python-version
.venv/

View file

@ -4,18 +4,30 @@ repos:
# Run the local formatting, linting, and testing tool chains.
- repo: local
hooks:
- id: local-pre-commit-checks
name: Run code formatters and linters
entry: poetry run nox -s pre-commit --
- id: local-fix-branch-references
name: Check for wrong branch references
entry: poetry run nox -s fix-branch-references --
language: system
stages: [commit]
types: [text]
- id: local-format
name: Format the source files
entry: poetry run nox -s format --
language: system
stages: [commit]
types: [python]
- id: local-pre-merge-checks
name: Run the entire test suite
entry: poetry run nox -s pre-merge --
- id: local-lint
name: Lint the source files
entry: poetry run nox -s lint --
language: system
stages: [merge-commit, push]
stages: [commit]
types: [python]
- id: local-test-suite
name: Run the entire test suite
entry: poetry run nox -s test-suite --
language: system
stages: [merge-commit]
types: [text]
# Enable hooks provided by the pre-commit project to
# enforce rules that local tools could not that easily.
- repo: https://github.com/pre-commit/pre-commit-hooks

View file

@ -6,11 +6,40 @@ operating in France from January 2016 to January 2017.
The goal is to
optimize the platform's delivery process involving independent couriers.
## Structure
The analysis is structured into three aspects
that iteratively build on each other.
## Real-time Demand Forecasting
### Real-time Demand Forecasting
## Predictive Routing
### Predictive Routing
## Shift & Capacity Planning
### Shift & Capacity Planning
## Installation & Contribution
To play with the code developed for the analyses,
you can clone the project with [git](https://git-scm.com/)
and install the contained `urban-meal-delivery` package
and all its dependencies
in a [virtual environment](https://docs.python.org/3/tutorial/venv.html)
with [poetry](https://python-poetry.org/docs/):
`git clone https://github.com/webartifex/urban-meal-delivery.git`
and
`poetry install --extras research`
The `--extras` option is necessary as the non-develop dependencies
are structured in the [pyproject.toml](https://github.com/webartifex/urban-meal-delivery/blob/main/pyproject.toml) file
into dependencies related to only the `urban-meal-delivery` source code package
and dependencies used to run the [Jupyter](https://jupyter.org/) environment
with the analyses.
Contributions are welcome.
Use the [issues](https://github.com/webartifex/urban-meal-delivery/issues) tab.
The project is licensed under the [MIT license](https://github.com/webartifex/urban-meal-delivery/blob/main/LICENSE.txt).

44
alembic.ini Normal file
View file

@ -0,0 +1,44 @@
[alembic]
file_template = rev_%%(year)d%%(month).2d%%(day).2d_%%(hour).2d_%%(rev)s_%%(slug)s
script_location = %(here)s/migrations
[post_write_hooks]
hooks=black
black.type=console_scripts
black.entrypoint=black
# The following is taken from the default alembic.ini file.
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
qualname =
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S

4
migrations/README.md Normal file
View file

@ -0,0 +1,4 @@
# Database Migrations
This project uses [alembic](https://alembic.sqlalchemy.org/en/latest)
to run the database migrations

45
migrations/env.py Normal file
View file

@ -0,0 +1,45 @@
"""Configure Alembic's migration environment."""
import os
from logging import config as log_config
import sqlalchemy as sa
from alembic import context
from urban_meal_delivery import config as umd_config
from urban_meal_delivery import db
# Disable the --sql option, a.k.a, the "offline mode".
if context.is_offline_mode():
raise NotImplementedError('The --sql option is not implemented in this project')
# Set up the default Python logger from the alembic.ini file.
log_config.fileConfig(context.config.config_file_name)
def include_object(obj, _name, type_, _reflected, _compare_to):
"""Only include the clean schema into --autogenerate migrations."""
if type_ in {'table', 'column'} and obj.schema != umd_config.DATABASE_SCHEMA:
return False
return True
engine = sa.create_engine(umd_config.DATABASE_URI)
with engine.connect() as connection:
context.configure(
connection=connection,
include_object=include_object,
target_metadata=db.Base.metadata,
version_table='{alembic_table}{test_schema}'.format(
alembic_table=umd_config.ALEMBIC_TABLE,
test_schema=(f'_{umd_config.CLEAN_SCHEMA}' if os.getenv('TESTING') else ''),
),
version_table_schema=umd_config.ALEMBIC_TABLE_SCHEMA,
)
with context.begin_transaction():
context.run_migrations()

31
migrations/script.py.mako Normal file
View file

@ -0,0 +1,31 @@
"""${message}.
Revision: # ${up_revision} at ${create_date}
Revises: # ${down_revision | comma,n}
"""
import os
import sqlalchemy as sa
from alembic import op
${imports if imports else ""}
from urban_meal_delivery import configuration
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
config = configuration.make_config('testing' if os.getenv('TESTING') else 'production')
def upgrade():
"""Upgrade to revision ${up_revision}."""
${upgrades if upgrades else "pass"}
def downgrade():
"""Downgrade to revision ${down_revision}."""
${downgrades if downgrades else "pass"}

View file

@ -0,0 +1,802 @@
"""Create the database from scratch.
Revision: #f11cd76d2f45 at 2020-08-06 23:24:32
"""
import os
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql
from urban_meal_delivery import configuration
revision = 'f11cd76d2f45'
down_revision = None
branch_labels = None
depends_on = None
config = configuration.make_config('testing' if os.getenv('TESTING') else 'production')
def upgrade():
"""Upgrade to revision f11cd76d2f45."""
op.execute(f'CREATE SCHEMA {config.CLEAN_SCHEMA};')
op.create_table( # noqa:ECE001
'cities',
sa.Column('id', sa.SmallInteger(), autoincrement=False, nullable=False),
sa.Column('name', sa.Unicode(length=10), nullable=False),
sa.Column('kml', sa.UnicodeText(), nullable=False),
sa.Column('center_latitude', postgresql.DOUBLE_PRECISION(), nullable=False),
sa.Column('center_longitude', postgresql.DOUBLE_PRECISION(), nullable=False),
sa.Column('northeast_latitude', postgresql.DOUBLE_PRECISION(), nullable=False),
sa.Column('northeast_longitude', postgresql.DOUBLE_PRECISION(), nullable=False),
sa.Column('southwest_latitude', postgresql.DOUBLE_PRECISION(), nullable=False),
sa.Column('southwest_longitude', postgresql.DOUBLE_PRECISION(), nullable=False),
sa.Column('initial_zoom', sa.SmallInteger(), nullable=False),
sa.PrimaryKeyConstraint('id', name=op.f('pk_cities')),
*(
[ # noqa:WPS504
sa.ForeignKeyConstraint(
['id'],
[f'{config.ORIGINAL_SCHEMA}.cities.id'],
name=op.f('pk_cities_sanity'),
onupdate='RESTRICT',
ondelete='RESTRICT',
),
]
if not config.TESTING
else []
),
schema=config.CLEAN_SCHEMA,
)
op.create_table( # noqa:ECE001
'couriers',
sa.Column('id', sa.Integer(), autoincrement=False, nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('vehicle', sa.Unicode(length=10), nullable=False),
sa.Column('speed', postgresql.DOUBLE_PRECISION(), nullable=False),
sa.Column('capacity', sa.SmallInteger(), nullable=False),
sa.Column('pay_per_hour', sa.SmallInteger(), nullable=False),
sa.Column('pay_per_order', sa.SmallInteger(), nullable=False),
sa.CheckConstraint(
"vehicle IN ('bicycle', 'motorcycle')",
name=op.f('ck_couriers_on_available_vehicle_types'),
),
sa.CheckConstraint(
'0 <= capacity AND capacity <= 200',
name=op.f('ck_couriers_on_capacity_under_200_liters'),
),
sa.CheckConstraint(
'0 <= pay_per_hour AND pay_per_hour <= 1500',
name=op.f('ck_couriers_on_realistic_pay_per_hour'),
),
sa.CheckConstraint(
'0 <= pay_per_order AND pay_per_order <= 650',
name=op.f('ck_couriers_on_realistic_pay_per_order'),
),
sa.CheckConstraint(
'0 <= speed AND speed <= 30', name=op.f('ck_couriers_on_realistic_speed'),
),
sa.PrimaryKeyConstraint('id', name=op.f('pk_couriers')),
*(
[ # noqa:WPS504
sa.ForeignKeyConstraint(
['id'],
[f'{config.ORIGINAL_SCHEMA}.couriers.id'],
name=op.f('pk_couriers_sanity'),
onupdate='RESTRICT',
ondelete='RESTRICT',
),
]
if not config.TESTING
else []
),
schema=config.CLEAN_SCHEMA,
)
op.create_table(
'customers',
sa.Column('id', sa.Integer(), autoincrement=False, nullable=False),
sa.PrimaryKeyConstraint('id', name=op.f('pk_customers')),
schema=config.CLEAN_SCHEMA,
)
op.create_table( # noqa:ECE001
'addresses',
sa.Column('id', sa.Integer(), autoincrement=False, nullable=False),
sa.Column('primary_id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('place_id', sa.Unicode(length=120), nullable=False), # noqa:WPS432
sa.Column('latitude', postgresql.DOUBLE_PRECISION(), nullable=False),
sa.Column('longitude', postgresql.DOUBLE_PRECISION(), nullable=False),
sa.Column('city_id', sa.SmallInteger(), nullable=False),
sa.Column('city', sa.Unicode(length=25), nullable=False), # noqa:WPS432
sa.Column('zip_code', sa.Integer(), nullable=False),
sa.Column('street', sa.Unicode(length=80), nullable=False), # noqa:WPS432
sa.Column('floor', sa.SmallInteger(), nullable=True),
sa.CheckConstraint(
'-180 <= longitude AND longitude <= 180',
name=op.f('ck_addresses_on_longitude_between_180_degrees'),
),
sa.CheckConstraint(
'-90 <= latitude AND latitude <= 90',
name=op.f('ck_addresses_on_latitude_between_90_degrees'),
),
sa.CheckConstraint(
'0 <= floor AND floor <= 40', name=op.f('ck_addresses_on_realistic_floor'),
),
sa.CheckConstraint(
'30000 <= zip_code AND zip_code <= 99999',
name=op.f('ck_addresses_on_valid_zip_code'),
),
sa.ForeignKeyConstraint(
['city_id'],
[f'{config.CLEAN_SCHEMA}.cities.id'],
name=op.f('fk_addresses_to_cities_via_city_id'),
onupdate='RESTRICT',
ondelete='RESTRICT',
),
sa.ForeignKeyConstraint(
['primary_id'],
[f'{config.CLEAN_SCHEMA}.addresses.id'],
name=op.f('fk_addresses_to_addresses_via_primary_id'),
onupdate='RESTRICT',
ondelete='RESTRICT',
),
sa.PrimaryKeyConstraint('id', name=op.f('pk_addresses')),
*(
[ # noqa:WPS504
sa.ForeignKeyConstraint(
['id'],
[f'{config.ORIGINAL_SCHEMA}.addresses.id'],
name=op.f('pk_addresses_sanity'),
onupdate='RESTRICT',
ondelete='RESTRICT',
),
]
if not config.TESTING
else []
),
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_addresses_on_city_id'),
'addresses',
['city_id'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_addresses_on_place_id'),
'addresses',
['place_id'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_addresses_on_primary_id'),
'addresses',
['primary_id'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_addresses_on_zip_code'),
'addresses',
['zip_code'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_table( # noqa:ECE001
'restaurants',
sa.Column('id', sa.SmallInteger(), autoincrement=False, nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=False),
sa.Column('name', sa.Unicode(length=45), nullable=False), # noqa:WPS432
sa.Column('address_id', sa.Integer(), nullable=False),
sa.Column('estimated_prep_duration', sa.SmallInteger(), nullable=False),
sa.CheckConstraint(
'0 <= estimated_prep_duration AND estimated_prep_duration <= 2400',
name=op.f('ck_restaurants_on_realistic_estimated_prep_duration'),
),
sa.ForeignKeyConstraint(
['address_id'],
[f'{config.CLEAN_SCHEMA}.addresses.id'],
name=op.f('fk_restaurants_to_addresses_via_address_id'),
onupdate='RESTRICT',
ondelete='RESTRICT',
),
sa.PrimaryKeyConstraint('id', name=op.f('pk_restaurants')),
*(
[ # noqa:WPS504
sa.ForeignKeyConstraint(
['id'],
[f'{config.ORIGINAL_SCHEMA}.businesses.id'],
name=op.f('pk_restaurants_sanity'),
onupdate='RESTRICT',
ondelete='RESTRICT',
),
]
if not config.TESTING
else []
),
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_restaurants_on_address_id'),
'restaurants',
['address_id'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_table( # noqa:ECE001
'orders',
sa.Column('id', sa.Integer(), autoincrement=False, nullable=False),
sa.Column('delivery_id', sa.Integer(), nullable=True),
sa.Column('customer_id', sa.Integer(), nullable=False),
sa.Column('placed_at', sa.DateTime(), nullable=False),
sa.Column('ad_hoc', sa.Boolean(), nullable=False),
sa.Column('scheduled_delivery_at', sa.DateTime(), nullable=True),
sa.Column('scheduled_delivery_at_corrected', sa.Boolean(), nullable=True),
sa.Column('first_estimated_delivery_at', sa.DateTime(), nullable=True),
sa.Column('cancelled', sa.Boolean(), nullable=False),
sa.Column('cancelled_at', sa.DateTime(), nullable=True),
sa.Column('cancelled_at_corrected', sa.Boolean(), nullable=True),
sa.Column('sub_total', sa.Integer(), nullable=False),
sa.Column('delivery_fee', sa.SmallInteger(), nullable=False),
sa.Column('total', sa.Integer(), nullable=False),
sa.Column('restaurant_id', sa.SmallInteger(), nullable=False),
sa.Column('restaurant_notified_at', sa.DateTime(), nullable=True),
sa.Column('restaurant_notified_at_corrected', sa.Boolean(), nullable=True),
sa.Column('restaurant_confirmed_at', sa.DateTime(), nullable=True),
sa.Column('restaurant_confirmed_at_corrected', sa.Boolean(), nullable=True),
sa.Column('estimated_prep_duration', sa.Integer(), nullable=True),
sa.Column('estimated_prep_duration_corrected', sa.Boolean(), nullable=True),
sa.Column('estimated_prep_buffer', sa.Integer(), nullable=False),
sa.Column('courier_id', sa.Integer(), nullable=True),
sa.Column('dispatch_at', sa.DateTime(), nullable=True),
sa.Column('dispatch_at_corrected', sa.Boolean(), nullable=True),
sa.Column('courier_notified_at', sa.DateTime(), nullable=True),
sa.Column('courier_notified_at_corrected', sa.Boolean(), nullable=True),
sa.Column('courier_accepted_at', sa.DateTime(), nullable=True),
sa.Column('courier_accepted_at_corrected', sa.Boolean(), nullable=True),
sa.Column('utilization', sa.SmallInteger(), nullable=False),
sa.Column('pickup_address_id', sa.Integer(), nullable=False),
sa.Column('reached_pickup_at', sa.DateTime(), nullable=True),
sa.Column('pickup_at', sa.DateTime(), nullable=True),
sa.Column('pickup_at_corrected', sa.Boolean(), nullable=True),
sa.Column('pickup_not_confirmed', sa.Boolean(), nullable=True),
sa.Column('left_pickup_at', sa.DateTime(), nullable=True),
sa.Column('left_pickup_at_corrected', sa.Boolean(), nullable=True),
sa.Column('delivery_address_id', sa.Integer(), nullable=False),
sa.Column('reached_delivery_at', sa.DateTime(), nullable=True),
sa.Column('delivery_at', sa.DateTime(), nullable=True),
sa.Column('delivery_at_corrected', sa.Boolean(), nullable=True),
sa.Column('delivery_not_confirmed', sa.Boolean(), nullable=True),
sa.Column('courier_waited_at_delivery', sa.Boolean(), nullable=True),
sa.Column('logged_delivery_distance', sa.SmallInteger(), nullable=True),
sa.Column('logged_avg_speed', postgresql.DOUBLE_PRECISION(), nullable=True),
sa.Column('logged_avg_speed_distance', sa.SmallInteger(), nullable=True),
sa.CheckConstraint(
'0 <= estimated_prep_buffer AND estimated_prep_buffer <= 900',
name=op.f('ck_orders_on_estimated_prep_buffer_between_0_and_900'),
),
sa.CheckConstraint(
'0 <= estimated_prep_duration AND estimated_prep_duration <= 2700',
name=op.f('ck_orders_on_estimated_prep_duration_between_0_and_2700'),
),
sa.CheckConstraint(
'0 <= utilization AND utilization <= 100',
name=op.f('ck_orders_on_utilization_between_0_and_100'),
),
sa.CheckConstraint(
'(cancelled_at IS NULL AND cancelled_at_corrected IS NULL) OR (cancelled_at IS NULL AND cancelled_at_corrected IS TRUE) OR (cancelled_at IS NOT NULL AND cancelled_at_corrected IS NOT NULL)', # noqa:E501
name=op.f('ck_orders_on_corrections_only_for_set_value_1'),
),
sa.CheckConstraint(
'(courier_accepted_at IS NULL AND courier_accepted_at_corrected IS NULL) OR (courier_accepted_at IS NULL AND courier_accepted_at_corrected IS TRUE) OR (courier_accepted_at IS NOT NULL AND courier_accepted_at_corrected IS NOT NULL)', # noqa:E501
name=op.f('ck_orders_on_corrections_only_for_set_value_7'),
),
sa.CheckConstraint(
'(courier_notified_at IS NULL AND courier_notified_at_corrected IS NULL) OR (courier_notified_at IS NULL AND courier_notified_at_corrected IS TRUE) OR (courier_notified_at IS NOT NULL AND courier_notified_at_corrected IS NOT NULL)', # noqa:E501
name=op.f('ck_orders_on_corrections_only_for_set_value_6'),
),
sa.CheckConstraint(
'(delivery_at IS NULL AND delivery_at_corrected IS NULL) OR (delivery_at IS NULL AND delivery_at_corrected IS TRUE) OR (delivery_at IS NOT NULL AND delivery_at_corrected IS NOT NULL)', # noqa:E501
name=op.f('ck_orders_on_corrections_only_for_set_value_10'),
),
sa.CheckConstraint(
'(dispatch_at IS NULL AND dispatch_at_corrected IS NULL) OR (dispatch_at IS NULL AND dispatch_at_corrected IS TRUE) OR (dispatch_at IS NOT NULL AND dispatch_at_corrected IS NOT NULL)', # noqa:E501
name=op.f('ck_orders_on_corrections_only_for_set_value_5'),
),
sa.CheckConstraint(
'(estimated_prep_duration IS NULL AND estimated_prep_duration_corrected IS NULL) OR (estimated_prep_duration IS NULL AND estimated_prep_duration_corrected IS TRUE) OR (estimated_prep_duration IS NOT NULL AND estimated_prep_duration_corrected IS NOT NULL)', # noqa:E501
name=op.f('ck_orders_on_corrections_only_for_set_value_4'),
),
sa.CheckConstraint(
'(left_pickup_at IS NULL AND left_pickup_at_corrected IS NULL) OR (left_pickup_at IS NULL AND left_pickup_at_corrected IS TRUE) OR (left_pickup_at IS NOT NULL AND left_pickup_at_corrected IS NOT NULL)', # noqa:E501
name=op.f('ck_orders_on_corrections_only_for_set_value_9'),
),
sa.CheckConstraint(
'(pickup_at IS NULL AND pickup_at_corrected IS NULL) OR (pickup_at IS NULL AND pickup_at_corrected IS TRUE) OR (pickup_at IS NOT NULL AND pickup_at_corrected IS NOT NULL)', # noqa:E501
name=op.f('ck_orders_on_corrections_only_for_set_value_8'),
),
sa.CheckConstraint(
'(restaurant_confirmed_at IS NULL AND restaurant_confirmed_at_corrected IS NULL) OR (restaurant_confirmed_at IS NULL AND restaurant_confirmed_at_corrected IS TRUE) OR (restaurant_confirmed_at IS NOT NULL AND restaurant_confirmed_at_corrected IS NOT NULL)', # noqa:E501
name=op.f('ck_orders_on_corrections_only_for_set_value_3'),
),
sa.CheckConstraint(
'(restaurant_notified_at IS NULL AND restaurant_notified_at_corrected IS NULL) OR (restaurant_notified_at IS NULL AND restaurant_notified_at_corrected IS TRUE) OR (restaurant_notified_at IS NOT NULL AND restaurant_notified_at_corrected IS NOT NULL)', # noqa:E501
name=op.f('ck_orders_on_corrections_only_for_set_value_2'),
),
sa.CheckConstraint(
'(scheduled_delivery_at IS NULL AND scheduled_delivery_at_corrected IS NULL) OR (scheduled_delivery_at IS NULL AND scheduled_delivery_at_corrected IS TRUE) OR (scheduled_delivery_at IS NOT NULL AND scheduled_delivery_at_corrected IS NOT NULL)', # noqa:E501
name=op.f('ck_orders_on_corrections_only_for_set_value_0'),
),
sa.CheckConstraint(
'(ad_hoc IS TRUE AND scheduled_delivery_at IS NULL) OR (ad_hoc IS FALSE AND scheduled_delivery_at IS NOT NULL)', # noqa:E501
name=op.f('ck_orders_on_either_ad_hoc_or_scheduled_order'),
),
sa.CheckConstraint(
'NOT (EXTRACT(EPOCH FROM scheduled_delivery_at - placed_at) < 1800)',
name=op.f('ck_orders_on_scheduled_orders_not_within_30_minutes'),
),
sa.CheckConstraint(
'NOT (ad_hoc IS FALSE AND ((EXTRACT(HOUR FROM scheduled_delivery_at) <= 11 AND NOT (EXTRACT(HOUR FROM scheduled_delivery_at) = 11 AND EXTRACT(MINUTE FROM scheduled_delivery_at) = 45)) OR EXTRACT(HOUR FROM scheduled_delivery_at) > 22))', # noqa:E501
name=op.f('ck_orders_on_scheduled_orders_within_business_hours'),
),
sa.CheckConstraint(
'NOT (ad_hoc IS TRUE AND (EXTRACT(HOUR FROM placed_at) < 11 OR EXTRACT(HOUR FROM placed_at) > 22))', # noqa:E501
name=op.f('ck_orders_on_ad_hoc_orders_within_business_hours'),
),
sa.CheckConstraint(
'NOT (cancelled IS FALSE AND cancelled_at IS NOT NULL)',
name=op.f('ck_orders_on_only_cancelled_orders_may_have_cancelled_at'),
),
sa.CheckConstraint(
'NOT (cancelled IS TRUE AND delivery_at IS NOT NULL)',
name=op.f('ck_orders_on_cancelled_orders_must_not_be_delivered'),
),
sa.CheckConstraint(
'cancelled_at > courier_accepted_at',
name=op.f('ck_orders_on_ordered_timestamps_16'),
),
sa.CheckConstraint(
'cancelled_at > courier_notified_at',
name=op.f('ck_orders_on_ordered_timestamps_15'),
),
sa.CheckConstraint(
'cancelled_at > delivery_at',
name=op.f('ck_orders_on_ordered_timestamps_21'),
),
sa.CheckConstraint(
'cancelled_at > dispatch_at',
name=op.f('ck_orders_on_ordered_timestamps_14'),
),
sa.CheckConstraint(
'cancelled_at > left_pickup_at',
name=op.f('ck_orders_on_ordered_timestamps_19'),
),
sa.CheckConstraint(
'cancelled_at > pickup_at', name=op.f('ck_orders_on_ordered_timestamps_18'),
),
sa.CheckConstraint(
'cancelled_at > reached_delivery_at',
name=op.f('ck_orders_on_ordered_timestamps_20'),
),
sa.CheckConstraint(
'cancelled_at > reached_pickup_at',
name=op.f('ck_orders_on_ordered_timestamps_17'),
),
sa.CheckConstraint(
'cancelled_at > restaurant_confirmed_at',
name=op.f('ck_orders_on_ordered_timestamps_13'),
),
sa.CheckConstraint(
'cancelled_at > restaurant_notified_at',
name=op.f('ck_orders_on_ordered_timestamps_12'),
),
sa.CheckConstraint(
'courier_accepted_at < delivery_at',
name=op.f('ck_orders_on_ordered_timestamps_42'),
),
sa.CheckConstraint(
'courier_accepted_at < left_pickup_at',
name=op.f('ck_orders_on_ordered_timestamps_40'),
),
sa.CheckConstraint(
'courier_accepted_at < pickup_at',
name=op.f('ck_orders_on_ordered_timestamps_39'),
),
sa.CheckConstraint(
'courier_accepted_at < reached_delivery_at',
name=op.f('ck_orders_on_ordered_timestamps_41'),
),
sa.CheckConstraint(
'courier_accepted_at < reached_pickup_at',
name=op.f('ck_orders_on_ordered_timestamps_38'),
),
sa.CheckConstraint(
'courier_notified_at < courier_accepted_at',
name=op.f('ck_orders_on_ordered_timestamps_32'),
),
sa.CheckConstraint(
'courier_notified_at < delivery_at',
name=op.f('ck_orders_on_ordered_timestamps_37'),
),
sa.CheckConstraint(
'courier_notified_at < left_pickup_at',
name=op.f('ck_orders_on_ordered_timestamps_35'),
),
sa.CheckConstraint(
'courier_notified_at < pickup_at',
name=op.f('ck_orders_on_ordered_timestamps_34'),
),
sa.CheckConstraint(
'courier_notified_at < reached_delivery_at',
name=op.f('ck_orders_on_ordered_timestamps_36'),
),
sa.CheckConstraint(
'courier_notified_at < reached_pickup_at',
name=op.f('ck_orders_on_ordered_timestamps_33'),
),
sa.CheckConstraint(
'dispatch_at < courier_accepted_at',
name=op.f('ck_orders_on_ordered_timestamps_26'),
),
sa.CheckConstraint(
'dispatch_at < courier_notified_at',
name=op.f('ck_orders_on_ordered_timestamps_25'),
),
sa.CheckConstraint(
'dispatch_at < delivery_at',
name=op.f('ck_orders_on_ordered_timestamps_31'),
),
sa.CheckConstraint(
'dispatch_at < left_pickup_at',
name=op.f('ck_orders_on_ordered_timestamps_29'),
),
sa.CheckConstraint(
'dispatch_at < pickup_at', name=op.f('ck_orders_on_ordered_timestamps_28'),
),
sa.CheckConstraint(
'dispatch_at < reached_delivery_at',
name=op.f('ck_orders_on_ordered_timestamps_30'),
),
sa.CheckConstraint(
'dispatch_at < reached_pickup_at',
name=op.f('ck_orders_on_ordered_timestamps_27'),
),
sa.CheckConstraint(
'estimated_prep_buffer % 60 = 0',
name=op.f('ck_orders_on_estimated_prep_buffer_must_be_whole_minutes'),
),
sa.CheckConstraint(
'estimated_prep_duration % 60 = 0',
name=op.f('ck_orders_on_estimated_prep_duration_must_be_whole_minutes'),
),
sa.CheckConstraint(
'left_pickup_at < delivery_at',
name=op.f('ck_orders_on_ordered_timestamps_51'),
),
sa.CheckConstraint(
'left_pickup_at < reached_delivery_at',
name=op.f('ck_orders_on_ordered_timestamps_50'),
),
sa.CheckConstraint(
'pickup_at < delivery_at', name=op.f('ck_orders_on_ordered_timestamps_49'),
),
sa.CheckConstraint(
'pickup_at < left_pickup_at',
name=op.f('ck_orders_on_ordered_timestamps_47'),
),
sa.CheckConstraint(
'pickup_at < reached_delivery_at',
name=op.f('ck_orders_on_ordered_timestamps_48'),
),
sa.CheckConstraint(
'placed_at < cancelled_at', name=op.f('ck_orders_on_ordered_timestamps_2'),
),
sa.CheckConstraint(
'placed_at < courier_accepted_at',
name=op.f('ck_orders_on_ordered_timestamps_7'),
),
sa.CheckConstraint(
'placed_at < courier_notified_at',
name=op.f('ck_orders_on_ordered_timestamps_6'),
),
sa.CheckConstraint(
'placed_at < delivery_at', name=op.f('ck_orders_on_ordered_timestamps_11'),
),
sa.CheckConstraint(
'placed_at < dispatch_at', name=op.f('ck_orders_on_ordered_timestamps_5'),
),
sa.CheckConstraint(
'placed_at < first_estimated_delivery_at',
name=op.f('ck_orders_on_ordered_timestamps_1'),
),
sa.CheckConstraint(
'placed_at < left_pickup_at',
name=op.f('ck_orders_on_ordered_timestamps_9'),
),
sa.CheckConstraint(
'placed_at < reached_delivery_at',
name=op.f('ck_orders_on_ordered_timestamps_10'),
),
sa.CheckConstraint(
'placed_at < reached_pickup_at',
name=op.f('ck_orders_on_ordered_timestamps_8'),
),
sa.CheckConstraint(
'placed_at < restaurant_confirmed_at',
name=op.f('ck_orders_on_ordered_timestamps_4'),
),
sa.CheckConstraint(
'placed_at < restaurant_notified_at',
name=op.f('ck_orders_on_ordered_timestamps_3'),
),
sa.CheckConstraint(
'placed_at < scheduled_delivery_at',
name=op.f('ck_orders_on_ordered_timestamps_0'),
),
sa.CheckConstraint(
'reached_delivery_at < delivery_at',
name=op.f('ck_orders_on_ordered_timestamps_52'),
),
sa.CheckConstraint(
'reached_pickup_at < delivery_at',
name=op.f('ck_orders_on_ordered_timestamps_46'),
),
sa.CheckConstraint(
'reached_pickup_at < left_pickup_at',
name=op.f('ck_orders_on_ordered_timestamps_44'),
),
sa.CheckConstraint(
'reached_pickup_at < pickup_at',
name=op.f('ck_orders_on_ordered_timestamps_43'),
),
sa.CheckConstraint(
'reached_pickup_at < reached_delivery_at',
name=op.f('ck_orders_on_ordered_timestamps_45'),
),
sa.CheckConstraint(
'restaurant_confirmed_at < pickup_at',
name=op.f('ck_orders_on_ordered_timestamps_24'),
),
sa.CheckConstraint(
'restaurant_notified_at < pickup_at',
name=op.f('ck_orders_on_ordered_timestamps_23'),
),
sa.CheckConstraint(
'restaurant_notified_at < restaurant_confirmed_at',
name=op.f('ck_orders_on_ordered_timestamps_22'),
),
sa.CheckConstraint(
'(pickup_at IS NULL AND pickup_not_confirmed IS NULL) OR (pickup_at IS NOT NULL AND pickup_not_confirmed IS NOT NULL)', # noqa:E501
name=op.f('pickup_not_confirmed_only_if_pickup'),
),
sa.CheckConstraint(
'(delivery_at IS NULL AND delivery_not_confirmed IS NULL) OR (delivery_at IS NOT NULL AND delivery_not_confirmed IS NOT NULL)', # noqa:E501
name=op.f('delivery_not_confirmed_only_if_delivery'),
),
sa.CheckConstraint(
'(delivery_at IS NULL AND courier_waited_at_delivery IS NULL) OR (delivery_at IS NOT NULL AND courier_waited_at_delivery IS NOT NULL)', # noqa:E501
name=op.f('courier_waited_at_delivery_only_if_delivery'),
),
sa.ForeignKeyConstraint(
['courier_id'],
[f'{config.CLEAN_SCHEMA}.couriers.id'],
name=op.f('fk_orders_to_couriers_via_courier_id'),
onupdate='RESTRICT',
ondelete='RESTRICT',
),
sa.ForeignKeyConstraint(
['customer_id'],
[f'{config.CLEAN_SCHEMA}.customers.id'],
name=op.f('fk_orders_to_customers_via_customer_id'),
onupdate='RESTRICT',
ondelete='RESTRICT',
),
sa.ForeignKeyConstraint(
['delivery_address_id'],
[f'{config.CLEAN_SCHEMA}.addresses.id'],
name=op.f('fk_orders_to_addresses_via_delivery_address_id'),
onupdate='RESTRICT',
ondelete='RESTRICT',
),
sa.ForeignKeyConstraint(
['pickup_address_id'],
[f'{config.CLEAN_SCHEMA}.addresses.id'],
name=op.f('fk_orders_to_addresses_via_pickup_address_id'),
onupdate='RESTRICT',
ondelete='RESTRICT',
),
sa.ForeignKeyConstraint(
['restaurant_id'],
[f'{config.CLEAN_SCHEMA}.restaurants.id'],
name=op.f('fk_orders_to_restaurants_via_restaurant_id'),
onupdate='RESTRICT',
ondelete='RESTRICT',
),
sa.PrimaryKeyConstraint('id', name=op.f('pk_orders')),
*(
[ # noqa:WPS504
sa.ForeignKeyConstraint(
['id'],
[f'{config.ORIGINAL_SCHEMA}.orders.id'],
name=op.f('pk_orders_sanity'),
onupdate='RESTRICT',
ondelete='RESTRICT',
),
sa.ForeignKeyConstraint(
['delivery_id'],
[f'{config.ORIGINAL_SCHEMA}.deliveries.id'],
name=op.f('pk_deliveries_sanity'),
onupdate='RESTRICT',
ondelete='RESTRICT',
),
]
if not config.TESTING
else []
),
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_orders_on_cancelled'),
'orders',
['cancelled'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_orders_on_cancelled_at_corrected'),
'orders',
['cancelled_at_corrected'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_orders_on_courier_accepted_at_corrected'),
'orders',
['courier_accepted_at_corrected'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_orders_on_courier_id'),
'orders',
['courier_id'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_orders_on_courier_notified_at_corrected'),
'orders',
['courier_notified_at_corrected'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_orders_on_customer_id'),
'orders',
['customer_id'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_orders_on_delivery_address_id'),
'orders',
['delivery_address_id'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_orders_on_delivery_at_corrected'),
'orders',
['delivery_at_corrected'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_orders_on_delivery_id'),
'orders',
['delivery_id'],
unique=True,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_orders_on_dispatch_at_corrected'),
'orders',
['dispatch_at_corrected'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_orders_on_estimated_prep_buffer'),
'orders',
['estimated_prep_buffer'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_orders_on_estimated_prep_duration'),
'orders',
['estimated_prep_duration'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_orders_on_estimated_prep_duration_corrected'),
'orders',
['estimated_prep_duration_corrected'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_orders_on_left_pickup_at_corrected'),
'orders',
['left_pickup_at_corrected'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_orders_on_pickup_address_id'),
'orders',
['pickup_address_id'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_orders_on_pickup_at_corrected'),
'orders',
['pickup_at_corrected'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_orders_on_placed_at'),
'orders',
['placed_at'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_orders_on_restaurant_confirmed_at_corrected'),
'orders',
['restaurant_confirmed_at_corrected'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_orders_on_restaurant_id'),
'orders',
['restaurant_id'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_orders_on_restaurant_notified_at_corrected'),
'orders',
['restaurant_notified_at_corrected'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_orders_on_scheduled_delivery_at'),
'orders',
['scheduled_delivery_at'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
op.create_index(
op.f('ix_orders_on_scheduled_delivery_at_corrected'),
'orders',
['scheduled_delivery_at_corrected'],
unique=False,
schema=config.CLEAN_SCHEMA,
)
def downgrade():
"""Downgrade to revision None."""
op.execute(f'DROP SCHEMA {config.CLEAN_SCHEMA} CASCADE;')

View file

@ -27,7 +27,7 @@ as unified tasks to assure the quality of the source code:
=> may be paths or options
GitHub Actions implements a CI workflow:
GitHub Actions implements the following CI workflow:
- "format", "lint", and "test" as above
@ -36,30 +36,31 @@ GitHub Actions implements a CI workflow:
- "docs": build the documentation with sphinx
The pre-commit framework invokes the "pre-commit" and "pre-merge" sessions:
The pre-commit framework invokes the following tasks:
- "pre-commit" before all commits:
- before any commit:
+ triggers "format" and "lint" on staged source files
+ => test coverage may be < 100%
+ "format" and "lint" as above
+ "fix-branch-references": replace branch references with the current one
- "pre-merge" before all merges and pushes:
+ same as "pre-commit"
+ plus: triggers "test", "safety", and "docs" (that ignore extra arguments)
+ => test coverage is enforced to be 100%
- before merges: run the entire "test-suite" independent of the file changes
"""
import contextlib
import glob
import os
import re
import shutil
import subprocess # noqa:S404
import tempfile
from typing import Generator, IO, Tuple
import nox
from nox.sessions import Session
GITHUB_REPOSITORY = 'webartifex/urban-meal-delivery'
PACKAGE_IMPORT_NAME = 'urban_meal_delivery'
# Docs/sphinx locations.
@ -74,7 +75,9 @@ PYTEST_LOCATION = 'tests/'
# Paths with all *.py files.
SRC_LOCATIONS = (
f'{DOCS_SRC}/conf.py',
f'{DOCS_SRC}conf.py',
'migrations/env.py',
'migrations/versions/',
'noxfile.py',
PACKAGE_SOURCE_LOCATION,
PYTEST_LOCATION,
@ -192,23 +195,6 @@ def lint(session):
)
@nox.session(name='pre-commit', python=PYTHON, venv_backend='none')
def pre_commit(session):
"""Run the format and lint sessions.
Source files must be well-formed before they enter git.
Intended to be run as a pre-commit hook.
Passed in extra arguments are forwarded. So, if it is run as a pre-commit
hook, only the currently staged source files are formatted and linted.
"""
# "format" and "lint" are run in sessions on their own as
# session.notify() creates new Session objects.
session.notify('format')
session.notify('lint')
@nox.session(python=PYTHON)
def test(session):
"""Test the code base.
@ -235,7 +221,12 @@ def test(session):
# non-develop dependencies be installed in the virtual environment.
session.run('poetry', 'install', '--no-dev', external=True)
_install_packages(
session, 'packaging', 'pytest', 'pytest-cov', 'xdoctest[optional]',
session,
'packaging',
'pytest',
'pytest-cov',
'pytest-env',
'xdoctest[optional]',
)
# Interpret extra arguments as options for pytest.
@ -249,6 +240,8 @@ def test(session):
'--cov-branch',
'--cov-fail-under=100',
'--cov-report=term-missing:skip-covered',
'-k',
'not e2e',
PYTEST_LOCATION,
)
session.run('pytest', '--version')
@ -306,28 +299,22 @@ def docs(session):
print(f'Docs are available at {os.getcwd()}/{DOCS_BUILD}index.html') # noqa:WPS421
@nox.session(name='pre-merge', python=PYTHON)
def pre_merge(session):
"""Run the format, lint, test, safety, and docs sessions.
@nox.session(name='test-suite', python=PYTHON)
def test_suite(session):
"""Run the entire test suite.
Intended to be run either as a pre-merge or pre-push hook.
Intended to be run as a pre-commit hook.
Ignores the paths passed in by the pre-commit framework
for the test, safety, and docs sessions so that the
entire test suite is executed.
and runs the entire test suite.
"""
# Re-using an old environment is not so easy here as the "test" session
# runs `poetry install --no-dev`, which removes previously installed packages.
if session.virtualenv.reuse_existing:
raise RuntimeError(
'The "pre-merge" session must be run without the "-r" option',
'The "test-suite" session must be run without the "-r" option',
)
session.notify('format')
session.notify('lint')
session.notify('safety')
session.notify('docs')
# Little hack to not work with the extra arguments provided
# by the pre-commit framework. Create a flag in the
# env(ironment) that must contain only `str`-like objects.
@ -340,6 +327,116 @@ def pre_merge(session):
test(session)
@nox.session(name='fix-branch-references', python=PYTHON, venv_backend='none')
def fix_branch_references(session): # noqa:WPS210
"""Replace branch references with the current branch.
Intended to be run as a pre-commit hook.
Many files in the project (e.g., README.md) contain links to resources
on github.com or nbviewer.jupyter.org that contain branch labels.
This task rewrites these links such that they contain the branch reference
of the current branch. If the branch is only a temporary one that is to be
merged into the 'main' branch, all references are adjusted to 'main' as well.
This task may be called with one positional argument that is interpreted
as the branch to which all references are changed into.
The format must be "--branch=BRANCH_NAME".
"""
# Adjust this to add/remove glob patterns
# whose links are re-written.
paths = ['*.md', '**/*.md', '**/*.ipynb']
# Get the branch git is currently on.
# This is the branch to which all references are changed into
# if none of the two exceptions below apply.
branch = (
subprocess.check_output( # noqa:S603
('git', 'rev-parse', '--abbrev-ref', 'HEAD'),
)
.decode()
.strip()
)
# If the current branch is only a temporary one that is to be merged
# into 'main', we adjust all branch references to 'main' as well.
if branch.startswith('release') or branch.startswith('research'):
branch = 'main'
# If a "--branch=BRANCH_NAME" argument is passed in
# as the only positional argument, we use BRANCH_NAME.
# Note: The --branch is required as session.posargs contains
# the staged files passed in by pre-commit in most cases.
if session.posargs and len(session.posargs) == 1:
match = re.match(
pattern=r'^--branch=([\w\.-]+)$', string=session.posargs[0].strip(),
)
if match:
branch = match.groups()[0]
rewrites = [
{
'name': 'github',
'pattern': re.compile(
fr'((((http)|(https))://github\.com/{GITHUB_REPOSITORY}/((blob)|(tree))/)([\w\.-]+)/)', # noqa:E501
),
'replacement': fr'\2{branch}/',
},
{
'name': 'nbviewer',
'pattern': re.compile(
fr'((((http)|(https))://nbviewer\.jupyter\.org/github/{GITHUB_REPOSITORY}/((blob)|(tree))/)([\w\.-]+)/)', # noqa:E501
),
'replacement': fr'\2{branch}/',
},
]
for expanded in _expand(*paths):
with _line_by_line_replace(expanded) as (old_file, new_file):
for line in old_file:
for rewrite in rewrites:
line = re.sub(rewrite['pattern'], rewrite['replacement'], line)
new_file.write(line)
def _expand(*patterns: str) -> Generator[str, None, None]:
"""Expand glob patterns into paths.
Args:
*patterns: the patterns to be expanded
Yields:
expanded: a single expanded path
""" # noqa:RST213
for pattern in patterns:
yield from glob.glob(pattern.strip())
@contextlib.contextmanager
def _line_by_line_replace(path: str) -> Generator[Tuple[IO, IO], None, None]:
"""Replace/change the lines in a file one by one.
This generator function yields two file handles, one to the current file
(i.e., `old_file`) and one to its replacement (i.e., `new_file`).
Usage: loop over the lines in `old_file` and write the files to be kept
to `new_file`. Files not written to `new_file` are removed!
Args:
path: the file whose lines are to be replaced
Yields:
old_file, new_file: handles to a file and its replacement
"""
file_handle, new_file_path = tempfile.mkstemp()
with os.fdopen(file_handle, 'w') as new_file:
with open(path) as old_file:
yield old_file, new_file
shutil.copymode(path, new_file_path)
os.remove(path)
shutil.move(new_file_path, path)
@nox.session(name='init-project', python=PYTHON, venv_backend='none')
def init_project(session):
"""Install the pre-commit hooks."""
@ -348,25 +445,27 @@ def init_project(session):
@nox.session(name='clean-pwd', python=PYTHON, venv_backend='none')
def clean_pwd(session):
def clean_pwd(session): # noqa:WPS210,WPS231
"""Remove (almost) all glob patterns listed in .gitignore.
The difference compared to `git clean -X` is that this task
does not remove pyenv's .python-version file and poetry's
virtual environment.
"""
exclude = frozenset(('.python-version', '.venv', 'venv'))
exclude = frozenset(('.env', '.python-version', '.venv/', 'venv/'))
with open('.gitignore') as file_handle:
paths = file_handle.readlines()
for path in paths:
path = path.strip()
if path.startswith('#') or path in exclude:
for path in _expand(*paths):
if path.startswith('#'):
continue
for expanded in glob.glob(path):
session.run(f'rm -rf {expanded}')
for excluded in exclude:
if path.startswith(excluded):
break
else:
session.run('rm', '-rf', path)
def _begin(session):
@ -428,11 +527,11 @@ def _install_packages(session: Session, *packages_or_pip_args: str, **kwargs) ->
# TODO (isort): Remove this fix after
# upgrading to isort ^5.3.0 in pyproject.toml.
# upgrading to isort ^5.5.4 in pyproject.toml.
@contextlib.contextmanager
def _isort_fix(session):
"""Temporarily upgrade to isort 5.3.0."""
session.install('isort==5.3.0')
"""Temporarily upgrade to isort 5.5.4."""
session.install('isort==5.5.4')
try:
yield
finally:

1399
poetry.lock generated

File diff suppressed because it is too large Load diff

View file

@ -9,7 +9,7 @@ target-version = ["py38"]
[tool.poetry]
name = "urban-meal-delivery"
version = "0.1.0"
version = "0.2.0"
authors = ["Alexander Hess <alexander@webartifex.biz>"]
description = "Optimizing an urban meal delivery platform"
@ -27,7 +27,29 @@ repository = "https://github.com/webartifex/urban-meal-delivery"
[tool.poetry.dependencies]
python = "^3.8"
# Package => code developed in *.py files and packaged under src/urban_meal_delivery
alembic = "^1.4.2"
click = "^7.1.2"
psycopg2 = "^2.8.5" # adapter for PostgreSQL
python-dotenv = "^0.14.0"
sqlalchemy = "^1.3.18"
# Jupyter Lab => notebooks with analyses using the developed package
# IMPORTANT: must be kept in sync with the "research" extra below
jupyterlab = { version="^2.2.2", optional=true }
nb_black = { version="^1.0.7", optional=true }
numpy = { version="^1.19.1", optional=true }
pandas = { version="^1.1.0", optional=true }
pytz = { version="^2020.1", optional=true }
[tool.poetry.extras]
research = [
"jupyterlab",
"nb_black",
"numpy",
"pandas",
"pytz",
]
[tool.poetry.dev-dependencies]
# Task Runners
@ -37,7 +59,7 @@ pre-commit = "^2.6.0"
# Code Formatters
autoflake = "^1.3.1"
black = "^19.10b0"
isort = "^4.3.21" # TODO (isort): not ^5.2.2 due to pylint and wemake-python-styleguide
isort = "^4.3.21" # TODO (isort): not ^5.5.4 due to wemake-python-styleguide
# (Static) Code Analyzers
flake8 = "^3.8.3"
@ -53,6 +75,7 @@ wemake-python-styleguide = "^0.14.1" # flake8 plug-in
packaging = "^20.4" # used to test the packaged version
pytest = "^6.0.1"
pytest-cov = "^2.10.0"
pytest-env = "^0.6.2"
xdoctest = { version="^0.13.0", extras=["optional"] }
# Documentation

View file

@ -84,9 +84,11 @@ ignore =
# If --ignore is passed on the command
# line, still ignore the following:
extend-ignore =
# Too long line => duplicate with E501.
B950,
# Comply with black's style.
# Source: https://github.com/psf/black/blob/master/docs/compatible_configs.md#flake8
E203, W503,
E203, W503, WPS348,
# f-strings are ok.
WPS305,
# Classes should not have to specify a base class.
@ -100,6 +102,20 @@ per-file-ignores =
docs/conf.py:
# Allow shadowing built-ins and reading __*__ variables.
WPS125,WPS609,
migrations/env.py:
# Type annotations are not strictly enforced.
ANN0, ANN2,
migrations/versions/*.py:
# Type annotations are not strictly enforced.
ANN0, ANN2,
# File names of revisions are ok.
WPS114,WPS118,
# Revisions may have too many expressions.
WPS204,WPS213,
# No overuse of string constants (e.g., 'RESTRICT').
WPS226,
# Too many noqa's are ok.
WPS402,
noxfile.py:
# Type annotations are not strictly enforced.
ANN0, ANN2,
@ -109,6 +125,17 @@ per-file-ignores =
WPS213,
# No overuse of string constants (e.g., '--version').
WPS226,
# The noxfile is rather long => allow many noqa's.
WPS402,
src/urban_meal_delivery/configuration.py:
# Allow upper case class variables within classes.
WPS115,
# Numbers are normal in config files.
WPS432,
src/urban_meal_delivery/db/addresses.py:
WPS226,
src/urban_meal_delivery/db/orders.py:
WPS226,
tests/*.py:
# Type annotations are not strictly enforced.
ANN0, ANN2,
@ -116,8 +143,12 @@ per-file-ignores =
S101,
# Shadowing outer scopes occurs naturally with mocks.
WPS442,
# Modules may have many test cases.
WPS202,WPS204,WPS214,
# No overuse of string constants (e.g., '__version__').
WPS226,
# Numbers are normal in test cases as expected results.
WPS432,
# Explicitly set mccabe's maximum complexity to 10 as recommended by
# Thomas McCabe, the inventor of the McCabe complexity, and the NIST.
@ -135,10 +166,10 @@ show-source = true
# wemake-python-styleguide's settings
# ===================================
allowed-domain-names =
obj,
param,
result,
value,
min-name-length = 3
max-name-length = 40
# darglint
strictness = long
@ -186,7 +217,15 @@ single_line_exclusions = typing
[mypy]
cache_dir = .cache/mypy
[mypy-nox.*,packaging,pytest]
[mypy-dotenv]
ignore_missing_imports = true
[mypy-nox.*]
ignore_missing_imports = true
[mypy-packaging]
ignore_missing_imports = true
[mypy-pytest]
ignore_missing_imports = true
[mypy-sqlalchemy.*]
ignore_missing_imports = true
@ -199,6 +238,9 @@ disable =
# We use TODO's to indicate locations in the source base
# that must be worked on in the near future.
fixme,
# Too many false positives and cannot be disabled within a file.
# Source: https://github.com/PyCQA/pylint/issues/214
duplicate-code,
# Comply with black's style.
bad-continuation, bad-whitespace,
# =====================
@ -229,3 +271,7 @@ addopts =
--strict-markers
cache_dir = .cache/pytest
console_output_style = count
env =
TESTING=true
markers =
e2e: integration tests, inlc., for example, tests touching a database

View file

@ -6,8 +6,11 @@ Example:
True
"""
import os as _os
from importlib import metadata as _metadata
from urban_meal_delivery import configuration as _configuration
try:
_pkg_info = _metadata.metadata(__name__)
@ -21,3 +24,14 @@ else:
__author__ = _pkg_info['author']
__pkg_name__ = _pkg_info['name']
__version__ = _pkg_info['version']
# Global `config` object to be used in the package.
config: _configuration.Config = _configuration.make_config(
'testing' if _os.getenv('TESTING') else 'production',
)
# Import `db` down here as it depends on `config`.
# pylint:disable=wrong-import-position
from urban_meal_delivery import db # noqa:E402,F401 isort:skip

View file

@ -0,0 +1,101 @@
"""Provide package-wide configuration.
This module provides utils to create new `Config` objects
on the fly, mainly for testing and migrating!
Within this package, use the `config` proxy at the package's top level
to access the current configuration!
"""
import datetime
import os
import random
import string
import warnings
import dotenv
dotenv.load_dotenv()
def random_schema_name() -> str:
"""Generate a random PostgreSQL schema name for testing."""
return 'temp_{name}'.format(
name=''.join(
(random.choice(string.ascii_lowercase) for _ in range(10)), # noqa:S311
),
)
class Config:
"""Configuration that applies in all situations."""
# pylint:disable=too-few-public-methods
CUTOFF_DAY = datetime.datetime(2017, 2, 1)
# If a scheduled pre-order is made within this
# time horizon, we treat it as an ad-hoc order.
QUASI_AD_HOC_LIMIT = datetime.timedelta(minutes=45)
DATABASE_URI = os.getenv('DATABASE_URI')
# The PostgreSQL schema that holds the tables with the original data.
ORIGINAL_SCHEMA = os.getenv('ORIGINAL_SCHEMA') or 'public'
# The PostgreSQL schema that holds the tables with the cleaned data.
CLEAN_SCHEMA = os.getenv('CLEAN_SCHEMA') or 'clean'
ALEMBIC_TABLE = 'alembic_version'
ALEMBIC_TABLE_SCHEMA = 'public'
def __repr__(self) -> str:
"""Non-literal text representation."""
return '<configuration>'
class ProductionConfig(Config):
"""Configuration for the real dataset."""
# pylint:disable=too-few-public-methods
TESTING = False
class TestingConfig(Config):
"""Configuration for the test suite."""
# pylint:disable=too-few-public-methods
TESTING = True
DATABASE_URI = os.getenv('DATABASE_URI_TESTING') or Config.DATABASE_URI
CLEAN_SCHEMA = os.getenv('CLEAN_SCHEMA_TESTING') or random_schema_name()
def make_config(env: str = 'production') -> Config:
"""Create a new `Config` object.
Args:
env: either 'production' or 'testing'; defaults to the first
Returns:
config: a namespace with all configurations
Raises:
ValueError: if `env` is not as specified
""" # noqa:DAR203
config: Config
if env.strip().lower() == 'production':
config = ProductionConfig()
elif env.strip().lower() == 'testing':
config = TestingConfig()
else:
raise ValueError("Must be either 'production' or 'testing'")
# Without a PostgreSQL database the package cannot work.
if config.DATABASE_URI is None:
warnings.warn('Bad configurartion: no DATABASE_URI set in the environment')
return config

View file

@ -0,0 +1,11 @@
"""Provide the ORM models and a connection to the database."""
from urban_meal_delivery.db.addresses import Address # noqa:F401
from urban_meal_delivery.db.cities import City # noqa:F401
from urban_meal_delivery.db.connection import make_engine # noqa:F401
from urban_meal_delivery.db.connection import make_session_factory # noqa:F401
from urban_meal_delivery.db.couriers import Courier # noqa:F401
from urban_meal_delivery.db.customers import Customer # noqa:F401
from urban_meal_delivery.db.meta import Base # noqa:F401
from urban_meal_delivery.db.orders import Order # noqa:F401
from urban_meal_delivery.db.restaurants import Restaurant # noqa:F401

View file

@ -0,0 +1,82 @@
"""Provide the ORM's Address model."""
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.dialects import postgresql
from sqlalchemy.ext import hybrid
from urban_meal_delivery.db import meta
class Address(meta.Base):
"""An Address of a Customer or a Restaurant on the UDP."""
__tablename__ = 'addresses'
# Columns
id = sa.Column(sa.Integer, primary_key=True, autoincrement=False) # noqa:WPS125
_primary_id = sa.Column('primary_id', sa.Integer, nullable=False, index=True)
created_at = sa.Column(sa.DateTime, nullable=False)
place_id = sa.Column(
sa.Unicode(length=120), nullable=False, index=True, # noqa:WPS432
)
latitude = sa.Column(postgresql.DOUBLE_PRECISION, nullable=False)
longitude = sa.Column(postgresql.DOUBLE_PRECISION, nullable=False)
_city_id = sa.Column('city_id', sa.SmallInteger, nullable=False, index=True)
city_name = sa.Column('city', sa.Unicode(length=25), nullable=False) # noqa:WPS432
zip_code = sa.Column(sa.Integer, nullable=False, index=True)
street = sa.Column(sa.Unicode(length=80), nullable=False) # noqa:WPS432
floor = sa.Column(sa.SmallInteger)
# Constraints
__table_args__ = (
sa.ForeignKeyConstraint(
['primary_id'], ['addresses.id'], onupdate='RESTRICT', ondelete='RESTRICT',
),
sa.ForeignKeyConstraint(
['city_id'], ['cities.id'], onupdate='RESTRICT', ondelete='RESTRICT',
),
sa.CheckConstraint(
'-90 <= latitude AND latitude <= 90', name='latitude_between_90_degrees',
),
sa.CheckConstraint(
'-180 <= longitude AND longitude <= 180',
name='longitude_between_180_degrees',
),
sa.CheckConstraint(
'30000 <= zip_code AND zip_code <= 99999', name='valid_zip_code',
),
sa.CheckConstraint('0 <= floor AND floor <= 40', name='realistic_floor'),
)
# Relationships
city = orm.relationship('City', back_populates='addresses')
restaurant = orm.relationship('Restaurant', back_populates='address', uselist=False)
orders_picked_up = orm.relationship(
'Order',
back_populates='pickup_address',
foreign_keys='[Order._pickup_address_id]',
)
orders_delivered = orm.relationship(
'Order',
back_populates='delivery_address',
foreign_keys='[Order._delivery_address_id]',
)
def __repr__(self) -> str:
"""Non-literal text representation."""
return '<{cls}({street} in {city})>'.format(
cls=self.__class__.__name__, street=self.street, city=self.city_name,
)
@hybrid.hybrid_property
def is_primary(self) -> bool:
"""If an Address object is the earliest one entered at its location.
Street addresses may have been entered several times with different
versions/spellings of the street name and/or different floors.
`is_primary` indicates the first in a group of addresses.
"""
return self.id == self._primary_id

View file

@ -0,0 +1,83 @@
"""Provide the ORM's City model."""
from typing import Dict
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.dialects import postgresql
from urban_meal_delivery.db import meta
class City(meta.Base):
"""A City where the UDP operates in."""
__tablename__ = 'cities'
# Generic columns
id = sa.Column( # noqa:WPS125
sa.SmallInteger, primary_key=True, autoincrement=False,
)
name = sa.Column(sa.Unicode(length=10), nullable=False)
kml = sa.Column(sa.UnicodeText, nullable=False)
# Google Maps related columns
_center_latitude = sa.Column(
'center_latitude', postgresql.DOUBLE_PRECISION, nullable=False,
)
_center_longitude = sa.Column(
'center_longitude', postgresql.DOUBLE_PRECISION, nullable=False,
)
_northeast_latitude = sa.Column(
'northeast_latitude', postgresql.DOUBLE_PRECISION, nullable=False,
)
_northeast_longitude = sa.Column(
'northeast_longitude', postgresql.DOUBLE_PRECISION, nullable=False,
)
_southwest_latitude = sa.Column(
'southwest_latitude', postgresql.DOUBLE_PRECISION, nullable=False,
)
_southwest_longitude = sa.Column(
'southwest_longitude', postgresql.DOUBLE_PRECISION, nullable=False,
)
initial_zoom = sa.Column(sa.SmallInteger, nullable=False)
# Relationships
addresses = orm.relationship('Address', back_populates='city')
def __repr__(self) -> str:
"""Non-literal text representation."""
return '<{cls}({name})>'.format(cls=self.__class__.__name__, name=self.name)
@property
def location(self) -> Dict[str, float]:
"""GPS location of the city's center.
Example:
{"latitude": 48.856614, "longitude": 2.3522219}
"""
return {
'latitude': self._center_latitude,
'longitude': self._center_longitude,
}
@property
def viewport(self) -> Dict[str, Dict[str, float]]:
"""Google Maps viewport of the city.
Example:
{
'northeast': {'latitude': 48.9021449, 'longitude': 2.4699208},
'southwest': {'latitude': 48.815573, 'longitude': 2.225193},
}
""" # noqa:RST203
return {
'northeast': {
'latitude': self._northeast_latitude,
'longitude': self._northeast_longitude,
},
'southwest': {
'latitude': self._southwest_latitude,
'longitude': self._southwest_longitude,
},
}

View file

@ -0,0 +1,17 @@
"""Provide connection utils for the ORM layer."""
import sqlalchemy as sa
from sqlalchemy import engine
from sqlalchemy import orm
import urban_meal_delivery
def make_engine() -> engine.Engine: # pragma: no cover
"""Provide a configured Engine object."""
return sa.create_engine(urban_meal_delivery.config.DATABASE_URI)
def make_session_factory() -> orm.Session: # pragma: no cover
"""Provide a configured Session factory."""
return orm.sessionmaker(bind=make_engine())

View file

@ -0,0 +1,51 @@
"""Provide the ORM's Courier model."""
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.dialects import postgresql
from urban_meal_delivery.db import meta
class Courier(meta.Base):
"""A Courier working for the UDP."""
# pylint:disable=too-few-public-methods
__tablename__ = 'couriers'
# Columns
id = sa.Column(sa.Integer, primary_key=True, autoincrement=False) # noqa:WPS125
created_at = sa.Column(sa.DateTime, nullable=False)
vehicle = sa.Column(sa.Unicode(length=10), nullable=False)
historic_speed = sa.Column('speed', postgresql.DOUBLE_PRECISION, nullable=False)
capacity = sa.Column(sa.SmallInteger, nullable=False)
pay_per_hour = sa.Column(sa.SmallInteger, nullable=False)
pay_per_order = sa.Column(sa.SmallInteger, nullable=False)
# Constraints
__table_args__ = (
sa.CheckConstraint(
"vehicle IN ('bicycle', 'motorcycle')", name='available_vehicle_types',
),
sa.CheckConstraint('0 <= speed AND speed <= 30', name='realistic_speed'),
sa.CheckConstraint(
'0 <= capacity AND capacity <= 200', name='capacity_under_200_liters',
),
sa.CheckConstraint(
'0 <= pay_per_hour AND pay_per_hour <= 1500', name='realistic_pay_per_hour',
),
sa.CheckConstraint(
'0 <= pay_per_order AND pay_per_order <= 650',
name='realistic_pay_per_order',
),
)
# Relationships
orders = orm.relationship('Order', back_populates='courier')
def __repr__(self) -> str:
"""Non-literal text representation."""
return '<{cls}(#{courier_id})>'.format(
cls=self.__class__.__name__, courier_id=self.id,
)

View file

@ -0,0 +1,26 @@
"""Provide the ORM's Customer model."""
import sqlalchemy as sa
from sqlalchemy import orm
from urban_meal_delivery.db import meta
class Customer(meta.Base):
"""A Customer of the UDP."""
# pylint:disable=too-few-public-methods
__tablename__ = 'customers'
# Columns
id = sa.Column(sa.Integer, primary_key=True, autoincrement=False) # noqa:WPS125
def __repr__(self) -> str:
"""Non-literal text representation."""
return '<{cls}(#{customer_id})>'.format(
cls=self.__class__.__name__, customer_id=self.id,
)
# Relationships
orders = orm.relationship('Order', back_populates='customer')

Some files were not shown because too many files have changed in this diff Show more