Skip to content

Instantly share code, notes, and snippets.

View MattOates's full-sized avatar

Matt Oates MattOates

View GitHub Profile
@MattOates
MattOates / descriptor_typing_problem.py
Created February 17, 2020 16:37
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__
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 February 9, 2020 23:00
Dirty script to render the Mandelbrot set to the terminal
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;
@MattOates
MattOates / proxy.sh
Created June 7, 2017 16:18
Proxy stuff on macOS
function proxyon {
sudo networksetup -setwebproxy Wi-Fi localhost 8080
sudo networksetup -setsecurewebproxy Wi-Fi localhost 8080
sudo networksetup -setwebproxystate Wi-Fi on
sudo networksetup -setsecurewebproxystate Wi-Fi on
ssh -2CnNqT -L 8080:wwwcache.server.com:8080 user@bastion.host
}
function proxyoff {
sudo networksetup -setwebproxystate Wi-Fi off
from typing import NamedTuple
import json
class NamedTupleJSONEncoder(json.JSONEncoder):
def encode(self, o):
print(f"Encoding {type(o)}")
return super().encode(o)
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)
#!/usr/bin/env perl6
sub character_differences(Str $str1, Str $str2) {
$str1.comb Z~~ $str2.comb
}
sub hamming_distance(Str $str1, Str $str2) {
my @differences = character_differences($str1, $str2);
return @differences.grep(* == False).elems;
}
#!/usr/bin/env perl6
use v6;
use Stats;
#Tim's original times:
#primes-inline-loop(1000) ran in 2.425 seconds (σ = 0.213 seconds).
#primes-inline-loop-upto-sqrt(1000) ran in 2.311 seconds (σ = 0.131 seconds).
#primes-inline-loop-upto-int-sqrt(1000) ran in 2.274 seconds (σ = 0.161).
sub bench($name, &code) {
Time: <span id="clockDisplay"></span>
@MattOates
MattOates / main.p6
Created October 16, 2018 15:04
6pad gist test
say "Ohai!";
for ^10 -> $stuff {
say $stuff if $stuff.is-prime;
}
@MattOates
MattOates / hackerrank_bench.p6
Last active September 13, 2018 09:07
Some additional benched P6 examples from Tyil's Hackerrank post https://www.tyil.nl/post/2018/09/13/hackerrank-solutions-python3-and-perl6-part-1/
use Stats;
sub bench($name, &code) {
my ($start,$end);
my @times;
for 1..100 {
$start = now;
code();