Skip to content

Instantly share code, notes, and snippets.

View mgd020's full-sized avatar

mgd020 mgd020

View GitHub Profile
@mgd020
mgd020 / multilocationfield.py
Created September 28, 2016 00:23
A haystack search field for specifying multiple locations for a single document.
from __future__ import absolute_import, division, print_function, unicode_literals
import six
from django.contrib.gis.geos import Point
from haystack import indexes
from haystack.exceptions import SpatialError
from haystack.utils.geo import ensure_geometry, ensure_point
@mgd020
mgd020 / crop.py
Created October 25, 2016 04:17
Fix wagtails built-in FillOperation to center on the focal point
"""
Install via:
from __future__ import absolute_import, division, print_function, unicode_literals
from wagtail.wagtailcore import hooks
from wagtail.wagtailimages.models import Filter
from .crop import CropOperation
@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 / 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 / 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 / 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 / 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