Content :
# This example pyproject.toml is for a basic pip+setuptools setup. | |
# If you use a project management tool (like Poetry), then | |
# those tools will have slightly different configurations or additions. | |
# I highly recommend using a project management tool for your project. | |
# Project management is a highly opinionated subject. | |
# There are a lot of good, robust tools in this space now (as of 2023) | |
# Two that I've used and recommend are Poetry and PDM. | |
# Poetry is more mature, PDM is recent, both work well. | |
# - Poetry: https://python-poetry.org/ |
import random | |
import string | |
import time | |
values = [] | |
for i in range(10): | |
values.append("".join(random.choices(string.ascii_letters + string.digits, k=10))) | |
n = 500000 | |
print(f"Construct {n=} dicts with these random values: {values=}") |
"""Time functions using a decorator.""" | |
import time | |
from collections.abc import Callable | |
from functools import wraps | |
def time_function[**P, R](func: Callable[[P], R]) -> Callable[[P], R]: | |
""" | |
Time functions using a decorator. |
This logging setup configures Structlog to output pretty logs in development, and JSON log lines in production.
Then, you can use Structlog loggers or standard logging
loggers, and they both will be processed by the Structlog pipeline (see the hello()
endpoint for reference). That way any log generated by your dependencies will also be processed and enriched, even if they know nothing about Structlog!
Requests are assigned a correlation ID with the asgi-correlation-id
middleware (either captured from incoming request or generated on the fly).
All logs are linked to the correlation ID, and to the Datadog trace/span if instrumented.
This data "global to the request" is stored in context vars, and automatically added to all logs produced during the request thanks to Structlog.
You can add to these "global local variables" at any point in an endpoint with `structlog.contextvars.bind_contextvars(custom
# -*- coding: utf-8 -*- | |
import os | |
from html.parser import HTMLParser | |
import dash | |
import pandas as pd | |
import plotly.express as px | |
import requests | |
from dash import html, dcc, dash_table, Input, Output |
When a fixture is not used but needs to be included for some reasons, pylint will rightfully issue a warning. For instance, a fixture setup a specific database state and another fixture include it to ensure database is in a specific state.
@pytest.fixture
def init_database():
"""Setup database state"""
...
# ---------------------------------------------------------------------------- | |
import json | |
import simdjson | |
import libpy_simdjson | |
decoder_std = json.JSONDecoder() | |
encoder_std = json.JSONEncoder(ensure_ascii=False) | |
p = simdjson.Parser() | |
p2 = libpy_simdjson.Parser() |