Skip to content

Instantly share code, notes, and snippets.

Avatar

Matt Oates MattOates

View GitHub Profile
@MattOates
MattOates / base.py
Created Dec 13, 2020
Attempt to make a nice Tortoise BaseModel to work with FastAPI
View base.py
from typing import (
Type,
Optional,
)
from tortoise.models import Model
from tortoise.contrib.pydantic import (
pydantic_model_creator,
PydanticModel,
)
@MattOates
MattOates / observable.py
Last active Jul 24, 2020
Observer pattern in Python, but has some annoying problems with respect to mypy typing
View observable.py
from typing import Any, Callable, Dict, List, Optional, Set
from abc import ABC, abstractmethod
import logging
LOGGER = logging.getLogger(__name__)
class Observer(ABC):
@abstractmethod
def update(self, observed: "Observable", event: Any) -> None:
View flatten_counter.py
things = {"hi":10,"bye":3}
def flatten_counter(counter):
for value, count in counter.items():
for i in range(count):
yield value
print(list(flatten_counter(things)))
@MattOates
MattOates / collatz_numba.py
Last active Feb 26, 2020
Helper functions for investigating the collatz graph and paths
View collatz_numba.py
#!/usr/bin/env python3
import numpy as np
from numba import njit, uint64
from numba.typed import Dict
collatz_graph = Dict.empty(key_type=uint64, value_type=uint64)
collatz_path_lengths = Dict.empty(key_type=uint64, value_type=uint64)
def collatz(n: uint64, collatz_graph: Dict, collatz_path_lengths: Dict):
# collatz_path_lengths[n] = collatz_iter(n, collatz_graph)
@MattOates
MattOates / collatz.py
Created Feb 26, 2020
Some functions for exploring the Collatz graph
View collatz.py
from typing import Dict
import numpy as np
collatz_graph: Dict[int, int] = dict()
collatz_path_lengths: Dict[int, int] = dict()
def collatz(n: int):
collatz_path_lengths[n] = collatz_iter(n)
return collatz_path(n)
@MattOates
MattOates / descriptor_typing_problem.py
Last active Feb 17, 2020
The general problem is that mypy doesn't really understand that a descriptor on an attribute is going to have the return value of __get__ not __call__/__init__
View descriptor_typing_problem.py
"""
If you run the following with mypy you will get the following type error
mypy descriptor_typing_problem.py
descriptor_typing_problem.py:36: error: Incompatible types in assignment (expression has type "attr_descriptor", variable has type "int")
"""
from enum import Enum
from typing import Type, Any
Sentinels = Enum("Sentinels", "NO_INIT")
@MattOates
MattOates / descriptor_typing_problem.py
Created Feb 17, 2020
The general problem is that mypy doesn't really understand that a descriptor on an attribute is going to have the return value of __get__ not __call__/__init__
View descriptor_typing_problem.py
from enum import Enum
from typing import Type, Any
Sentinels = Enum("Sentinels", "NO_INIT")
class attr_descriptor:
def __init__(self, default=Sentinels.NO_INIT):
self.default = default
@MattOates
MattOates / mandelbrot.pl6
Last active Feb 9, 2020
Dirty script to render the Mandelbrot set to the terminal
View mandelbrot.pl6
sub terminal-width(:$default=100) {
my $width = run('tput', 'cols', :out).out.slurp-rest.trim.Int;
return $width < $default ?? $default !! $width;
}
sub is-mandelbrot(Complex $z0, int $max=100) {
my Complex $z = $z0;
for ^$max -> $n {
return $n if ($z.abs() > 2e0);
$z = $z**2 + $z0;
View named_tuple_dumping_fail.py
from typing import NamedTuple
import json
class NamedTupleJSONEncoder(json.JSONEncoder):
def encode(self, o):
print(f"Encoding {type(o)}")
return super().encode(o)
View sketch_of_concept_trigger.sql
CREATE TABLE patient (
name text,
administrative_gender uuid references concept.concept_cid,
ethnicity uuid references concept.concept_cid,
phenotypic_sex_cid uuid references concept.concept_cid,
);
-- Parametric trigger for validating concept FK against sets of codesystems
-- concept_ind_codesystem(concept_field_name, array_of_codesystems)