Skip to content

Instantly share code, notes, and snippets.

View Hultner's full-sized avatar
📯
What are you trying to shell?

Alexander Hultnér Hultner

📯
What are you trying to shell?
View GitHub Profile
@Hultner
Hultner / patched_schema.py
Created July 15, 2021 13:27
Example of patching schema for float Enum query parameters in FastAPI.
from enum import Enum
from fastapi import FastAPI, Depends, HTTPException, Query
from pydantic import BaseModel, ValidationError
app = FastAPI()
class Number(float, Enum):
@Hultner
Hultner / docker-py3.10a6.sh
Created March 12, 2021 15:08
Python 3.10 alpha 6 in docker
docker run -it python:3.10.0a6-buster
@Hultner
Hultner / overload_matching.py
Created March 11, 2021 20:54
Suggestion for overloaded python definitions, not real python syntax.
def fun([head, *tail]): …
def fun([end]): …
def fun(element): …
@Hultner
Hultner / pattern-matching-status-codes.py
Last active March 11, 2021 20:53
Pattern matching HTTP Status codes
status_code = 200
match status_code:
case 404: print("Not found")
case 200: print("Ok")
case _: raise ValueError("Can't handle status code")
# Prints "Ok"
@Hultner
Hultner / pattern-matching.py
Created March 11, 2021 15:47
Python Structural Pattern Matching.
Python 3.10.0a6+ (heads/master:87f649a409, Mar 11 2021, 16:29:20) [Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> status_code = 200
>>> match status_code:
... case 404: print("Not found")
... case 200: print("Ok")
... case _: raise ValueError("Can't handle status code")
...
Ok
>>>
@Hultner
Hultner / bound_base_conditional.py
Created March 5, 2021 12:28
Use assignment with bound TypeVar in conditional.
from typing import Type, TypeVar
class Base:
common: str = "Data"
class ImportModel(Base):
pass
@Hultner
Hultner / generate_interfaces.sh
Created September 22, 2020 15:11
Generate type script interfaces from schema.
#!/usr/bin/env sh
sed -e 's/export interface components {//' \
-e 's/schemas: {//' -e 's/ }\n}$//' \
-e '$d' models/api.ts \
| sed -e '$d' \
| sed -E 's/[[:space:]]*([A-Z].*)+:[[:space:]]*{/export interface \1 {/' \
| sed -E "s/components\['schemas'\]\['([[:alpha:]_[:digit:]]+)'\]/\1/" > models/api-interface.ts && \
rm models/api.ts
@Hultner
Hultner / subtle_markdown.py
Last active June 4, 2020 11:12
Rich subtle markdown style
from typing import Any
from rich.console import Console, ConsoleOptions, RenderResult
from rich.markdown import Markdown, Heading, Text
class SubtleHeading(Heading):
def __init__(self, level):
# Defering all headings by one level to avoid the borderd box for H1, hence level 2 is max
super().__init__(level + 1)
@Hultner
Hultner / model_generator_poc.hy
Created May 19, 2020 19:46
Generate python data models using hy, input is eventually thought to be database introspection based.
(import [pydantic [BaseModel]])
(defn create-attribute [type name]
`(^~type ~name))
(defn create-attributes [attributes]
(map (fn [x] (create-attribute (unpack-iterable x)))
attributes))
@Hultner
Hultner / lock.ipynb
Created April 4, 2020 18:21
Lock puzzle from nordic python
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.