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 / README.md
Last active March 11, 2024 10:12
URLLib3 transport implementation (Extracted from HTTPX)

urllib3-transport

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

When used with HTTPX, this transport makes it easier to transition from Requests to HTTPX by keeping the same underlying HTTP networking layer.

Compatible with: HTTPX 0.15.x, 0.16.x (i.e. HTTPCore 0.11.x and HTTPCore 0.12.x).

Note: not all urllib3 pool manager options are supported here — feel free to adapt this gist to your specific needs.

@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 July 11, 2023 23:07 — 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 December 4, 2023 15:52
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 May 11, 2023 09:53
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