Skip to content

Instantly share code, notes, and snippets.

View jasonlvhit's full-sized avatar
💭
I may be slow to respond.

辣椒面 jasonlvhit

💭
I may be slow to respond.
View GitHub Profile
#Python 装饰器
#http://programmingbits.pythonblogs.com/27_programmingbits/archive/50_function_decorators.html
def memoize(f):
cache = {}
def helper(x):
if x not in cache:
cache[x] = f(x)
return cache[x]
return helper
//JavaScript : The Good Parts
//闭包,存储数据
var memoizer = function (memo, formula) {
var recur = function(n){
var result = memo[n];
if (typeof result !== 'number') {
result = formula(recur, n);
memo[n] = result;
};
return result;
#代码来自Python 2.7 Lib/wsgiref/utils.py
#__contains__判断一个字典中是否有key, 等同于has_key
_hoppish = {
'connection':1, 'keep-alive':1, 'proxy-authenticate':1,
'proxy-authorization':1, 'te':1, 'trailers':1, 'transfer-encoding':1,
'upgrade':1
}.__contains__
def is_hop_by_hop(header_name):
#C数组下标的实现
"""
其实一个例子就能说明,C中数组下标的实现方法。
................
int array[4];
array[2] 和 2[array]是等价的。
................
2[array],这是个什么玩意儿。
2[array]等价于:
@jasonlvhit
jasonlvhit / KMP.cpp
Last active August 29, 2015 13:56
很久之前的KMP
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
void match_list(char * pattern, int match[] , size_t len)
{
memset(match, 0 ,sizeof(int)*len);
@jasonlvhit
jasonlvhit / douban_user_login.py
Last active August 29, 2015 13:56
豆瓣OAuth2用户认证登录, Flask应用。
from sqlalchemy.exc import IntegrityError
from flask import g, request, session, redirect, flash
import urllib
import urllib2
from app import models
@app.route('/douban/login')
def get_auth_code():
return redirect("https://www.douban.com/service/auth2/auth?client_id=0fb837361a070e2d2f28a5a24e152a87&redirect_uri=https://douping.sinaapp.com/auth&response_type=code")
/*写多了Java和Python,写C++全是坑。。。。。:| */
#include <cstdio>
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <cstdlib>
#include <cassert>
class Student
@jasonlvhit
jasonlvhit / HeapSort.py
Created March 4, 2014 13:06
堆排序
#heap sort
#intruduction to algorithm
import sys
from math import floor
def Max_Heapify(data,i):
l = 2*i
r = 2*i + 1
if l <= len(data) - 1 and data[l] > data[i]:
largest = l
@jasonlvhit
jasonlvhit / escape.py
Created March 15, 2014 11:58
Python 3.3 Lib/html/__init__.py
_escape_map = {ord('&'): '&amp;', ord('<'): '&lt;', ord('>'): '&gt;'}
_escape_map_full = {ord('&'): '&amp;', ord('<'): '&lt;', ord('>'): '&gt;',
ord('"'): '&quot;', ord('\''): '&#x27;'}
# NB: this is a candidate for a bytes/string polymorphic interface
def escape(s, quote=True):
"""
Replace special characters "&", "<" and ">" to HTML-safe sequences.
If the optional flag quote is true (the default), the quotation mark
@jasonlvhit
jasonlvhit / URL_re.py
Last active August 29, 2015 13:57
URL regex representation
import re
cctlds = ['AC','AD','AE','AF','AG','AI','AL','AM','AN','AO','AQ','AR','AS','AT','AU','AW','AX','AZ','BA','BB','BD',\
'BE','BF','BG','BH','BI','BJ','BL','BM','BN','BO','BQ','BR','BS','BT','BV','BW','BY','BZ','CA','CC','CD',\
'CF','CG','CH','CI','CK','CL','CM','CN','CO','CO','CR','CU','CV','CW','CX','CY','CZ','DE','DJ','DK','DM',\
'DO','DZ','EC','EE','EG','EH','ER','ES','ET','EU','FI','FJ','FK','FM','FO','FR','GA','GB','GD','GE','GF',\
'GG','GH','GI','GL','GM','GN','GP','GQ','GR','GS','GT','GU','GW','GY','HK','HM','HN','HR','HT','HU','ID',\
'IE','IL','IM','IN','IO','IQ','IR','IS','IT','JE','JM','JO','JP','KE','KG','KH','KI','KM','KN','KP','KR',\
'KW','KY','KZ','LA','LB','LC','LI','LK','LR','LS','LT','LU','LV','LY','MA','MC','MD','ME','MF','MG','MH',\
'MK','ML','MM','MN','MO','MP','MQ','MR','MS','MT','MU','MV','MW','MX','MY','MZ','NA','NC','NE','NF','NG',\