Skip to content

Instantly share code, notes, and snippets.

View microamp's full-sized avatar
💭
>°))彡

Sangho Na microamp

💭
>°))彡
  • Auckland, New Zealand
  • 11:34 (UTC +12:00)
View GitHub Profile
@microamp
microamp / timeit.hy
Created February 19, 2014 22:29
A simple 'timeit' macro in Hy
(import time)
(defmacro timeit [expr]
`(let [[start# ((. time time))]]
(let [[val# ~expr]]
(let [[end# ((. time time))]]
(print (+ "time elapsed: "
(str (* (- end# start#) 1000))
" msecs"))))))
@microamp
microamp / immutable.py
Last active October 16, 2023 05:30
How to make immutable classes in Python
#-*- coding: utf-8 -*-
from collections import namedtuple
class Immutable(namedtuple("Immutable", "x, y")):
"""Immutable class using collections.namedtuple."""
def __str__(self):
return "class: {c}, x: {x}, y: {y}".format(c=Immutable.__name__,
x=self.x, y=self.y)
@microamp
microamp / lazy.md
Created June 5, 2014 00:35
Python: Lazy vs Strict
In [1]: from operator import add

In [2]: reduce(add, map(add, range(1000000), range(1000000)))
Out[2]: 999999000000

In [3]: %timeit reduce(add, map(add, range(1000000), range(1000000)))
10 loops, best of 3: 153 ms per loop

In [4]: %timeit reduce(add, map(add, xrange(1000000), xrange(1000000)))
@microamp
microamp / gapi.go
Last active March 30, 2020 13:02
Calling Google API call in Go
package main
import (
"bytes"
"io"
"log"
"os"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
@microamp
microamp / gapi.py
Last active April 24, 2016 03:34
Calling Google API call in Python
#-*- coding: utf-8 -*-
from pprint import pprint
import json
import os
from oauth2client.client import SignedJwtAssertionCredentials
import httplib2
ENV_SERVICE_ACCOUNT = "GAPI_SERVICE_ACCOUNT"
@microamp
microamp / avro_ser_deser.go
Created April 4, 2016 08:42
Avro serialisation/deserialisation example
package main
import (
"bytes"
"log"
"github.com/linkedin/goavro"
)
const schemaPerson = `
@microamp
microamp / NetworkWordCount.scala
Last active April 24, 2016 03:30
The simplest Spark Streaming example (stateless)
import org.apache.spark._
import org.apache.spark.streaming._
object NetworkWordCount {
def main(args: Array[String]) {
// Create a local StreamingContext with two working thread and batch interval of 1 second.
// The master requires 2 cores to prevent from a starvation scenario.
val conf = new SparkConf().setMaster("local[8]").setAppName("NetworkWordCount")
val ssc = new StreamingContext(conf, Seconds(5))
@microamp
microamp / StatefulNetworkWordCount.scala
Created April 24, 2016 03:31
The simplest Spark Streaming example (state updated using `updateStateByKey`)
import org.apache.spark._
import org.apache.spark.streaming._
object StatefulNetworkWordCount {
def updateFunction(newValues: Seq[Int], runningCount: Option[Int]): Option[Int] = {
val newCount = runningCount.getOrElse(0) + newValues.sum
Some(newCount)
}
def main(args: Array[String]) {
@microamp
microamp / StatefulNetworkWordCount2.scala
Created April 24, 2016 03:33
Same as StatefulNetworkWordCount but with `mapWithState`
import org.apache.spark._
import org.apache.spark.streaming._
object StatefulNetworkWordCount2 {
def updateFunction2(word: String, one: Option[Int], state: State[Int]) = {
val sum = one.getOrElse(0) + state.getOption.getOrElse(0)
val output = (word, sum)
state.update(sum)
output
}
@microamp
microamp / handlers.go
Last active November 24, 2016 10:56
http.HandleFunc and http.Handle
// type HandlerFunc func(ResponseWriter, *Request)
//
// func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
// f(w, r)
// }
//
// func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
// DefaultServeMux.HandleFunc(pattern, handler)
// }