Skip to content

Instantly share code, notes, and snippets.

Neil Lyons nwjlyons

Block or report user

Report or block nwjlyons

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
@nwjlyons
nwjlyons / path_converters.py
Created Oct 14, 2019
Django Date Path Converter
View path_converters.py
from datetime import datetime, date
class DateConverter:
regex = '[0-9]{4}-[0-9]{2}-[0-9]{2}'
def to_python(self, value: str):
return datetime.strptime(value, '%Y-%m-%d').date()
def to_url(self, value: date):
View preload_redirect_middleware.py
from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
def preload_redirect_middleware(get_response):
def middleware(request):
response = get_response(request)
if isinstance(response, (HttpResponseRedirect, HttpResponsePermanentRedirect)):
@nwjlyons
nwjlyons / keyword_arg.py
Last active Jun 18, 2019
Python 2/3 add keyword arg
View keyword_arg.py
# python 2
class Person(object):
def __init__(self, name, age):
self.name = name
self.age = age
class Employee(Person):
def __init__(self, *args, **kwargs):
self.job_title = kwargs.pop('job_title')
@nwjlyons
nwjlyons / benchmarks.py
Created Sep 18, 2018
Redis v SQL benchmarks
View benchmarks.py
import timeit
from django.core.cache import cache
from django.contrib.auth.models import User
def benchmark_redis(n=1):
for _ in range(n):
cache.get("blank")
@nwjlyons
nwjlyons / blocks.py
Last active Jun 26, 2018
Wagtail NonFieldErrors for Blocks
View blocks.py
class NonFieldErrors:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.non_field_errors = []
self.meta.form_template = 'struct.html'
def get_form_context(self, value, prefix='', errors=None):
context = super().get_form_context(value, prefix, errors)
context['non_field_errors'] = self.non_field_errors
return context
@nwjlyons
nwjlyons / tron.lua
Created Apr 2, 2018
Tron game built for the PICO8 fantasy console
View tron.lua
pico-8 cartridge // http://www.pico-8.com
version 16
__lua__
line_weight = 1
border_colour = 7
up = 2
right = 1
down = 3
left = 0
View django-nested-urls-example.py
# https://docs.djangoproject.com/en/2.0/topics/http/urls/#including-other-urlconfs
from django.urls import include, path
from . import views
# Change this
urlpatterns = [
path('<page_slug>-<page_id>/history/', views.history),
path('<page_slug>-<page_id>/edit/', views.edit),
@nwjlyons
nwjlyons / cron_to_human.py
Created Jan 17, 2018
Convert a cron file into human readable format.
View cron_to_human.py
# Prerequisites
#
# - npm install -g hcron
import subprocess
cron_input = open('cron.input.txt')
cron_output = open('cron.output.txt', 'w')
for line in cron_input.readlines():
@nwjlyons
nwjlyons / chunks.py
Created Dec 6, 2017
Python split generator into sub lists
View chunks.py
def chunks(generator, chunk_size):
"""Yield successive chunks from a generator"""
chunk = []
for item in generator:
if len(chunk) >= chunk_size:
yield chunk
chunk = [item]
else:
chunk.append(item)
You can’t perform that action at this time.