Skip to content

Instantly share code, notes, and snippets.

View jmoiron's full-sized avatar

Jason Moiron jmoiron

View GitHub Profile
@jmoiron
jmoiron / autogobuild.sh
Created August 2, 2012 23:27
Run this script in your directory to auto build whenever you modify a golang file
#!/bin/bash
cur=`pwd`
inotifywait -mqr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' \
-e modify ./ | while read date time dir file; do
ext="${file##*.}"
if [[ "$ext" = "go" ]]; then
echo "$file changed @ $time $date, rebuilding..."
go build
@jmoiron
jmoiron / blargparse.py
Created September 12, 2013 21:02
allow certain arguments to short circuit required positional arguments and other argparse errors
#!/usr/bin/env python
# I had a script which I wanted to have a special option that short-circuited the normal
# argument parsing error handling behavior so that it could be run without thenormal
# required arguments. This option would work similar to how `--help` or `--version`
# works, except that the parser would return a valid args object in its presence
# rather than exiting the program.
import argparse
@jmoiron
jmoiron / valuer.go
Created October 14, 2013 18:03
Example uses of sql.Scanner and driver.Valuer
package main
import (
"bytes"
"compress/gzip"
"database/sql/driver"
"errors"
"fmt"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
@jmoiron
jmoiron / 01-curl.go
Last active December 16, 2022 10:34
io.Reader & io.Writer fun
package main
import (
"fmt"
"io"
"net/http"
"os"
)
func init() {
@jmoiron
jmoiron / crawler.py
Created May 27, 2011 20:37
Simple gevent/httplib2 web crawler.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Simple async crawler/callback queue based on gevent."""
import traceback
import logging
import httplib2
import gevent
@jmoiron
jmoiron / livescores.py
Created September 11, 2012 02:19
scrape livescores.com
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" """
import json
from urllib2 import urlopen
from lxml.html import document_fromstring
from lxml.cssselect import CSSSelector as cs
@jmoiron
jmoiron / shm_test.c
Created April 2, 2015 20:07
if you mmap an shm_open created fd, then ftruncate+mmap again, do you lose the memory you've been writing to?
#include <stdbool.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
void print_region(char *ptr, size_t len);
// mark a few locations in a pointer
void mark(char *ptr, size_t len) {
>>> humanize.naturalsize(1<<30)
'1.1 GB'
>>> humanize.naturalsize(1<<30, binary=True)
'1.0 GiB'
humanize.naturalsize(1<<30, gnu=True)
'1.0G'
@jmoiron
jmoiron / bongo.sh
Created June 30, 2012 21:58
export and import all collections from a mongodb database into/outof a single tarball
#!/bin/bash
LOADING=false
usage()
{
cat << EOF
usage: $0 [options] dbname
OPTIONS:
~$ chef-shell -wat
/opt/chefdk/embedded/lib/ruby/gems/2.3.0/gems/mixlib-cli-1.7.0/lib/mixlib/cli.rb:230:in `parse_options': invalid option: -wat (OptionParser::InvalidOption)
from /opt/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.19.36/lib/chef/shell.rb:288:in `parse_opts'
from /opt/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.19.36/lib/chef/shell.rb:178:in `parse_opts'
from /opt/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.19.36/lib/chef/shell.rb:56:in `start'
from /opt/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.19.36/bin/chef-shell:34:in `<top (required)>'
from /usr/bin/chef-shell:57:in `load'
from /usr/bin/chef-shell:57:in `<main>'