Skip to content

Instantly share code, notes, and snippets.

View lwzm's full-sized avatar
🎯
Focusing

Weizhao Li lwzm

🎯
Focusing
View GitHub Profile
#!/bin/sh
for i in $(seq 10); do
seq $(((i-1)*100)) $((i*100)) >$i
done
@lwzm
lwzm / python timeit
Created December 2, 2013 02:02
my simple version
#!/usr/bin/env python3
def f():
for _ in range(1000**2):
pass
import time
t0 = time.time()
f()
@lwzm
lwzm / backup.sh
Last active December 30, 2015 00:19
auto run something whith inotiry
#!/bin/sh
DUMP_FILE="dump"
while inotifywait -e modify,attrib $DUMP_FILE; do
while lsof $DUMP_FILE; do
echo "waiting..." && sleep 1
done
cp $DUMP_FILE $DUMP_FILE.$(date "+%Y-%m-%d--%T")
done
@lwzm
lwzm / patch_timeout.py
Last active December 30, 2015 14:39
make _Timeout(tornado.ioloop) visible
import datetime
import random
import time
import tornado.ioloop
import tornado.web
TIME = "%Y-%m-%d %H:%M:%S"
# patch
class Dict(dict):
def __missing__(self, k):
return k
namespace = Dict()
eval("{a:1, b:c, 3:3, _:4}", None, namespace)
# T_T... I'm too late, see library/stdtypes.html#str.format_map
>>> class Default(dict):
import os
import os.path
def walk(directory, dir_filter=None, file_filter=None):
"""return a list of all files in this directory, sorted"""
all_files = []
for root, dirs, files in os.walk(directory):
dirs[:] = sorted(filter(dir_filter, dirs))
all_files.extend(map(lambda f: os.path.abspath(os.path.join(root, f)),
sorted(filter(file_filter, files))))
def list_all_same_type(type_conv):
def conv(v):
return [conv(i) for i in v] if isinstance(v, list) else type_conv(v)
return conv
eval_orig = eval
def patch_eval():
if eval is not eval_orig:
return False
from functools import lru_cache
c = lru_cache(maxsize=1000)(compile)
e = eval_orig
@lwzm
lwzm / test.py
Last active August 29, 2015 13:57
a, b, *c = 1,2,3,4,5
*a, b = 1,2,3,4
a, *b, c = 1,2,3,4,5
[i for i, *_ in [[0], [1, 2], [3, 4, 5]]]
lst = [(0,), (1,), (2,), (3,), (4,), (5,), (6,), (7,), (8,), (9,)]
assert [i for i, in lst] == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
@lwzm
lwzm / login.sql
Last active August 29, 2015 14:01
create table users(
id integer primary key,
zone integer not null,
user text not null,
name text not null,
password text default '',
born real default 0.0,
ban real default 0.0,
login real default 0.0,
logout real default 0.0,