Skip to content

Instantly share code, notes, and snippets.

[etcd] Jul 31 03:41:53.475 INFO | 20634ec121f9479ca7a6cd50a5efcc0b: warning: heartbeat time out peer="86a6adecb8c
[etcd] Jul 31 03:42:24.976 INFO | 20634ec121f9479ca7a6cd50a5efcc0b: warning: heartbeat time out peer="86a6adecb8c
[etcd] Jul 31 03:44:00.477 INFO | 20634ec121f9479ca7a6cd50a5efcc0b: warning: heartbeat time out peer="86a6adecb8c
[etcd] Jul 31 03:44:32.225 INFO | 20634ec121f9479ca7a6cd50a5efcc0b: warning: heartbeat time out peer="86a6adecb8c
[etcd] Jul 31 03:45:36.475 INFO | 20634ec121f9479ca7a6cd50a5efcc0b: warning: heartbeat time out peer="86a6adecb8c
[etcd] Jul 31 03:46:10.975 INFO | 20634ec121f9479ca7a6cd50a5efcc0b: warning: heartbeat time out peer="86a6adecb8c
[etcd] Jul 31 03:46:43.976 INFO | 20634ec121f9479ca7a6cd50a5efcc0b: warning: heartbeat time out peer="86a6adecb8c
[etcd] Jul 31 03:47:12.563 INFO | 20634ec121f9479ca7a6cd50a5efcc0b: warning: heartbeat time out peer="86a6adecb8c
[etcd] Jul 31 03:47:16.475 INFO | 20634ec121f9479ca7a6cd50a5efcc0b:
@suzaku
suzaku / clean_movies.js
Created May 2, 2014 23:34
豆瓣电影首页[正在热映]中去掉分数低于7的条目
(function () {
var hot_movies = document.querySelectorAll('.ui-slide-item');
[].forEach.call(
hot_movies,
function (e) {
var rate = e.getAttribute('data-rate');
rate = parseInt(rate, 10);
if (!isNaN(rate) && rate < 7) {
e.remove();
}
@suzaku
suzaku / readme.md
Last active December 22, 2015 13:58
string concatenation performance experiment

method1-6 are taken from this article

method7 is added because I believe using generator expression would be faster

to run this experiment, you'll need to install line_profiler, memory_profiler and psutil first, all of which are just one pip install away.

after installing these profiling tools, we can run stest.py like this:

kernprof.py -l -v stest.py method1
@suzaku
suzaku / system.py
Created August 25, 2013 07:48
system in Python
import os
import sys
def my_system(command):
pid = os.fork()
if pid == -1:
return -1
elif pid == 0:
os.execv('/bin/sh', ['/bin/sh', '-c', command])
@suzaku
suzaku / test_sync
Last active December 21, 2015 04:09
O_DSYNC vs. O_SYNC vs. Call fsync manually vs. no sync
In [19]: fd = os.open('t1', os.O_CREAT | os.O_DSYNC | os.O_WRONLY)
In [20]: %timeit os.write(fd, 'a')
1000 loops, best of 3: 232 us per loop
In [21]: fd2 = os.open('t2', os.O_CREAT | os.O_SYNC | os.O_WRONLY)
In [22]: %timeit os.write(fd2, 'a')
1000 loops, best of 3: 737 us per loop

Pony Object-Relational Mapper

Pony is an object-relational mapper. The most interesting feature of Pony is its ability to write queries to the database using generator expressions. Pony works with entities which are mapped to a SQL database. Using generator syntax for writing queries allows the user to formulate very eloquent queries. It increases the level of abstraction and allows a programmer to concentrate on the business logic of the application. For this purpose Pony analyzes the abstract syntax tree of a generator and translates it to its SQL equivalent.

Following is an example of a query in Pony:

    select(p for p in Product if p.name.startswith('A') and p.cost <= 1000)
"""Compute mean and variance to normalize data"""
import dpark
import plac
import numpy as np
def prepare(s):
return np.array([float(i.strip()) for i in s.split(",")])
def main(filename):
lines = dpark.textFile(filename)
@suzaku
suzaku / normalize.py
Created December 21, 2012 02:23
normalize data with dpark
"""Compute mean and variance to normalize data"""
import dpark
import plac
import math
def prepare(s):
return [float(i.strip()) for i in s.split(",")]
def add_squared_list(vals):
squared_vals = [v * v for v in vals]
@suzaku
suzaku / try_plac.py
Created December 16, 2012 12:50
try plac
import plac
import random
@plac.annotations(name="your name")
def hello(name):
"Greet you half of the time, and otherwise * you"
if random.randint(0, 10) > 5:
print "Hello %s" % name
else:
print "Screw you. I'm ... going ... home~"
import random
import math
import csv
def hypothesis(params, features):
z = sum(p * f for p, f in zip(params, features))
return 1 / (1 + math.e ** -z)
def logistic_regression(learning_rate, samples):