Skip to content

Instantly share code, notes, and snippets.

View xiaocong's full-sized avatar
🐶
I may be slow to respond.

xiaocong xiaocong

🐶
I may be slow to respond.
View GitHub Profile
def changed(l):
l.append("a") # change the original object
def unchanged(l):
l = l + ["a"] # l + ["a"] will create a new list, instead of changing the original object
l1 = []
changed(l1)
# l1 == ["a"]
# simple iteration
a = []
for x in range(10):
a.append(x*2)
# a == [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
# list comprehension
a = [x*2 for x in range(10)]
# dict comprehension
# create virtual env
virtualenv --no-site-packages --python=python3.2 ./venv
# activate virtual env
source ./venv/bin/activate
# install requirements
pip install -r requirements.txt
#.....
# update requirements
# 计算列表内所有元素的和, 包括基本loop方式,sum函数方式,以及使用reduce函数
# the basic way
s = 0
for x in range(10):
s += x
# the right way
s = sum(range(10))
def memoize(f):
memo = {} # 将结果缓存在memo字典对象中,key是参数,value是结果。
def helper(x):
if x not in memo:
memo[x] = f(x)
return memo[x]
return helper
# 修饰fib函数,任何fib函数的调用,将首先察看是否已经有缓存的结果
@memoize
def argument_test_natural_number(f):
def helper(x):
if type(x) == int and x > 0:
return f(x)
else:
raise Exception("Argument is not an integer")
return helper
@argument_test_natural_number
def faculty(n):
@xiaocong
xiaocong / cros-domain-proxy.js
Created July 3, 2013 08:15
proxy server example for adding CORS headers to any existing http services.
// node.js proxy server example for adding CORS headers to any existing http services.
// yes, i know this is super basic, that's why it's here. use this to help understand how http-proxy works with express if you need future routing capabilities
var httpProxy = require('http-proxy'),
express = require('express');
var proxy = new httpProxy.RoutingProxy();
var proxyOptions = {
host: 'ats.borqs.com',
@xiaocong
xiaocong / find_executable.py
Created August 14, 2013 01:31
python: find executable path
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
"""
import jsonrpclib
import os
import urllib2
import subprocess
@xiaocong
xiaocong / gist:6988640
Created October 15, 2013 08:50 — forked from anjackson/gist:2888380
python proxy
import bottle
from wsgiproxy.app import WSGIProxyApp
# Remove "hop-by-hop" headers (as defined by RFC2613, Section 13)
# since they are not allowed by the WSGI standard.
FILTER_HEADERS = [
'Connection',
'Keep-Alive',
'Proxy-Authenticate',
'Proxy-Authorization',
@xiaocong
xiaocong / gist:7767195
Created December 3, 2013 10:36 — forked from Mithrandir0x/gist:3639232
angular provider/service/factory
// Source: https://groups.google.com/forum/#!topic/angular/hVrkvaHGOfc
// jsFiddle: http://jsfiddle.net/pkozlowski_opensource/PxdSP/14/
// author: Pawel Kozlowski
var myApp = angular.module('myApp', []);
//service style, probably the simplest one
myApp.service('helloWorldFromService', function() {
this.sayHello = function() {
return "Hello, World!"