Skip to content

Instantly share code, notes, and snippets.

View mgd020's full-sized avatar

mgd020 mgd020

View GitHub Profile
@mgd020
mgd020 / render_block.py
Created January 17, 2017 02:59
Render the content of a block somewhere else in a template
from __future__ import absolute_import, division, print_function, unicode_literals
from django import template
from django.template.loader_tags import BlockNode, ExtendsNode
register = template.Library()
@register.simple_tag(takes_context=True)
@mgd020
mgd020 / flatten_xml.py
Created February 9, 2017 05:47
Flatten an element tree node into an xpath ordered dict.
from __future__ import absolute_import, division, print_function, unicode_literals
from collections import OrderedDict
def flatten_xml(node):
"""
Produce an ordered dictionary of elements enountered.
Keys are valid xpath selectors.
@mgd020
mgd020 / ugeturl.py
Created April 11, 2017 23:54
Translate django url strings by slugs
from __future__ import absolute_import, division, print_function, unicode_literals
import re
from django.utils import six
from django.utils.functional import lazy, SimpleLazyObject
from django.utils.translation import ugettext
URL_SLUG_RE = SimpleLazyObject(lambda: re.compile(r'^[a-zA-Z0-9\-]+$'))
@mgd020
mgd020 / earley_recog.py
Last active April 30, 2017 09:34
Python implementation of Earley recogniser
from __future__ import absolute_import, division, print_function, unicode_literals
from collections import namedtuple
from itertools import chain
class Rule(namedtuple('Rule', ['lhs', 'rhs'])):
def __new__(cls, lhs, rhs):
return super(Rule, cls).__new__(cls, lhs, tuple(rhs)) # ensure hashable
@mgd020
mgd020 / jsonxml.py
Last active June 8, 2017 23:53
Convert between XML and JSON
from __future__ import absolute_import, division, print_function, unicode_literals
from xml import sax
from xml.sax import saxutils
class XMLParser(sax.handler.ContentHandler):
def __init__(self, cdata_key):
self.cdata_key = cdata_key
@mgd020
mgd020 / technical_response.py
Created August 10, 2017 05:02
Allow Django superusers to view unhandled exceptions in debug mode.
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
import sys
from django.http import Http404
from django.views.debug import technical_500_response
class SuperuserTechnical500Middleware(object):
@mgd020
mgd020 / lexer.py
Last active August 15, 2017 12:19
python lexer with state stack
# -*- coding: utf-8 -*-
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import re
import unittest
from collections import namedtuple
class TokenError(Exception):
@mgd020
mgd020 / json_uri.js
Created December 11, 2017 13:36
encode json for
function encodeJSONURIComponent(obj) {
return btoa(encodeURIComponent(JSON.stringify(obj))).replace('+', '-').replace('/', '_').replace(/=+$/, '');
}
function decodeJSONURIComponent(str) {
return JSON.parse(decodeURIComponent(atob(str.replace('-', '+').replace('_', '/'))));
}
@mgd020
mgd020 / qct.py
Last active December 28, 2017 12:08
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
# 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'==')