Skip to content

Instantly share code, notes, and snippets.

View u8sand's full-sized avatar

Daniel J. B. Clarke u8sand

View GitHub Profile
@u8sand
u8sand / jsonb_deep_key_values.sql
Last active September 11, 2019 16:49
A postgresql function for enumerating deep key/value pairs of a jsonb object
create or replace function jsonb_deep_key_value (j jsonb)
returns table (key text, value jsonb)
as $$
with recursive t(key, value) as (
select
jj.key,
jj.value
from
jsonb_each(jsonb_build_object('', j)) as jj
union all (
@u8sand
u8sand / update-requirements.sh
Last active June 29, 2020 14:58
One-liners for ensuring requirements.txt / system deps match
# Produce a unified requirements.txt (no guarantees this comparison works properly for all versioning cases)
cat requirements.txt <(pip freeze) | awk 'BEGIN { FS="=="; OFS="==" } { if (V[tolower($1)] != "") { if ($2 != V[tolower($1)]) { print $1, $2 } else { print $1, V[tolower($1)] } } else { V[tolower($1)]=$2 } }'
# Produce a diff for manual inspection
cat requirements.txt <(pip freeze) | awk 'BEGIN { FS="=="; OFS="==" } { if (V[tolower($1)] != "") { if ($2 != V[tolower($1)]) { print "- "$1, V[tolower($1)]; print "+ "$1, $2 } else { print " "$1, $2 } } else { V[tolower($1)]=$2 } }'
@u8sand
u8sand / commandify.py
Last active August 22, 2019 18:42
Treat a set of python functions as a command-line application
# From https://gist.github.com/u8sand/b7f9f146c6349316e324d2261e5fd1ac
def inject(ctx=globals()):
''' [NOT SECURE]: Exposes a set of python functions as a python command-line application.
All functions exposed with docstring, argument names, defaults, annotations, etc...
The first line of a docstring is treated as the description; and the command-line spec is
automatically converted.
Usage: add the following to the bottom of your script
```
@u8sand
u8sand / duplicate_image_map.py
Last active February 24, 2019 19:48
A python script for dealing with duplicate images
#!/usr/bin/env python3
import os
import sys
import glob
import imagehash
from PIL import Image
from functools import reduce
from itertools import chain
@u8sand
u8sand / react-fitty.js
Created February 16, 2019 17:28
A demo of fitty with react for contained numbers
import React from 'react'
import fitty from 'fitty'
class Fitty extends React.Component {
fitty_props = ['minSize', 'maxSize', 'multiLine']
constructor(props) {
super(props)
this.init = this.init.bind(this)
@u8sand
u8sand / Dockerfile
Last active February 7, 2019 18:04
Simple nginx proxy config with environment variables
FROM nginx
RUN rm /etc/nginx/nginx.conf
ADD ./setup.sh /setup.sh
ENTRYPOINT [ "/bin/bash", "/setup.sh" ]
CMD [ "nginx", "-g", "daemon off;" ]
@u8sand
u8sand / range.js
Created January 7, 2019 20:33
A pythonic `range` function for javascript
export function range(start, end) {
if (end === undefined) {
end = start
start = 0
}
function *_range() {
for(let i = start; i < end; i++) {
yield i
}
import { matrix_flatten, matrix_slice, slice } from './matrix'
import { range } from './range'
export function count_first_na(L) {
for (let i = 0; i < L.length; i++) {
if (L[i] != null)
return i
}
throw new Error('NaNs not identified')
}
@u8sand
u8sand / matrix.js
Created January 7, 2019 20:30
Matrix numpy-style slice manipulations in Javascript
export function slice(left, right) {
return {
left,
right,
}
}
export function resolve_slice(s, l) {
if (s === null) {
return slice(0, l)
@u8sand
u8sand / tape_it.js
Last active December 8, 2018 18:05
Convert tape tests into mocha assertions!
var test = require('tape')
var assert = require('assert')
/**
* Convert tape tests into mocha assertions!
*
* @example
* tape_it('my mocha tape test assertion', function (test) {
* test('my tape test', function (t) {
* t.equal(1, 1)