Skip to content

Instantly share code, notes, and snippets.

View mgd020's full-sized avatar

mgd020 mgd020

View GitHub Profile
@mgd020
mgd020 / srt.py
Created September 4, 2022 11:34
Offset and scale SRT translation file
from dataclasses import dataclass
from datetime import datetime, timedelta
TIME_FORMAT = '%H:%M:%S,%f'
def load_time(s: str) -> float:
t = datetime.strptime(s, TIME_FORMAT)
return (t - t.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds()
@mgd020
mgd020 / parseHTML.js
Created October 20, 2021 00:49
turn a string of html into a document fragment
function parseHTML(html) {
var template = document.createElement('template');
template.innerHTML = html;
return template.content;
}
@mgd020
mgd020 / throttle.py
Last active November 4, 2021 04:53
Django throttling class using cache
import time
from typing import List, NamedTuple
from django.core.cache import caches
class RateLimit(NamedTuple):
seconds: int
limit: int
@mgd020
mgd020 / iseu.js
Created August 18, 2021 04:59
is the user in the EU
isEu = function() {
var euCountryCodes = new Set(['be', 'el', 'lt', 'pt', 'bg', 'es', 'lu', 'ro', 'cz', 'fr', 're', 'gp', 'mq', 'gf', 'yt', 'bl', 'mf', 'pm', 'wf', 'pf', 'nc', 'hu', 'si', 'dk', 'fo', 'gl', 'hr', 'mt', 'sk', 'de', 'it', 'nl', 'aw', 'cw', 'sx', 'fi', 'ax', 'ee', 'cy', 'at', 'se', 'ie', 'lv', 'pl', 'uk', 'gb', 'ai', 'bm', 'io', 'vg', 'ky', 'fk', 'gi', 'ms', 'pn', 'sh', 'tc', 'gg', 'je', 'im']);
return function isEu() {
return (
Intl.DateTimeFormat().resolvedOptions().timeZone.startsWith('Europe/') ||
euCountryCodes.has((navigator.userLanguage || navigator.language).slice(0, 2))
);
}
}();
from fractions import Fraction, gcd
def matrix_determinant(m):
"""determinant using laplace transform"""
size = len(m)
assert len(m[0]) == size
if size == 2:
return m[0][0] * m[1][1] - m[0][1] * m[1][0]
return sum(
@mgd020
mgd020 / getsizeof_deep.py
Created September 3, 2020 23:50
recursive getsizeof
import sys
def getsizeof_deep(o, verbose=False):
default_size = sys.getsizeof(0)
seen = set()
todo = [o]
size = 0
# encoding: US-ASCII
# stolen largely from http://www.ruby-forum.com/topic/140784
require 'stringio'
require 'fileutils'
def extract_chunk(input, output)
lenword = input.read(4)
length = lenword.unpack('N')[0]
type = input.read(4)
@mgd020
mgd020 / fnv.py
Last active August 21, 2020 03:56
implementation of fnv-1(a) with xor folding and both lazy and non-lazy reduction
# http://isthe.com/chongo/tech/comp/fnv/
# https://tools.ietf.org/html/draft-eastlake-fnv-11
FNV_PARAMS = {
# bits -> prime, offset basis
# see http://isthe.com/chongo/tech/comp/fnv/
32: (16777619, 2166136261),
64: (1099511628211, 14695981039346656037),
128: (309485009821345068724781371, 144066263297769815596495629667062367629),
@mgd020
mgd020 / random.js
Last active June 19, 2020 05:52
Generate a cryptographically random String with a given length
function getRandomBase64String(length) {
var array = new Uint8Array(Math.ceil(length * 3 / 4));
crypto.getRandomValues(array);
return btoa(String.fromCharCode.apply(null, array)).slice(0, length);
}
function getRandomHexString(length) {
var array = new Uint32Array(Math.ceil(length / 8));
crypto.getRandomValues(array);
return Array.prototype.map.call(array, i => i.toString(16).padStart(2, '0')).join('').slice(0, length);
@mgd020
mgd020 / sam.py
Created April 1, 2020 11:59
Lambda wrapper for wsgi app
import base64
import cgi
import codecs
import io
import sys
from urllib.parse import urlencode
from wsgiref.util import FileWrapper
from .wsgi import application