Skip to content

Instantly share code, notes, and snippets.


Luke Plant spookylukey

View GitHub Profile
spookylukey /
Created May 22, 2020
get_object_or_404 example
def product_detail(request, slug):
product = Product.objects.get(slug=slug)
except Product.DoesNotExist:
raise Http404()
return TemplateResponse(request, 'products/product_detail.html', {
'product': product,
spookylukey /
Created Apr 25, 2020
Django checks for mismatched arguments and types in URLs
# Quick and dirty URL checker:
# - checks presence of parameters to every callback registered in main urlconf
# - checks for bad additional parameters (args without default)
# - checks type of parameters if possible
# - can handle all Django's built-in path converters,
# and any other that has a type annotation on the `to_python` method
# Limitations
# - can't check callbacks defined using ``**kwargs`` (e.g. most CBVs)
View gist:b721f1464ddc6517822c93032b536fa1
showcase production
+--------------------------+--------------------------+------------------------------------------------------------------+ │+--------------------------+--------------------------+------------------------------------------------------------------+
| Column | Type | Modifiers | │| Column | Type | Modifiers |
|--------------------------+--------------------------+------------------------------------------------------------------| │|--------------------------+--------------------------+------------------------------------------------------------------|
| id
spookylukey /
Created Jan 7, 2020
visidata glue code for Django models and attrs, with type support
from datetime import date
import visidata
from django.db.models import QuerySet
def get_main_attrs(instance):
if hasattr(instance, '_meta'):
return meta_to_col_list(instance._meta)
elif hasattr(instance, '__attrs_attrs__'):
return [(, field.type or visidata.anytype)
spookylukey /
Last active Jan 7, 2020
visidata glue code for Django models and attrs
import visidata
from django.db.models import QuerySet
def get_main_attrs(instance):
retval = []
if hasattr(instance, '_meta'):
for field in instance._meta.get_fields():
if not hasattr(field, 'get_attname'):
spookylukey / ast.diff
Created Jan 25, 2019
Beginnings of a patch to use Python AST for compiling
View ast.diff
diff --git a/fluent.runtime/fluent/runtime/ b/fluent.runtime/fluent/runtime/
index f00f750..b76b147 100644
--- a/fluent.runtime/fluent/runtime/
+++ b/fluent.runtime/fluent/runtime/
@@ -3,6 +3,7 @@ Utilities for doing Python code generation
from __future__ import absolute_import, unicode_literals
+import ast
import keyword
spookylukey / Confirm.js
Created Sep 19, 2018
Elm 0.18 confirm Task
View Confirm.js
// Native/Confirm.js
var _user$project$Native_Confirm = function () {
var scheduler = _elm_lang$core$Native_Scheduler;
function confirmTask(prompt, successValue, failValue) {
return scheduler.nativeBinding(function (callback) {
if (window.confirm(prompt)) {
} else {
spookylukey /
Created Sep 27, 2017
Hollerith constant parsing in pyparsing
import pyparsing as pp
def hollerith():
intExpr = pp.Word(pp.nums).setParseAction(lambda t: int(t[0]))
stringExpr = pp.Forward()
def countedParseAction(toks):
n = toks[0]
contents = pp.CharsNotIn('', exact=n)
stringExpr << (pp.Suppress(pp.CaselessLiteral('H')) + contents)
spookylukey / convert_leading_spaces_to_en
Created Jun 22, 2017
Make source code readable on forums that have poor support
View convert_leading_spaces_to_en
#!/usr/bin/env python3
import sys
def fix_line(l):
if not l:
return l
if l[0] == " ":
# Non breaking: "\u00a0" - but munged by some browsers
# EM space: "\u2003" - not munged by browsers
# If it's complex, Django's Generic CBVs will probably
# make your life harder. And if it's simple, they probably
# will too.
# Before:
from django.views import generic
class ReportPDFDetailView(generic.DetailView):
model = DesignerReport
You can’t perform that action at this time.