Using Python's built-in defaultdict we can easily define a tree data structure:
def tree(): return defaultdict(tree)
That's it!
{ | |
"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 |
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): |
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] |
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 |
Using Python's built-in defaultdict we can easily define a tree data structure:
def tree(): return defaultdict(tree)
That's it!
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", |
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), |