Skip to content

Instantly share code, notes, and snippets.

if
timeout 5s sleep 2;
then
echo 'OK';
else
echo 'Not OK';
fi
if
timeout 5s sleep 10;
@baojie
baojie / timeout.py
Last active August 29, 2015 14:22
Timeout any function
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):
# Disable crontab -r
function crontab ()
{
# Replace -r with -e
/usr/bin/crontab "${@/-r/-e}"
}
(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 December 17, 2014 03:48
Test whether statements in a module will just run once under multiple imports
print "run a from", __file__
@baojie
baojie / youtube_search_simple.py
Created May 4, 2014 19:35
Simplified youtube search example
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"
# 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;
import begin
@begin.subcommand
def add():
print "add"
@begin.subcommand
def dele():
print "dele"
@baojie
baojie / hashtest.py
Last active January 6, 2023 16:20
python hash function test
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)
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