This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################ 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nodes": [{ | |
"url": "https://www.example.com/index.html" | |
}, | |
{ | |
"url": "https://www.example.com/category.html" | |
}, | |
{ | |
"url": "https://www.example.com/post/1" | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |