Skip to content

Instantly share code, notes, and snippets.

View jmoiron's full-sized avatar

Jason Moiron jmoiron

View GitHub Profile
func LoadGzippedJSON(r io.Reader, v interface{}) error {
raw, err := gzip.NewReader(r)
if err != nil {
return err
}
return json.NewDecoder(raw).Decode(&v)
}
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 ReadAll(r io.Reader) ([]byte, error)
@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() {
+++ Statistics Dump +++ (1393879884)
++ Incoming Requests ++
7122195 QUERY
1 IQUERY
4 STATUS
1 NOTIFY
++ Incoming Queries ++
5189989 A
81035 NS
1754 CNAME
@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 / 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 / color.go
Last active December 14, 2015 19:29
very simple console color escapes for python & go
const (
white = iota + 89
black
red
green
yellow
blue
purple
)
package main
import (
"bytes"
"fmt"
"github.com/moovweb/gokogiri"
"github.com/moovweb/gokogiri/css"
"github.com/moovweb/gokogiri/html"
"github.com/moovweb/gokogiri/xml"
"github.com/moovweb/gokogiri/xpath"
@jmoiron
jmoiron / gist:3995399
Created November 1, 2012 18:00
xml wrapping functions for lxml-ish css selection
// Selectable implements a simple interface which allows to get the inner text
// of some element as well as run a CSS select on it and get a list of nodes
type Selectable interface {
CssSelect(selector string) []Node
Text() string
}
// A node wrapper, in order to provide a similar interface in the future
// possibly without gokogiri
type Node struct {