Skip to content

Instantly share code, notes, and snippets.

View adriangb's full-sized avatar

Adrian Garcia Badaracco adriangb

View GitHub Profile
@adriangb
adriangb / main.py
Last active June 2, 2022 16:58 — forked from Kludex/main.py
Initial implementation of a Hook system to build middlewares.
from typing import (
Awaitable,
Iterable,
Mapping,
Optional,
Protocol,
Tuple,
TypeVar,
Union,
cast,
@adriangb
adriangb / example.py
Created February 27, 2022 23:56
template class
from typing import ClassVar, Protocol
class APIKey(Protocol): # provided by some framework
header_name: ClassVar[str]
key: str
def __init__(self, key: str) -> None: # so that framework can construct the subclass
self.key = key
@adriangb
adriangb / main.py
Last active February 10, 2022 17:03 — forked from Kludex/main.py
Run Xpresso and Uvicorn Programmatically
""" Snippet that demonstrates how to use Uvicorn in code.
Feel free to run:
- `python main.py`
"""
import asyncio
import uvicorn
from pydantic import BaseSettings
@adriangb
adriangb / hashedpyany.rs
Last active November 27, 2021 10:07
Hashable Python objects in PyO3
use std::cmp;
use std::hash;
use pyo3::basic::CompareOp;
use pyo3::prelude::*;
// We can't put a Py<PyAny> directly into a HashMap key
// So to be able to hold references to arbitrary Python objects in HashMap as keys
// we wrap them in a struct that gets the hash() when it receives the object from Python
use std::cmp;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::hash;
use pyo3::prelude::*;
use pyo3::types::{PyTuple};
// We can't put a Py<PyAny> directly into a HashMap key
use std::cmp;
use std::hash;
use std::collections::HashSet;
use std::os::raw::c_int;
use pyo3::prelude::*;
use pyo3::ffi;
use pyo3::conversion::{AsPyPointer};
use pyo3::{pyobject_native_type_base,pyobject_native_type_info,pyobject_native_type_extract,PyNativeType};
use pyo3::types::{PyString};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
from __future__ import annotations
import cProfile
import pstats
from collections import deque
from dataclasses import dataclass, field
from random import Random
from timeit import default_timer
from typing import Awaitable, Callable, Deque, Iterable, List, Optional, Protocol, Union
import typing
from dataclasses import dataclass
@dataclass
class BodyBase:
content_type: str
@dataclass
from __future__ import annotations
from dataclasses import dataclass as dataclass
from dataclasses import field
from typing import Any, Dict, List, Literal, Mapping, Optional, Union
ParameterLocations = Literal["header", "path", "query", "cookie"]
PathParamStyles = Literal["simple", "label", "matrix"]
QueryParamStyles = Literal["form", "spaceDelimited", "pipeDelimited", "deepObject"]
HeaderParamStyles = Literal["simple"]