Skip to content

Instantly share code, notes, and snippets.

mgd020 mgd020

View GitHub Profile
@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
@mgd020
mgd020 / cached_property.py
Created Mar 12, 2018
Method decorator which extends regular property to cache the value.
View cached_property.py
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__
@staticmethod
def _set(obj, value):
raise AttributeError("can't set attribute")
View pretty_id.py
# 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
mgd020 / qct.py
Last active Dec 28, 2017
Extracts the map image from Quick Chart (.QCT) files into PNG
View qct.py
# -*- 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
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.