Skip to content

Instantly share code, notes, and snippets.

View kmohrf's full-sized avatar

kmohrf kmohrf

View GitHub Profile
@kmohrf
kmohrf / apt_panic_1
Created May 26, 2021 21:49
Raspberry Pi Zero Kernel Panics
Panic: apt update && apt upgrade -y
SD: practically unmodified (enable uart, wifi configuration)
Connected Peripherals:
* UART via FTDI TTL-232R
* (using internal wifi)
[ 1316.502504] 8<--- cut here ---
[ 1316.507168] Unable to handle kernel paging request at virtual address bf8447b8
[ 1316.516107] pgd = 9d6bdbd4
[ 1316.520427] [bf8447b8] *pgd=00000000
from typing import Any, Iterable, Tuple
def partition_by_type(iterable: Iterable[Any], *args: Tuple[type], with_rest=False):
"""Partitions iterable by types.
Creates as many iterators as type tuples have been given and optionally
returns another iterator for all non-matching instances."""
iterator = iter(iterable)
types_map = {
types: collections.deque()
for types in args
@kmohrf
kmohrf / migration_util.py
Last active June 2, 2020 20:10
Wagtail StreamField migration for block type name changes
__license__ = "MIT"
__copyright__ = "Copyright 2020, Konrad Mohrfeldt"
from functools import partial
from typing import Callable, Iterable, Union
from django.db import migrations
from django.db.migrations.state import StateApps
from django.db.models.base import ModelBase
from wagtail.core.blocks.stream_block import StreamValue
@kmohrf
kmohrf / draftail-api-monkeypatch.js
Last active August 23, 2021 10:15
Wagtail Replace-Quote-Plugin for Draftail/DraftJS
(function (draftail) {
const registeredPlugins = []
const initEditor = draftail.initEditor
draftail.registerDraftPlugin = function (plugin) {
registeredPlugins.push(plugin)
}
draftail.initEditor = function (selector, options, currentScript) {
const plugins = (options.plugins || []).concat(registeredPlugins)
const newOptions = Object.assign({}, options, { plugins: plugins })
@kmohrf
kmohrf / root-shell
Last active May 3, 2018 20:47
user aware root-shell
#!/bin/sh
set -eu
# we only want to run this script as root
if [ ! "$(id -u)" = 0 ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
@kmohrf
kmohrf / brightness.py
Created February 25, 2017 13:34
Calculate Image brightness with Python Pillow
import sys
from PIL import Image
def calculate_brightness(image):
greyscale_image = image.convert('L')
histogram = greyscale_image.histogram()
pixels = sum(histogram)
brightness = scale = len(histogram)
#!/usr/bin/env python
# encoding: utf-8
def weirdness_happens(foo = []):
foo.append(1)
return foo
print(weirdness_happens()) # [1]
print(weirdness_happens()) # [1, 1]
@kmohrf
kmohrf / test.js
Last active June 8, 2016 14:43
default argument implementations with referenceable datatypes in .js and .py
const create_object = (obj = {}) => obj
console.log(create_object() === create_object()) // false