Skip to content

Instantly share code, notes, and snippets.

View Xophmeister's full-sized avatar

Christopher Harrison Xophmeister

View GitHub Profile
@Xophmeister
Xophmeister / hello-world.sh
Last active January 21, 2020 13:20
Create a group of homogeneous machines, with SSH access, instance metadata and user data, in OpenStack
#!/usr/bin/env bash
declare INDEX="$(cloud-init query ds.meta_data.meta.index)"
declare TOTAL="$(cloud-init query ds.meta_data.meta.total)"
echo "Hello, World! This is instance $(( INDEX + 1 )) of ${TOTAL}." \
| tee /root/hello.txt
@Xophmeister
Xophmeister / thread_loop.py
Last active July 12, 2017 11:21
Minimal example of a Python event loop running in its own thread
"""
asyncio Demonstration
MIT License
Copyright (c) 2017 Genome Research Ltd.
"""
import asyncio
from threading import Lock, Thread
from typing import NamedTuple
@Xophmeister
Xophmeister / ldif2json
Created July 5, 2017 09:34
Quick-and-Dirty LDIF to JSON convertor
#!/usr/bin/env awk -f
# Convert LDIF into JSON
# MIT License
# Copyright (c) 2017 Christopher Harrison
function json_string(str) {
# Convert a string into an escaped JSON string, with enclosing quotes
return "\"" gensub(/"/, "\\\\\"", "g", str) "\""
@Xophmeister
Xophmeister / parse.py
Last active August 17, 2016 10:52
Simple S-Expression Evaluator
# Grammar:
# expr = number
# | "(" op expr+ ")"
# op = "+" | "-" | "*" | "/"
# number = "-"? DIGIT+
import re
from functools import reduce
from typing import Any, List, Optional, Tuple, Union
@Xophmeister
Xophmeister / is_valid.py
Last active August 4, 2016 20:55
Check for balanced parentheses recursively
import unittest
from enum import Enum
from typing import List, Union
class Parens(Enum):
brace = '()'
curly = '{}'
square = '[]'
@Xophmeister
Xophmeister / stack-test.rkt
Created February 23, 2016 10:19
Mutable stack implementation in Racket
#lang racket/base
(require rackunit
racket/match
"stack.rkt")
; Create stack
(make-stack test-stack)
; Is it a stack?
@Xophmeister
Xophmeister / bin2int.c
Created October 14, 2015 13:57
Binary string to integer
int bin2int(const char* s) {
int out = 0;
while (*s) {
out <<= 1;
if (*s == '1') ++out;
++s;
}
return out;
@Xophmeister
Xophmeister / foo.sh
Created September 14, 2015 09:59
Process substitution is neat :)
#!/bin/bash
# Change the group and group permissions for everything, recursively, in the cwd
find . | tee >(xargs chgrp -h NEW_GROUP) | xargs chmod -h g+rw
@Xophmeister
Xophmeister / human_size.c
Last active September 8, 2015 13:40
Quick-and-Dirty Human Size
#include <stdio.h>
#include <sys/types.h>
/**
@brief Human file size
@param size File size in bytes
@return Base 2 prefixed file size string
This isn't as complete as Gnulib's `human_readable`, but it does the
job without any allocation or messing around
@Xophmeister
Xophmeister / Makefile
Created September 3, 2015 10:07
Generic [GNU] Makefile for C projects on OS X (or otherwise), using pkg-config
# Output
TARGET = my_binary
# OS X default is a GCC frontend to LLVM
# Real GCC can be installed via, e.g., Homebrew
CC = gcc
LD = ld
# List of library dependencies
# pkg-config can be installed via, e.g., Homebrew