Skip to content

Instantly share code, notes, and snippets.

@sanpingz
sanpingz / domain.py
Last active April 6, 2017 14:56
检测未被注册的域名
#!/usr/bin/env python
# coding: utf-8
"""
domain.py
简单的whois客户端,可用于判断域名是否被注册
python domain.py [num] # num为字母组合的个数(1,8)
"""
import urllib, re, os, sys
@sanpingz
sanpingz / func_cache.py
Last active December 16, 2015 06:48
装饰器实现的函数缓存器
import time
def timer(func):
def newFunc(*pargs, **kargs):
start = time.time()
back = func(*pargs, **kargs)
print "Timer: %.2fs" % (time.time()-start)
return back
return newFunc
@sanpingz
sanpingz / lcm.py
Last active December 16, 2015 05:09
最小公倍数
#!/usr/bin/env python
# coding: utf-8
# 最大公约数
#欧几里德算法又称辗转相除法,用于计算两个整数m, n的最大公约数。其计算原理依赖于下面的定理:
# gcd(m, n) = gcd(n, m mod n)
#这个定理的意思是:整数m、n的最大公约数等于n和m除以n的余数的最大公约数。
def gcd(m, n):
while n:
m, n = n, m % n
@sanpingz
sanpingz / cookie.js
Created August 21, 2012 00:48
Cookie操作
@sanpingz
sanpingz / _vimrc
Last active March 28, 2017 02:14
vim配置文件
cd C:\Users\Calvin
set nocompatible
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswin
set diffexpr=MyDiff()
function MyDiff()
let opt = '-a --binary '
if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
@sanpingz
sanpingz / ProducerConsumer.java
Created July 7, 2012 05:13
生产者消费者
public class ProducerConsumer {
public static final int N = 100; //define the buffer
static Producer producer = new Producer(); //init a new producer thread
static Consumer consumer = new Consumer(); //init a new consumer thread
static Monitor monitor = new Monitor(); //init a new monitor
public static void main(String args[]) {
producer.start();
consumer.start();
}
@sanpingz
sanpingz / Chain.java
Created June 17, 2012 04:02
责任链模式
import java.util.*;
import static com.mceiba.util.Print.*;
class Enums{
public static <T extends Enum<T>> T random(Class<T> type){
Random rand = new Random();
return type.getEnumConstants()[rand.nextInt(type.getEnumConstants().length)];
}
}
class Mail {
// The NO's lower the probability of random selection:
@sanpingz
sanpingz / Commands.java
Created June 17, 2012 02:42
命令模式
import static com.mceiba.util.Print.*;
import java.util.*;
interface Command { void action(); }
enum Signal { RED, YELLOW, GREEN }
public class Commands{
public static void main(String[] args){
EnumMap<Signal, Command> em = new EnumMap<Signal, Command>(Signal.class);
em.put(Signal.RED, new Command(){
public void action() { println("stop"); }
import static com.mceiba.util.Print.*;
import java.lang.reflect.*;
import java.lang.reflect.Proxy;
interface Something{
void doSomething();
void somethingElse(String arg);
}
class RealObjects implements Something{
public void doSomething() { println("doSomething"); }
@sanpingz
sanpingz / Proxy.java
Created May 31, 2012 11:21
代理模式
import static com.mceiba.util.Print.*;
interface Interface{
void doSomething();
void somethingElse(String arg);
}
class RealObject implements Interface{
public void doSomething() { println("doSomething"); }
public void somethingElse(String arg) { println("somethingElse "+arg); }
}