Skip to content

Instantly share code, notes, and snippets.

@nwjlyons
nwjlyons / email.sh
Last active November 6, 2023 21:12
Send plain and html email via telnet.
telnet 0.0.0.0 1025
Trying 0.0.0.0...
Connected to 0.0.0.0.
Escape character is '^]'.
220 mailhog.example ESMTP MailHog
helo localhost
250 Hello localhost
mail from: <neil@example.com>
250 Sender neil@example.com ok
rcpt to: <lisa@example.com>
@nwjlyons
nwjlyons / echo.go
Created September 28, 2017 13:14
Print HTTP request to stdout
package main
import (
"net"
"os"
"log"
"fmt"
"io"
)
@nwjlyons
nwjlyons / chunks.py
Created December 6, 2017 10:26
Python split generator into sub lists
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)
@nwjlyons
nwjlyons / cron_to_human.py
Created January 17, 2018 17:35
Convert a cron file into human readable format.
# 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 / django-nested-urls-example.py
Last active March 29, 2018 17:14
Django 2.0 nested URLs
# 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 / tron.lua
Created April 2, 2018 19:55
Tron game built for the PICO8 fantasy console
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
@nwjlyons
nwjlyons / blocks.py
Last active June 26, 2018 16:02
Wagtail NonFieldErrors for Blocks
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 / benchmarks.py
Created September 18, 2018 07:44
Redis v SQL benchmarks
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 / docker-rm-stopped-containers.sh
Created May 30, 2019 12:59
Delete stopped containers
docker rm $(docker ps -a -q)
@nwjlyons
nwjlyons / keyword_arg.py
Last active June 18, 2019 10:16
Python 2/3 add keyword arg
# 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')