Skip to content

Instantly share code, notes, and snippets.

View Goldziher's full-sized avatar
🐙
Codoctupus

Na'aman Hirschfeld Goldziher

🐙
Codoctupus
View GitHub Profile
@Goldziher
Goldziher / route_handlers.py
Last active January 2, 2022 08:57
Route Handlers Example
from typing import List
from starlite import Partial, delete, get, patch, post, put
from my_app.models import Resource
@get(path="/resources")
def list_resources() -> List[Resource]:
...
@Goldziher
Goldziher / main.py
Created January 1, 2022 10:02
FastAPI official example
from typing import Optional
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@Goldziher
Goldziher / generate_api_types.sh
Last active November 30, 2021 21:47
Script to generate api specs from a given api using DTSGenerator
#!/bin/bash
SPECS_DOWNLOAD_URL="https://url-of-api.com/specs"
FILE="$PWD/src/my-api-specs.ts"
if [[ -f "$FILE" ]]; then
rm "$FILE"
fi
# use dtsgen to generate the target typescript file
dtsgen --url $SPECS_DOWNLOAD_URL >> "$FILE"
@Goldziher
Goldziher / recursiveResolve.ts
Last active November 27, 2021 19:21
Recursive Promise Resolve
function isRecord<T = any>(value: unknown): value is Record<string | symbol, T> {
return !!(value && typeof value === 'object');
}
function isPromise<T = unknown>(value: unknown): value is Promise<T> {
return isRecord(value) && Reflect.has(value, 'then');
}
async function recursiveResolve<T>(
parsedObject: Record<string, any>,
@Goldziher
Goldziher / duck.ts
Last active November 27, 2021 16:50
Duck typing in typescript
interface Duck {
quack(): string;
}
function isDuck(value: unknown): value is Duck {
return !!(
value &&
typeof value === 'object' &&
typeof Reflect.get(value, 'quack') === 'function'
);
@Goldziher
Goldziher / duck.js
Last active November 27, 2021 16:50
Duck typing in javascript
function isDuck(value) {
return !!(
value &&
typeof value === 'object' &&
typeof Reflect.get(value, 'quack') === 'function'
);
}
@Goldziher
Goldziher / duck.py
Last active November 27, 2021 18:52
python duck typing example
from typing import Any
def is_duck(value: Any) -> bool:
try:
value.quack()
return True
except (Attribute, ValueError):
return False
@Goldziher
Goldziher / chunk.py
Created November 10, 2021 13:32
generic python chunk function
from itertools import islice
from typing import Iterator, Sequence, TypeVar
T = TypeVar("T")
def chunk(sequence: Sequence[T], size: int) -> Iterator[list[T]]:
"""given a sequence, return an iterator of lists of the given size"""
return iter(lambda: list(islice(iter(sequence), size)), [])
@Goldziher
Goldziher / Dockerfile
Last active February 10, 2023 08:41
fastAPI Poetry Docker image
FROM python:3.9-slim AS install
RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get install -y --no-install-recommends curl \
&& apt-get autoremove -y
RUN pip install --upgrade pip
WORKDIR /app/
# install poetry and keep the get-poetry script so it can be reused later.
ENV POETRY_HOME="/opt/poetry"
RUN curl https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py > get-poetry.py
@Goldziher
Goldziher / useInterval.ts
Created October 21, 2021 19:27
React useInterval hook
import { useEffect, useRef } from 'react';
export function useInterval(
callback: (clearInterval: () => void) => any,
delay: number | null,
): () => void {
const idRef = useRef<null | number>(null);
const clearInterval = () => {
if (idRef.current) {