Skip to content

Instantly share code, notes, and snippets.

Avatar

Matt Oates MattOates

View GitHub Profile
@MattOates
MattOates / find-primes.p6
Last active Jul 13, 2021
Combined primes benchmark with a parallel implementation that does well to keep up despite doing a lot more calculations!
View find-primes.p6
#!/usr/bin/env perl6
use v6;
use Stats;
sub bench(Str $name, Int $upto, &code) {
my ($start,$end);
my @times;
for 1..20 {
View digispark_macOS_reverse_shell.c
#include "DigiKeyboard.h"
//Command key is ord 37 and as a modifier 0x00000008
//See NX_DEVICELCMDKEYMASK http://svn.red-bean.com/bob/SDL-altivec/trunk/src/video/quartz/SDL_QuartzEvents.m
//For a look at available keys https://github.com/digistump/DigisparkArduinoIntegration/blob/master/libraries/DigisparkKeyboard/DigiKeyboard.h
#define MOD_CMD_LEFT 0x00000008
boolean do_hack = true;
void setup() {
@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;