Skip to content

Instantly share code, notes, and snippets.

@samba
samba / csv-to-json.py
Created November 25, 2012 07:53
Very simple CSV to JSON converter
#!/usr/bin/env python
# Very simple CSV to JSON converter
# Supports CSV labels
# Usage:
# (without labels)
# csv-to-json.py ./data.csv > ./data.json
# like: [ [ 1, 2, 3 ] ]
# (with labels)
# csv-to-json.py -l ./data.csv > ./data.json
@samba
samba / find_superblock.sh
Created November 28, 2012 05:38
Superblock finder
#!/bin/sh
# A tool to find superblocks in an EXT4 volume (probably easily adapted for EXT2,3)
# The usecase for this tool is likely pretty narrow...
# I have an LVM volume on a RAID array that was incorrectly reassembled (i.e. recreated)
# in the wrong order (I think), and I'm trying to recover from it, but it's unrecognizable
# as a filesystem since the primary superblock is missing.
# This tool identifies all (seemingly) valid superblocks in a volume, though using them may
# introduce other errors if your filesystem is badly damaged. USE WITH CAUTION.
# The goal is to estimate how far offset the superblocks are and if disc order in the array
@samba
samba / restore.sh
Created November 28, 2012 07:14
Compressed disc image restoration with "threading"
#!/bin/sh
# Assumes images are broken out into 1024mb chunks, separately gzipped,
# stored as ./image/image.X.blob.gz, where X is an integer (0...).
# Decompresses and writes 6 in parallel (i.e. threading) since Gzip is slow.
DEVICE=$1
sectionsize=1024
maxthreads=5
curthreads=0
@samba
samba / host_setup.sh
Created December 6, 2012 21:33
LXC Setup script
#!/bin/bash
# THIS IS A WORK IN PROGRESS, might break things.
# Sets up LXC host configuration in AWS EC2 environments
# Reference: http://www.activestate.com/blog/2011/10/virtualization-ec2-cloud-using-lxc
# Exit NOW because we don't want to blow anything up outside our test systems
# (This is being drafted in a production environment.)
exit 1
echo 'none /sys/fs/cgroup cgroup defaults 0 0' >> /etc/fstab
@samba
samba / simpleformat.140bytes.js
Last active October 13, 2015 18:08
Simple Javascript String Formatter
// This one only supports the '{0}' syntax for now...
function _format(p){var x = arguments;return p.replace(/\{([0-9]+)\}/g, function($0,n){ return x[1+Number(n)] })}
@samba
samba / README.md
Last active November 4, 2015 06:34
A breadth-first graph search, generates paths with sorted (least-first) cost

Breadth-first Graph Search

Generates least-cost paths from an adjacency list with one cost factor per edge.

Why did @samba write this?

Simply because I needed the refresher, having taken my last Computer Science class nearly a decade ago, and having focused on a different domain of problems in recent years.

I wanted the practice. :)

@samba
samba / compat.sql
Created November 17, 2015 23:32
Redshift Compatibility Functions
/* Functions available in Redshift that can be emulated in Postgres for compatibility.
* This is most applicable when a schema needs to be migrated into a (local) PostgreSQL
* environment for modeling/development. As Redshift is derived from PostgreSQL, many
* DBA and related modeling tools designed for PostgreSQL are _not quite_ viable for
* use with Redshift. These functions attempt to augment native PostgreSQL to allow
* Redshift SQL to run in other Postges environments.
* I'll update this periodically with additional functions as needed.
*/
CREATE FUNCTION public.getdate() returns timestamptz
@samba
samba / crypto.sh
Last active December 12, 2015 06:08
OpenSSL for file encryption
## to encrypt
openssl aes-256-cbc -e -in plaintext.txt -out crypted.blob
## to decrypt
openssl aes-256-cbc -d -in crypted.blob -out plaintext.txt
@samba
samba / replace.py
Last active December 13, 2015 17:19
PERL-style RegExp substitution in Python (but within "s/.../.../", uses Python syntax)
#!/usr/bin/env python
# Python support for PERL's shorthand of pattern substitution (s/.../.../)
# Supports flags:
# i => case insensitive
# m => multiline match (caret and dollar represent line terminations, not whole-string)
# s => "dot-all" - dot matches everything, including new-line
# g => "global" - replace every instance (without this, only the first)
import re
@samba
samba / tricks.sh
Last active December 15, 2015 17:49
Shell tricks
#!/bin/sh
# Show duplicate files (compared by MD5 hash)
find ./ -type f -print0 | xargs -0 md5sum | sort | uniq -w 32 -D
# Remove smart-quotes from a buffer/file
sed "s/[”“]/\"/g; s/[’‘]/'/g"
# Rename a series of files (e.g. restore backups) by regular expression in OS X
find ./ -type f -name '*.bak' -print | sed -E 's/(.*)\.bak$/mv "&" "\1"/' > rename.sh