Skip to content

Instantly share code, notes, and snippets.

@CooperLuan
CooperLuan / REDRedshift_HowWhyWhat.ipynb
Created March 11, 2016 02:28
Slides for AWS Redshift 2016-03-11
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
/*
* 将 Mongo 生成的 ObjectId 转为 datetime/timestamp
* 考虑时区因素
*/
DROP FUNCTION f_oid_2_gen_time(oid varchar);
CREATE FUNCTION f_oid_2_gen_time
(oid varchar(30))
RETURNS timestamp
IMMUTABLE
#!/usr/bin/python
from multiprocessing import Pool
msg_list = [
'add|(1,2,3)',
'sqrt|(3,)',
'sqrt|(5,)',
'add|(4,5,6)',
]
@CooperLuan
CooperLuan / 0_reuse_code.js
Last active August 29, 2015 14:21
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@CooperLuan
CooperLuan / excerpt-x-forwarded-for.md
Created May 9, 2015 08:44
[博客摘抄] HTTP 请求头中的 X-Forwarded-For

HTTP 请求头中的 X-Forwarded-For

X-Forwarded-For 是由 Squid 引入的, 被引入 RFC7239 标准中,X-Forwarded-For 格式

X-Forwarded-For: client, proxy1, proxy2

值代表 离服务端最远的设备 IP + 每一级代理设备的 IP

上面的 XFF 代表访问由 client 发起,经过了 proxy1,proxy2 两台代理,此外还有终端代理 proxy3, 它的 IP 在 Remote Address

@CooperLuan
CooperLuan / paly-pubnub.py
Last active August 29, 2015 14:12
play pubnub
#/usr/bin/python3
"""
pip install git+git://github.com/CooperLuan/pubnub.git
"""
import os
import json
import threading
import inspect
import ctypes
import time
# IllegalCharacterError in pandas.DataFrame.to_excel(encoding='GB18030')
"""
具体的报错信息显示错误是在 openpyxl.cell.cell.py 抛出的
原因是不匹配 re.compile(r'[\000-\010]|[\013-\014]|[\016-\037]')
所以用这个正则处理 pandas 中的字符结果即
"""
df.apply(lambda x: re.sub(rr'[\000-\010]|[\013-\014]|[\016-\037]', r' ', x['field'], re.S))
from collections import defaultdict
# Using default dictionaries to represent simple trees
# Link http://sahandsaba.com/thirty-python-language-features-and-tricks-you-may-not-know.html#using-default-dictionaries-to-represent-simple-trees
tree = lambda :defaultdict(tree)
root = tree()
root['id'] = 'File'
root['value'] = 'File'
root['items'][0] = 'Open'
root['items'][1] = 'Close'
@CooperLuan
CooperLuan / schema_example.py
Last active July 2, 2017 03:56
python schema example
"""
pip install schema
check if people can retire(age 65 for male, 60 for female)
"""
from schema import Schema, And, Use, Optional
schema = Schema([{
'name': And(str, len),
@CooperLuan
CooperLuan / 20140331.py
Last active August 29, 2015 13:57
flask example
# encoding: utf8
import logging
logging.basicConfig(level=logging.INFO)
from flask import make_response, jsonify
from flask import abort, redirect
from flask import Flask, request
app = Flask('info-center')