Skip to content

Instantly share code, notes, and snippets.

@shihongzhi
shihongzhi / boot.html
Last active December 17, 2015 18:49
引用bootstrap的模板
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Template</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="css/bootstrap-responsive.min.css" rel="stylesheet" media="screen"/>
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen"/>
</head>
<body>
<h1>Hello, world!</h1>
@shihongzhi
shihongzhi / clearfix.css
Last active December 17, 2015 12:29
当没有后续节点之后,但是又想使用clear:both,清楚float。 则可以通过这段代码,设置父节点的style
.clearfix:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
.clearfix {
@shihongzhi
shihongzhi / gen_key_factory.py
Created October 21, 2012 05:16
洪教授python在web 2.0 网站中的应用演讲中 gen_key_factory函数的自己实现版本
import doctest
import inspect
def gen_key_factory(key_pattern, arg_names, defaults):
"""
产生gen_key 函数的工厂
洪教授python在web 2.0 网站中的应用演讲中 gen_key_factory函数的自己实现版本
"""
def gen_key(*a, **kw):
values = list(a)
def damerau_levenshtein_distance(source, target):
if not source and not target:
return 0
elif not source:
return len(target)
elif not target:
return len(source)
##Part 1
#iteration
class countdown_iter(object):
def __init__(self, start):
self.count = start
def __iter__(self):
return self
def next(self):
if self.count <= 0:
raise StopIteration
@shihongzhi
shihongzhi / Construction.cpp
Created August 24, 2012 02:11
在构造函数中调用虚函数
#include "stdafx.h"
using namespace std;
class C180
{
public:
C180() {
foo();
this->foo();
@shihongzhi
shihongzhi / gist:3434223
Created August 23, 2012 08:19
retry decorator
import time
import sys
# reload(sys)
# sys.setdefaultencoding('utf8')
def retry(ExceptionToCheck, tries=4, delay=3, backoff=2, logger=None):
def deco_retry(f):
def f_retry(*args, **kwargs):
mtries, mdelay = tries, delay
@shihongzhi
shihongzhi / tree.md
Created June 18, 2012 05:19 — forked from hrldcpr/tree.md
one-line tree in python

One-line Tree in Python

Using Python's built-in defaultdict we can easily define a tree data structure:

def tree(): return defaultdict(tree)

That's it!

@shihongzhi
shihongzhi / antispam.py
Created May 26, 2012 03:00
一道面试题,具体见我的博客http://shihongzhi.github.com/。这里使用bjoern作为WSGI server,从reqeust的‘QUERY_STRING’中得到json数据。这里的stategys和actions变量保存了需要处理的策略和行为。limit_time为限制次数。 本程序有个问题,当数据特别大的时候,datas这个list会特别大,导致性能下降,可以考虑用队列替换list,定时删除那些过时的json数据
# --- coding:utf-8 ---
import strategy
import bjoern
from urlparse import parse_qsl
strategys = [strategy.strategy_highfreq, strategy.strategy_repetition]
actions = ['answer', 'comment', 'question']
limit_time = 10
datas = []
@shihongzhi
shihongzhi / sendemail.py
Created May 24, 2012 07:22
使用smtplib发送邮件,支持html文件和发送附件。使用命令行操作:python sendemail.py -h 查看帮助
import os
import smtplib
import argparse
import re
import mimetypes
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import formatdate, COMMASPACE
from email import Encoders