Skip to content

Instantly share code, notes, and snippets.

mgd020 mgd020

Block or report user

Report or block mgd020

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@mgd020
mgd020 / technical_response.py
Created Aug 10, 2017
Allow Django superusers to view unhandled exceptions in debug mode.
View technical_response.py
# -*- 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 / earley_recog.py
Last active Apr 30, 2017
Python implementation of Earley recogniser
View earley_recog.py
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 / ugeturl.py
Created Apr 11, 2017
Translate django url strings by slugs
View ugeturl.py
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 / lexer.py
Last active Aug 15, 2017
python lexer with state stack
View lexer.py
# -*- 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 / flatten_xml.py
Created Feb 9, 2017
Flatten an element tree node into an xpath ordered dict.
View flatten_xml.py
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 / render_block.py
Created Jan 17, 2017
Render the content of a block somewhere else in a template
View render_block.py
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 / crop.py
Created Oct 25, 2016
Fix wagtails built-in FillOperation to center on the focal point
View crop.py
"""
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 / agentbox.py
Created Oct 12, 2016
Client library for agentbox API
View agentbox.py
from __future__ import absolute_import, division, print_function, unicode_literals
import decimal
import datetime
import json
import logging
from time import time
from urlparse import urljoin
import requests
@mgd020
mgd020 / multilocationfield.py
Created Sep 28, 2016
A haystack search field for specifying multiple locations for a single document.
View multilocationfield.py
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 / enum.py
Created Sep 27, 2016
a little python enum gist
View enum.py
class Enum(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
You can’t perform that action at this time.