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 / merging.py
Created October 5, 2018 19:49
A useful helper script for merging dict attributes. Helps identify and resolve duplicates in (for instance) databases
import json
import pprint
from jsondiff import diff
def prompt_merge_attr(*attrs_list):
''' Given multiple versions of an attribute dictionary,
facilitate merging them into a single version.
'''
final_attrs = {}
all_attrs = set([
@u8sand
u8sand / bind.py
Created September 12, 2018 17:40
Bind positional and named arguments to function, creating a new function in python
def bind(func, *args, **kwargs):
''' Bind positional and named arguments to `func`.
e.g.
Input:
new_print = bind(print, '>', sep=' ', end='!\n')
Output:
new_print('Test')
> Test!
'''
def func_wrapper(*_args, **_kwargs):
@u8sand
u8sand / pyswagger_wrapper.py
Last active October 5, 2018 19:49
A wrapper around pyswagger to make it easier to use.
''' Usage:
from pyswagger_wrapper import SwaggerClient
client = SwaggerClient('your_swagger_url', headers={'auth': 'whatever', 'if': 'necessary'})
client.actions.your_op_id.call(your=params)
'''
import json
from urllib import request
from pyswagger import App
@u8sand
u8sand / no-proofpoint
Last active February 18, 2019 16:38
Online Office at my work likes to modify urls of raw text.. This script reverses the process.
#!/usr/bin/env python3
# Usage:
# no-proofpoint < broken_file.txt > fixed_file.txt
import re, sys
proofpoint_re = re.compile(r'https?://.+?/url\?u=(.+?)&d=.+?&e=')
encode_re = re.compile(r'-(\w{2})')
stdin = sys.stdin.read()
prev = 0
for match in proofpoint_re.finditer(stdin):
@u8sand
u8sand / parse_uri
Created March 4, 2018 14:51
Perl-based uri parser for bash scripts
#!/usr/bin/env sh
# Parse a normal uri--e.g. http://user:pass@host:1234 into a custom perl-based substitution format.
# e.g. parse_uri "http://localhost:80" '$+{port}' ==> 80
function parse_uri() {
URI=$1
FMT=$2
echo ${URI} | perl -pe "s#(?<proto>[\w\+-]+)://((?<user>[-\w]+):(?<pass>[-\w]+)@)?((?<host>[-\w]+)(:(?<port>\d+))?)#${FMT}#g"
}
@u8sand
u8sand / every_env.sh
Last active May 16, 2019 20:09
Script for installing a suite of local version managers for python, node, and ruby (pyenv, rbenv, nodenv)
#!/bin/sh
HOME="/home/${USER}"
PROFILE="${HOME}/.profile"
install_env() {
ENV_CMD="$1"
ENV_URL="$2"
ENV_ROOT="${HOME}/.${ENV_CMD}"
ENV_CMD_ROOT="$(echo ${ENV_CMD} | tr /a-z/ /A-Z/)_ROOT"
@u8sand
u8sand / Dockerfile
Last active February 8, 2022 04:14
Docker u8sand/archlinux-devel-yaourt (https://hub.docker.com/r/u8sand/archlinux-devel-yaourt/)
# Usage:
#FROM u8sand/archlinux-devel-yaourt
#RUN echo "Instaling dependencies..."
#RUN pacman -Sy --noconfirm && sudo -u docker yaourt -S --noconfirm all your packages
#
# Your scripts
#
#Optional: Remove privileged account for security reasons
#RUN userdel -r docker && rm /etc/sudoers.d/docker
@u8sand
u8sand / PKGBUILD
Last active July 29, 2018 09:51
Process the archlinux pacman mirrorlist for US
pkgname=('pacman-mirrorlist-hook')
pkgver=1
pkgrel=1
pkgdesc='Pacman mirrorlist hook.'
arch=('any')
depends=('pacman' 'pacman-mirrorlist')
source=('pacman-mirrorlist.hook' 'rank-mirror-list.sh')
md5sums=('b51e1a45b23b2831709e7732fe88d4c8'
'5cceded6741d705fd09b919de9e1234b')
@u8sand
u8sand / enforce.py
Last active August 13, 2017 21:06
Enforce new python3.6 type decorators at runtime with this decorator.
#!/bin/env python
def enforce(func):
''' Decorator to enforce type decorators at runtime via
type assertion. Usage:
@enforce
def foo(a : str, b : int, c = True : bool) -> str:
return None
'''
def enforce_wrapper(*args, **kwargs):
@u8sand
u8sand / rank-mirror-list
Created January 20, 2017 03:34
Script for ranking the mirrorlist.pacnew files that are updated every once in a while.
#!/bin/bash
if [ $(id -u) != 0 ]; then
exec sudo -- "$0" "$@"
else
cat /etc/pacman.d/mirrorlist.pacnew | awk -v RS='\n\n' '/^## United States/{print $0}' | awk '/^#Server/{print substr($0, 2)}' | rankmirrors - > /etc/pacman.d/mirrorlist
rm /etc/pacman.d/mirrorlist.pacnew
fi