Skip to content

Instantly share code, notes, and snippets.

#!/bin/env python
#^_^ encoding: utf-8 ^_^
# @date: 14-5-4
import threading
import Queue
class WorkerPoolError(Exception):
pass
@icejoywoo
icejoywoo / decorator_demo.py
Last active August 29, 2015 14:00
Python decorator usage.
#!/bin/env python
#^_^ encoding: utf-8 ^_^
# @date: 14-5-4
import types
def wrapper(*args, **kwargs):
print args, kwargs
#!/bin/bash
# 显示脚本的各种调用命令
set -x
# 一但有任何一个语句返回非真的值,则退出bash
set -e
# 你使用未初始化的变量时,让bash自动退出
set -u
SCRIPT_PATH="${BASH_SOURCE[0]}";
@icejoywoo
icejoywoo / skiplist.py
Last active August 29, 2015 14:04
A simple skiplist implementation in python.
# ^_^ encoding: utf-8 ^_^
# @brief:
# @class:
# @date: 2013-8-31
# @author: wujiabin
class SkipListError(Exception):
pass
class Node(object):
@icejoywoo
icejoywoo / skiplist.c
Last active February 8, 2022 15:21
Example of Skip List source code for C
/*
Example of Skip List source code for C:
Skip Lists are a probabilistic alternative to balanced trees, as
described in the June 1990 issue of CACM and were invented by
William Pugh in 1987.
This file contains source code to implement a dictionary using
skip lists and a test driver to test the routines.
#!/bin/env python
""" Copied from http://code.activestate.com/recipes/82234-importing-a-dynamically-generated-module/.
This is a sample code to show how to load python code string to a module dynamically.
"""
# Importing a dynamically generated module
def importCode(code,name,add_to_sys_modules=0):
"""
#!/bin/env python
# ^_^ encoding: utf-8 ^_^
# @date: 2014/8/8
"""
worker_pool
简单的线程池, 提交任务即可, 会后台执行, 并将结果返回
示例见main部分
TODO
1. 清空status的时机, 删除已经返回的key, 否则内存要爆
"""
"""
Code to timeout with processes.
>>> @timeout(.5)
... def sleep(x):
... print "ABOUT TO SLEEP {0} SECONDS".format(x)
... time.sleep(x)
... return x
>>> sleep(1)
def sbv0(adict, reverse=False):
return sorted(adict.iteritems(), key=lambda (k, v): (v, k), reverse=reverse)
def sbv1(d, reverse=False):
L = [(k, v) for (k, v) in d.iteritems()]
return sorted(L, key=lambda x: x[1], reverse=reverse)
def sbv2(d, reverse=False):
# based on Recipe 466302: Sorting big files the Python 2.4 way
# by Nicolas Lehuen
import os
from tempfile import gettempdir
from itertools import islice, cycle
from collections import namedtuple
import heapq
Keyed = namedtuple("Keyed", ["key", "obj"])