Skip to content

Instantly share code, notes, and snippets.

mgd020 mgd020

View GitHub Profile
View fnv1a.py
fnv1a_128_prime = 309485009821345068724781371
fnv1a_128_offset = 144066263297769815596495629667062367629
fnv1a_128_mask = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
def fnv1a_128(bs):
h = fnv1a_offset
for b in bs:
h ^= b
h *= fnv1a_prime
View base_26.py
def base26_encode(n, max_len=6):
# base-26 alphabet (A-Z)
value = ''
digits = n
while digits > 0:
if len(value) == max_len:
raise ValueError(n)
value += chr((digits % 26) + A)
digits /= 26
value += 'A' * max(max_len - len(value), 0)
@mgd020
mgd020 / template.py
Created Oct 29, 2019
python template engine
View template.py
import re
example = """
one
two
three
{% if 1 %}{% end %}
{{
dance(
View image_nes_filter.js
var image_nes_filter = (function() {
var PIXEL_SIZE = 4;
dither = (function() {
// Floyd–Steinberg dither
var ERROR_DIFFUSION_1_16 = 1 / 16,
ERROR_DIFFUSION_3_16 = 3 / 16,
ERROR_DIFFUSION_5_16 = 5 / 16,
ERROR_DIFFUSION_7_16 = 7 / 16,
@mgd020
mgd020 / fairqueue.py
Created Apr 23, 2019
Fair Queueing implementation for Python multiprocessing
View fairqueue.py
"""A multiprocessing Queue but is "fair" to every flow/session/channel.
See https://en.wikipedia.org/wiki/Fair_queuing.
"""
import sys
import time
from multiprocessing.queues import Queue
@mgd020
mgd020 / onetomanyfield.py
Created Mar 21, 2019
Adds OneToManyField django model field
View onetomanyfield.py
from django.db import models
from django.db.models.fields.related import ManyToManyRel, ReverseOneToOneDescriptor
from django.utils.translation import gettext_lazy as _
class OneToManyRel(ManyToManyRel):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.multiple = False
@mgd020
mgd020 / set_tags.py
Created Feb 21, 2019
Add `set` like twigs to django templates
View set_tags.py
from django import template
from django.template.base import token_kwargs
register = template.Library()
class SetBlockNode(template.Node):
def __init__(self, name, nodelist):
self.name = name
@mgd020
mgd020 / choices.py
Created Sep 19, 2018
nice choices class for
View choices.py
"""
Example usage:
# declare
class Color(Choices):
RED = 'Red'
GREEN = 'Green'
BLUE = 'Blue'
# use on model
@mgd020
mgd020 / binary_json.py
Created Aug 27, 2018
An example implementation of binary json
View binary_json.py
import struct
import sys
"""
encoding is little endian
header details: (lsb to msb)
int7 1
length 7 bits of int [-64, 63]
@mgd020
mgd020 / util.js
Last active Jun 2, 2018
basic javascript utils
View util.js
function element(tagName) {
/*
create an element
arguments: tagName, [attributes], children...
*/
// help the minifier out a bit
var args = arguments
var doc = document
You can’t perform that action at this time.