Skip to content

Instantly share code, notes, and snippets.

View tianweidut's full-sized avatar
🎯
Focusing

tianwei tianweidut

🎯
Focusing
View GitHub Profile
@tianweidut
tianweidut / code_contest_template.cpp
Created October 9, 2012 07:40 — forked from wweic/code_contest_template.cpp
Code template for programming contest
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
import os, sys
class Winpdb(object):
'''Embed a Winpdb server.
If you have to tunnel to connect remotely from the Winpdb client, run:
ssh -C -N -f -L 51000:localhost:51000 $SERVER_HOST
@tianweidut
tianweidut / gist:4722360
Created February 6, 2013 12:56
CNKI 专利索引分析
# -*- coding:utf-8 -*-
'''
Created on 2011-12-17
@author: tianwei
'''
import os,sys
@tianweidut
tianweidut / custom_middlewares.py
Created April 18, 2013 07:58
How to jump last visited url in Django? (Django cannot provide build-in method and the request.META.REFERER can't work in all scenarios), so write a custom middleware is necessary.
#it is in the backend package
class PreviousURLMiddleware(object):
def process_response(self, request, response):
if response.status_code == 200:
try:
request.session["previous_url"] = request.session["current_visiting"]
except:
pass
request.session["current_visiting"] = request.get_full_path()
return response
@tianweidut
tianweidut / gist:5802482
Created June 18, 2013 03:26
Python Http Basic Authorization
import urllib2,base64
url = "http://192.168.2.90:7878/api/users/?format=json"
request = urllib2.Request(url)
username = "root"
password = "root"
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
@tianweidut
tianweidut / file.py
Created August 21, 2013 12:55
love xinxin
from django.core.files import File
def importFiles(record, preset):
ret = {'xml': record[XML], 'pic': record[PIC], 'js': record[JS]}
fs = FileStorage()
for key, value in ret.items():
if os.path.isfile(value) is True:
with File(open(value, 'r')) as f:
fs.file_name = os.path.basename(value).split('.')[0]
fs.mapping_preset_id = preset
@tianweidut
tianweidut / findPath.java
Created September 8, 2013 13:16
Find path in binary tree.
//要找到某条路径,就需要进行二叉树的遍历,在此使用前序遍历: 父节点-> left ->right
//Tips: 1. 虽然使用了递归,但是每个结点维护自己的buffer,比较耗空间
void findSum(TreeNode head, int sum, ArrayList<Integer> buffer, int level)
//head 是当前树的根节点, sum 是期望的路径和,
//buffer 是数组,可以看成stack, 每个结点对应着自己的buffer,值是从根节点按照前序遍历到自己的路径节点,
//这也是从其他节点到该节点的有效路径,如果对这个数组进行反向遍历,就得到了从某个结点到该节点的路径
//level 只是为了遍历的时候简单,完全可以用ArrayList的长度,可以去掉这个参数
{
if(head == null) return; // 递归判断,终止条件
int tmp = sum;
@tianweidut
tianweidut / visit.py
Last active December 23, 2015 00:09
safe call function in period
#coding: utf-8
import threading
import time
MAX = 8
MIN = 0
TIME_LIMIT = 60
g = 0
@tianweidut
tianweidut / git-rebase
Created April 20, 2014 03:24
git rebase after sync upstream master
$ git rebase upstream/master
First, rewinding head to replay your work on top of it...
Applying: config test for dae
Using index info to reconstruct a base tree...
M apitests/framework.py
M tests/test_commodity/framework.py
<stdin>:48: trailing whitespace.
dae test -p -- ilmen/apitests/ --junitxml=ilmen/reports/apitest_v2/nose.xml --cov=ilmen.view.commodity.api.v2 || RET=$?
warning: 1 line adds whitespace errors.
Falling back to patching base and 3-way merge...
#coding: utf-8
import os
from os.path import join, exists, splitext
import datetime
import uuid
import string
import random
import shutil
TEST_DIR = '/tmp/xinxin/'