Skip to content

Instantly share code, notes, and snippets.

View florimondmanca's full-sized avatar
🌱

Florimond Manca florimondmanca

🌱
View GitHub Profile
@florimondmanca
florimondmanca / bayesodds.py
Last active December 24, 2020 15:20
Bayes' Rule, reframed "odds" instead of "chances"
"""
Bayes rule, reframed as "odds" instead of "chances".
https://twitter.com/florimondmanca/status/1342119381573238789
Inspired by 3Blue1Brown's "The medical test paradox: Can redesigning Bayes rule help?".
"""
from fractions import Fraction
from typing import Any
@florimondmanca
florimondmanca / async_example.py
Last active June 16, 2023 10:19
HTTPX integration for OpenTelemetry (Proof of Concept)
from opentelemetry.instrumentation import httpx_client as httpx_opentelemetry
async def main():
opentelemetry_on_request, opentelemetry_on_response = httpx_opentelemetry.create_async_hooks()
event_hooks = {
"request": [opentelemetry_on_request],
"response": [opentelemetry_on_response]
}
@florimondmanca
florimondmanca / httpx-urllib3.md
Last active September 3, 2024 20:47
URLLib3 transport implementation (Extracted from HTTPX)

Changelog:

  • 2024-07-15: updated to support recent HTTPX versions, based on @karpetrosyan's comment.
  • 2021-01 (ish): initial version

urllib3-transport

An HTTPCore transport that uses urllib3 as the HTTP networking backend. (This was initially shipped with HTTPX.)

@florimondmanca
florimondmanca / example_httpcore.py
Last active August 4, 2020 06:34
Proof of concept for an `httpcore` Unix Domain Socket (UDS) transport, compatible with asyncio and trio. Also contains an anyio implementation example.
"""
Example using the transport directly.
"""
import json
from httpcore_uds import AsyncUDSTransport
async def main() -> None:
"""
@florimondmanca
florimondmanca / README.md
Last active August 22, 2024 15:05 — forked from imbolc/httpx_aiohttp.py
HTTPX vs aiohttp (over HTTPS)

Usage

  • Generate TLS certificates for localhost:
pip install trustme-cli
trustme-cli
  • Run wrk on each endpoint, eg:
@florimondmanca
florimondmanca / asgi-packages.md
Last active July 25, 2024 23:53
ASGI Packages

A list of ASGI-related Python packages I maintain:

@florimondmanca
florimondmanca / task_id_contextvars_demo.py
Created September 8, 2019 18:21
Sharing a task ID across coroutines using contextvars (Trio-based)
import trio
import contextvars
LAST_TASK_ID = 0
TASK_ID: contextvars.ContextVar[str] = contextvars.ContextVar("task_id")
async def producer(num_items: int, send_channel: trio.MemorySendChannel) -> None:
task_id = TASK_ID.get()
iden = f"Producer {task_id}"
@florimondmanca
florimondmanca / serializers.py
Last active September 21, 2024 10:34
DRF-inspired ModelSerializer implementation backed by Pydantic and Tortoise ORM
import datetime
import decimal
import json
from typing import Any, Optional, Type, Union, Dict, Tuple
from pydantic import BaseModel, ValidationError, create_model
from tortoise import fields as tortoise_fields
from tortoise.fields import Field
from tortoise.models import Model as TortoiseModel
@florimondmanca
florimondmanca / makemigrations.py
Created July 28, 2018 17:19
Script to run a stand-alone equivalent of Django's `manage.py makemigrations`
"""Script to run a stand-alone equivalent of Django's `makemigrations`."""
import sys
import django
from django.conf import settings
from django.core.management import call_command
def makemigrations(app_name: str):
@florimondmanca
florimondmanca / set-env.js
Created July 15, 2018 08:09
Build-time environment variable injection script for Angular apps
/* Generate environment.ts file using environment variables
Requires:
npm install --save yargs
npm install --save dotenv
Usage:
node set-env.js --env=dev
Put variables in a local .env file so they can be