Skip to content

Instantly share code, notes, and snippets.

View risicle's full-sized avatar

Robert Scott risicle

View GitHub Profile
@risicle
risicle / cache_chained_calculation.py
Created March 17, 2016 16:54
Django cache work-sharing using PostgreSQL advisory locks
from django.core.cache import cache
from django.core.cache.backends.base import DEFAULT_TIMEOUT
from django.db import connection , transaction
from hashlib import md5
def cache_chained_calculation(characteristic_str, calculate_function, timeout=DEFAULT_TIMEOUT, force_update=False):
"""
Attempt to obtain result of @calculate_function, represented by @characteristic_str, through cache or calling the
function. Should only allow one caller to be calculating the value at once (enforced using postgres advisory locks),
import datetime
def apply_calendar_month_delta(datetime_in, month_delta):
"""
Applies an (integer) @month_delta to @datetime_in. Cheats on days, clamping them to (0,28] to avoid complexities.
>>> from datetime import date
>>> apply_calendar_month_delta(date(2014, 1, 3), -1)
datetime.date(2013, 12, 3)
>>> apply_calendar_month_delta(date(2014, 9, 3), 6)
@risicle
risicle / retry_transaction_decorator.py
Created March 17, 2016 18:14
Django @retry_transaction decorator
from django.db import transaction, IntegrityError, DatabaseError
from functools import wraps
import logging
_transaction_logger = logging.getLogger("db.transactions")
def retry_transaction(attempts=2, on_failure=None, logger=_transaction_logger):
"""
Decorator for catching db errors and retrying the operation.
@risicle
risicle / nix-daemon.service
Created January 15, 2017 12:52
Nix daemon systemd service (from various sources)
[Unit]
Description=Nix daemon
[Service]
ExecStart=/nix/var/nix/profiles/default/bin/nix-daemon
Nice=15
IOSchedulingClass=3
Environment="CURL_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt"
[Install]

Keybase proof

I hereby claim:

  • I am risicle on github.
  • I am r_i_s (https://keybase.io/r_i_s) on keybase.
  • I have a public key whose fingerprint is 8868 8AE4 8AE6 3195 BCF5 F732 3A7B 7B7A 2611 CE25

To claim this, I am signing this object:

import png
from PIL import Image, ImageMath
import array
import math
def _planes_from_png(filename):
with open(filename, "rb") as png_file:
reader = png.Reader(filename)
@risicle
risicle / default.nix
Created July 29, 2018 18:32
default.nix for josm development (retarget as needed for different java versions)
{
pkgs ? import <nixpkgs> {}
}:
let
oraclejdk11 = pkgs.oraclejdk10.overrideAttrs (oldAttrs: {
src = pkgs.requireFile {
name = "jdk-11-ea+24_linux-x64_bin.tar.gz";
url = "http://jdk.java.net/11/";
sha256 = "51430792cd9955c7e41792964bede9fe7e4b2aae084986b8575467db75203a3a";
};
@risicle
risicle / template_ocr.py
Created March 3, 2019 16:57
Collection of utility functions to implement rudimentary template-matching based OCR using OpenCV, intended for use with large, uniform blocks of monospaced, non-language-correlated text (effectively resulting in a "character grid") where a 100.0% accuracy is required. This could be useful if one, for example, found themselves needing to recover…
"""
Collection of utility functions to implement rudimentary template-matching based OCR using OpenCV,
intended for use with large, uniform blocks of monospaced, non-language-correlated text (effectively
resulting in a "character grid") where a 100.0% accuracy is required. This could be useful if one, for
example, found themselves needing to recover a hardcopy-printed backup of an OpenPGP key.
Modern general-purpose OCR software doesn't tend to do that well with these, relying heavily on
language-context-based guessing, often trying to be too clever in layout auto-detection, all the while
being extremely tricky to configure precisely enough to allow us to impart our existing knowledge
about the constraints of the target text (expected alphabet, line length, expected font...).
building '/nix/store/cy97ghyfamlnaibslwb43f3r40y17zb0-python3.7-salmon-mail-3.1.0.drv'...
unpacking sources
unpacking sources
unpacking source archive /nix/store/c2kalm4pn4vrp98vsq75si01793rz65l-salmon-mail-3.1.0.tar.gz
unpacking source archive /nix/store/c2kalm4pn4vrp98vsq75si01793rz65l-salmon-mail-3.1.0.tar.gz
source root is salmon-mail-3.1.0
source root is salmon-mail-3.1.0
setting SOURCE_DATE_EPOCH to timestamp 1547748991 of file salmon-mail-3.1.0/setup.cfg
setting SOURCE_DATE_EPOCH to timestamp 1547748991 of file salmon-mail-3.1.0/setup.cfg
patching sources
building '/nix/store/k1wh7gwnjrg1wf6dxvslr3rwspywpbyq-nip2-8.3.0.drv'...
unpacking sources
unpacking source archive /nix/store/q27lq2k4miccpmzfgm6avlgqchxfc1f9-nip2-8.3.0.tar.gz
source root is nip2-8.3.0
setting SOURCE_DATE_EPOCH to timestamp 1463600557 of file nip2-8.3.0/po/nip2.pot
patching sources
configuring
fixing libtool script ./ltmain.sh
configure flags: --disable-static --disable-dependency-tracking --prefix=/nix/store/5wqncbrm38hv94rsfrda2xvpjrx41559-nip2-8.3.0
checking for a BSD-compatible install... /nix/store/bv1lw6a2kw0mn2y3lxhi43180idx6sp9-coreutils-8.31/bin/install -c