Skip to content

Instantly share code, notes, and snippets.

View spookylukey's full-sized avatar

Luke Plant spookylukey

View GitHub Profile
# 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
@spookylukey
spookylukey / ast.diff
Created January 25, 2019 13:27
Beginnings of a patch to use Python AST for compiling
diff --git a/fluent.runtime/fluent/runtime/codegen.py b/fluent.runtime/fluent/runtime/codegen.py
index f00f750..b76b147 100644
--- a/fluent.runtime/fluent/runtime/codegen.py
+++ b/fluent.runtime/fluent/runtime/codegen.py
@@ -3,6 +3,7 @@ Utilities for doing Python code generation
"""
from __future__ import absolute_import, unicode_literals
+import ast
import keyword
@spookylukey
spookylukey / two_forms_one_view.txt
Created June 29, 2016 10:49
Two forms one view
# Views
def two_form_view(request):
context = {}
if request.method == "POST":
question_form = QuestionForm(request.POST)
answer_form = AnswerForm(request.POST)
success = False
if 'q_button' in request.POST and question_form.is_valid()
question_form.save()
@spookylukey
spookylukey / django_visidata.py
Last active January 7, 2020 12:17
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'):
continue
showcase production
+--------------------------+--------------------------+------------------------------------------------------------------+ │+--------------------------+--------------------------+------------------------------------------------------------------+
| Column | Type | Modifiers | │| Column | Type | Modifiers |
|--------------------------+--------------------------+------------------------------------------------------------------| │|--------------------------+--------------------------+------------------------------------------------------------------|
| id
@spookylukey
spookylukey / django-url-checker.py
Created April 25, 2020 05:56
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)
@spookylukey
spookylukey / views.py
Created May 22, 2020 09:11
get_object_or_404 example
def product_detail(request, slug):
try:
product = Product.objects.get(slug=slug)
except Product.DoesNotExist:
raise Http404()
return TemplateResponse(request, 'products/product_detail.html', {
'product': product,
})
@spookylukey
spookylukey / model_utils.py
Last active November 14, 2020 10:42
Visidata 1.x 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.name, field.type or visidata.anytype)
# list_detail.object_list replacement with all the things I need
def object_list(request, queryset, extra_context=None,
template_name='', paginate_by=None):
class ObjectList(ListView):
def post(self, request, *args, **kwargs):
return self.get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
c = super(ObjectList, self).get_context_data(**kwargs)
@spookylukey
spookylukey / hollerith.py
Created September 27, 2017 06:57
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)