Skip to content

Instantly share code, notes, and snippets.

View mortehu's full-sized avatar

Morten Hustveit mortehu

  • Jump Trading
  • New York, New York
View GitHub Profile
@mortehu
mortehu / gist:5469769
Last active December 16, 2015 17:19
Fix spelling mistakes in bash command lines
# BEGIN: Put this in ~/.bashrc
function err_handle {
if [ "$?" != 0 ]
then
if echo "$previous_command" | grep -q '\<hlep\>'
then
NEW_COMMAND="$(echo "$previous_command" | sed 's/\<hlep\>/help/g')"
echo -ne "Command failed. Retry with '$NEW_COMMAND' (y/n)? "
read p
@mortehu
mortehu / gist:5579441
Created May 14, 2013 20:51
Retrieve missing episodes of Above & Beyond: Group Therapy Radio
#!/usr/bin/env python
from atomicfile import AtomicFile
import feedparser
import os
import re
import urllib2
import urlparse
feed = feedparser.parse("http://www.tatw.co.uk/podcast.xml")
@mortehu
mortehu / gist:6225231
Created August 13, 2013 20:15
Automatically install missing programs after failing to start
#!/bin/bash
function @()
{
if [ $? = 127 ]
then
PROGRAM=$(history 2 | head -n 1 | tr -s \ \ | cut -f3 -d\ | tr -d '[:space:]')
PATTERN="/s?bin/$PROGRAM\$"
SHORT_PATTERN="$PROGRAM"
elif [ $1 != "" ]
@mortehu
mortehu / gist:d9f9671dd4972a4c0c57
Last active August 29, 2015 14:10
libstdc++ hash function extracted
uint64_t Hash(const StringRef& key) {
static const uint64_t mul = (0xc6a4a793UL << 32UL) + 0x5bd1e995UL;
auto shift_mix = [](uint64_t v) -> uint64_t { return v ^ (v >> 47); };
auto len = key.size();
auto buf = key.data();
const auto len_aligned = len & ~7;
const auto end = buf + len_aligned;
@mortehu
mortehu / error.c
Created December 11, 2014 22:26
Error reporting
static __thread char* last_error;
const char* last_error(void) {
return last_error ? last_error : strerror(errno);
}
void clear_error(void) {
free(last_error);
last_error = NULL;
}
@mortehu
mortehu / google-cloud-storage-list.py
Created April 16, 2015 15:18
Minimal Google Cloud Storage API Python example
#!/usr/bin/env python
"""Sample Google Cloud Storage API client.
Based on <https://cloud.google.com/storage/docs/json_api/v1/json-api-python-samples>,
but removed parts that are not relevant to the Cloud Storage API.
Assumes the use of a service account, whose secrets are stored in
$HOME/google-api-secrets.json"""
@mortehu
mortehu / reboot.md
Last active August 29, 2015 14:20
Checklist for resilient reboot of Debian and Ubuntu servers

/etc/default/rcS

DELAYLOGIN=no
FSCKFIX=yes

/etc/fstab

Add the nofail option to as many mount points as possible. This will allow the boot process to continue even if a file system is corrupted.

Dropbear

@mortehu
mortehu / autoskeleton.sh
Created June 1, 2015 20:58
Script to generate minimal Makefile.am and configure.ac for a C++ project
#!/bin/bash
echo "Press Ctrl-C at any time to abort Makefile generation"
echo
echo "The project name should start with a letter and contain nothing but "
echo "letters (A-z), digits (0-9) and dashes ('-'). Do not include the "
echo "version number."
echo
echo "Example: hello-world"
@mortehu
mortehu / zip-test.cc
Last active May 23, 2022 12:00
C++ zip
#include <cstdio>
#include <list>
#include <vector>
#include "zip.h"
int main() {
std::vector<int> one{{1, 11}};
auto two = [] { return std::vector<short>{{2, 22}}; };
const std::list<float> three{{3, 33}};
@mortehu
mortehu / smooth-histogram.py
Created July 15, 2016 14:41
Smooth histograms
#!/usr/bin/env python
# Generates a curve for drawing a smooth histogram for a set of real values
# provided on the standard input.
# Copyright (C) 2016 Morten Hustveit
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or