Skip to content

Instantly share code, notes, and snippets.

@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"
},
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
################ 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
@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):
@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 / partial_eg.py
Last active August 29, 2015 14:06
Test functools.partial for https://www.v2ex.com/t/134401
from functools import partial
def func(a, b, c):
print a, b, c
func_fix_b = partial(func, b="what you want,")
func_fix_b(a="Here is", c="take it")
# Here is what you want, take it
func_fix_b("Here is", c="take it")
# Here is what you want, take it
@hahastudio
hahastudio / tree.md
Last active August 29, 2015 14:06 — forked from hrldcpr/tree.md

One-line Tree in Python

Using Python's built-in defaultdict we can easily define a tree data structure:

def tree(): return defaultdict(tree)

That's it!

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
d = {
"A0801_000000_201301": "1,321.8",
"A0801_000000_201302": "1,199.8",
"A0801_000000_201309": "1,433.4",
"A0802_000000_201305": "6,688.3",
"A0802_000000_201306": "8,085.2",
"A0802_000000_201307": "9,481.0",
"A0802_000000_201308": "10,878.4",
"A0802_000000_201309": "12,311.8",
"A0802_000000_201310": "13,739.9",
@hahastudio
hahastudio / simp_mapred.py
Last active August 29, 2015 14:04
A simple MapReduce model in Python, introducing the concept of MapReduce in word count problem
from itertools import groupby
# A sample input of a word count problem
source = ["Here is the first line in this source",
"And Here is the second line in this source",
"Welcome to the third line in this source"]
# map stage
map_result = map(lambda line: [(word.lower(), 1) for word in line.split()], source)
# [[('here', 1), ('is', 1), ('the', 1), ('first', 1),
# ('line', 1), ('in', 1), ('this', 1), ('source', 1)],
# [('and', 1), ('here', 1), ('is', 1), ('the', 1), ('second', 1),