Skip to content

Instantly share code, notes, and snippets.

@jmoiron
jmoiron / 01-curl.go
Last active October 19, 2024 11:28
io.Reader & io.Writer fun
package main
import (
"fmt"
"io"
"net/http"
"os"
)
func init() {
func ReadAll(r io.Reader) ([]byte, error)
func LoadGzippedJSON(r io.Reader, v interface{}) error {
data, err := ioutil.ReadAll(r)
if err != nil {
return err
}
// oh wait, we need a Reader again..
raw := bytes.NewBuffer(data)
unz, err := gzip.NewReader(raw)
if err != nil {
return err
func LoadGzippedJSON(r io.Reader, v interface{}) error {
raw, err := gzip.NewReader(r)
if err != nil {
return err
}
return json.NewDecoder(raw).Decode(&v)
}
# this is how we program for some reason
ls > files.txt
grep "foo" files.txt > grepped.txt
wc -l grepped.txt
rm files.txt grepped.txt
# github search for "json.loads(" => 210,000 matches
feed = urllib2.urlopen("http://example.com/api.json").read()
data = json.loads(feed)
# github search for "json.load(" => 58,000 matches
data = json.load(urllib2.urlopen("http://example.com/api.json"))
@jmoiron
jmoiron / Makefile
Last active December 23, 2015 19:09
monte carlo pi estimation in different languages
all: monte-c monte-go monte-rs monte-gccgo
monte-go:
go build montepi.go && mv montepi monte-go
monte-rs:
rustc -O -o monte-rs montepi.rs
monte-c:
gcc -std=c99 -O2 -o monte-c montepi.c -lm
@jmoiron
jmoiron / install.sh
Last active June 20, 2016 21:52
jinkies is a very simple jenkins cli
#!/bin/sh
python -c 'import docopt'
if [ $? != 0 ]; then
echo "Missing docopt module."
echo " pip install docopt"
echo ""
echo "or for ubuntu/debian users:"
echo " apt-get install python-docopt"
@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) {
@jmoiron
jmoiron / synpad.py
Last active August 29, 2015 14:22
synpad
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Simple synaptics client save/restore."""
import argparse
import sys
import os.path
from collections import OrderedDict
from subprocess import *