Skip to content

Instantly share code, notes, and snippets.

View hello_timeout.sh
if
timeout 5s sleep 2;
then
echo 'OK';
else
echo 'Not OK';
fi
if
timeout 5s sleep 10;
@baojie
baojie / timeout.py
Last active Aug 29, 2015
Timeout any function
View timeout.py
from functools import wraps
import errno
import os
import signal
import time
# timeout any function in given seconds
# solution from http://stackoverflow.com/questions/2281850/timeout-function-if-it-takes-too-long-to-finish
class TimeoutError(Exception):
View disable_crontab_r
# Disable crontab -r
function crontab ()
{
# Replace -r with -e
/usr/bin/crontab "${@/-r/-e}"
}
View hello.py
(lambda _, __, ___, ____, _____, ______, _______, ________:
getattr(
__import__(True.__class__.__name__[_] + [].__class__.__name__[__]),
().__class__.__eq__.__class__.__name__[:__] +
().__iter__().__class__.__name__[_____:________]
)(
_, (lambda _, __, ___: _(_, __, ___))(
lambda _, __, ___:
chr(___ % __) + _(_, __, ___ // __) if ___ else
(lambda: _).func_code.co_lnotab,
@baojie
baojie / a.py
Created Dec 17, 2014
Test whether statements in a module will just run once under multiple imports
View a.py
print "run a from", __file__
@baojie
baojie / youtube_search_simple.py
Created May 4, 2014
Simplified youtube search example
View youtube_search_simple.py
from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.tools import argparser
# Get API key https://cloud.google.com/console
DEVELOPER_KEY = "REPLACE ME"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
View cloudflare.conf
# Cloudflare
set_real_ip_from 199.27.128.0/21;
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 188.114.96.0/20;
View hello_begins.py
import begin
@begin.subcommand
def add():
print "add"
@begin.subcommand
def dele():
print "dele"
@baojie
baojie / hashtest.py
Last active Aug 29, 2015
python hash function test
View hashtest.py
import timeit
import random
import string
data = ''.join([random.choice(string.ascii_letters) for x in range(1024*1024)])
print "md5",
print timeit.timeit('hashlib.md5().update(data)', number=10000, setup='import hashlib; data="%s"' % data)
print "sha1",
print timeit.timeit('hashlib.sha1().update(data)', number=10000, setup='import hashlib; data="%s"' % data)
View hello_attrdict.py
from attrdict import AttrDict
a = AttrDict({'foo': 'bar'})
a.foo
# OUT: 'bar'
a.foo == a['foo']
# OUT: True
a.foo = 'yeah'
a['foo']
# OUT: 'yeah'
foo in a