Skip to content

Instantly share code, notes, and snippets.

@hahastudio
hahastudio / prob_choice.py
Last active August 29, 2015 14:08
Probabilistic choice for python
import random
def prob_choice(items):
total_prob = sum(i[1] for i in items)
r = random.uniform(0, total_prob)
for value, prob in items:
if r < prob:
return value
r -= prob
return items[-1][0]
@hahastudio
hahastudio / gist:4c544040a8026e408600
Last active June 21, 2019 02:09
gist for https://www.v2ex.com/t/157851 , a small sample for class
class Foo(object):
def talk(self, name):
raise NotImplementedError
def fly(self, ID):
raise NotImplementedError
class AAA(Foo):
def talk(self, name):
print "AAA: talking to %s now" % name
def fly(self, ID):
################ Types
from __future__ import division
Symbol = str # A Lisp Symbol is implemented as a Python str
List = list # A Lisp List is implemented as a Python list
Number = (int, float) # A Lisp Number is implemented as a Python int or float
################ Parsing: parse, tokenize, and read_from_tokens
def required(argName):
def decorator(f):
def wrapper(*args, **kwarg):
if request.args.get(argName):
return f(*args, **kwarg)
else:
abort()
return wrapper
return decorator
@hahastudio
hahastudio / graph.json
Last active May 31, 2017 08:27
D3.js Force Layout
{
"nodes": [{
"url": "https://www.example.com/index.html"
},
{
"url": "https://www.example.com/category.html"
},
{
"url": "https://www.example.com/post/1"
},
@hahastudio
hahastudio / crashes.csv
Last active June 27, 2017 08:50
d3.js Bar Line chart
date deaths accidents
19830101 1068 107
19840101 1042 104
19850101 956 97
19860101 967 95
19870101 837 92
19880101 797 87
19890101 769 80
19900101 770 79
19910101 800 79
@hahastudio
hahastudio / .block
Last active June 30, 2017 03:44 — forked from mbostock/.block
Grouped Bar Chart
license: gpl-3.0
@hahastudio
hahastudio / static_file_server.py
Created February 28, 2018 14:18
A simple static file server add xpi file support. From https://www.acmesystems.it/python_http .
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from os import curdir, sep
PORT_NUMBER = 8080
#This class will handles any incoming request from
#the browser
class myHandler(BaseHTTPRequestHandler):
#Handler for the GET requests
@hahastudio
hahastudio / .block
Last active June 8, 2018 05:46 — forked from mbostock/.block
Line with Missing Data
license: gpl-3.0
result = dict()
filepath = r"D:\test.log"
with open(filepath, "r") as fin:
for line in fin:
parts = line.split(", ")
tag = parts[0]
value = float(parts[1])
result.setdefault(tag, []).append(value)
for k, v in result.iteritems():
print k, sum(v), sum(v)/len(v)