Skip to content

Instantly share code, notes, and snippets.

mgd020 mgd020

View GitHub Profile
mgd020 /
Created Apr 23, 2019
Fair Queueing implementation for Python multiprocessing
"""A multiprocessing Queue but is "fair" to every flow/session/channel.
import sys
import time
from multiprocessing.queues import Queue
mgd020 /
Created Mar 21, 2019
Adds OneToManyField django model field
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 /
Created Feb 21, 2019
Add `set` like twigs to django templates
from django import template
from django.template.base import token_kwargs
register = template.Library()
class SetBlockNode(template.Node):
def __init__(self, name, nodelist): = name
mgd020 /
Created Sep 19, 2018
nice choices class for
Example usage:
# declare
class Color(Choices):
RED = 'Red'
GREEN = 'Green'
BLUE = 'Blue'
# use on model
mgd020 /
Created Aug 27, 2018
An example implementation of binary json
import struct
import sys
encoding is little endian
header details: (lsb to msb)
int7 1
length 7 bits of int [-64, 63]
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
mgd020 /
Created Mar 12, 2018
Method decorator which extends regular property to cache the value.
class cached_property(object):
"""Decorator that converts a method with a single self argument into a property cached on the instance."""
def __init__(self, func):
self._get = func
self.__doc__ = getattr(func, '__doc__')
self._name = '_cached_' + func.__name__
def _set(obj, value):
raise AttributeError("can't set attribute")
# Turn an integer into a nice base64 encoded string
# E.g. 4095 -> D_8
# requires python 3
import base64
def encode(n):
return base64.urlsafe_b64encode(n.to_bytes((n.bit_length() + 7) // 8, 'big')).rstrip(b'==')
mgd020 /
Last active Dec 28, 2017
Extracts the map image from Quick Chart (.QCT) files into PNG
# -*- coding: utf-8 -*-
# REQUIRES pillow-simd or pillow
from __future__ import absolute_import, division, print_function, unicode_literals
# import cProfile
# import cStringIO
import datetime
import itertools
import os
# import pstats
mgd020 / json_uri.js
Created Dec 11, 2017
encode json for
View json_uri.js
function encodeJSONURIComponent(obj) {
return btoa(encodeURIComponent(JSON.stringify(obj))).replace('+', '-').replace('/', '_').replace(/=+$/, '');
function decodeJSONURIComponent(str) {
return JSON.parse(decodeURIComponent(atob(str.replace('-', '+').replace('_', '/'))));
You can’t perform that action at this time.