Skip to content

Instantly share code, notes, and snippets.

View siteshen's full-sized avatar

siteshen siteshen

View GitHub Profile
body {
margin: 0 auto;
padding: 4em 1em;
max-width: 50em;
color: #566b78;
font-family: "Helvetica", "Arial", sans-serif;
line-height: 1.5;
}
@siteshen
siteshen / redis_client.py
Created March 3, 2016 12:16
A simple reads-write splitting redis client based on https://github.com/andymccurdy/redis-py.
import random
from redis import StrictRedis
from redis.connection import Connection, ConnectionPool
class RWSplitConnectionPool(object):
READ_COMMANDS = [
'info', 'smembers', 'hlen', 'hmget', 'srandmember', 'hvals', 'randomkey', 'strlen',
@siteshen
siteshen / utils.py
Created August 24, 2015 06:23
Some python util functions && classes
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import datetime
import functools
import time
# logging
# =======
@siteshen
siteshen / bearychat.py
Created August 24, 2015 06:13
BearyChat python client
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import json
import urllib
import urllib2
def bearychat(url, text, channel=None, markdown=None, attachments=None):
payload = {
'text': text,
@siteshen
siteshen / calling_code_en.txt
Created August 19, 2015 13:16
List of country calling codes
1 | Canada
1 | United States
20 | Egypt
211 | South Sudan
212 | Morocco
213 | Algeria
216 | Tunisia
218 | Libya
220 | Gambia
221 | Senegal
@siteshen
siteshen / update_column.sh
Created May 22, 2015 04:18
Example of update value of column with millions of rows.
#!/bin/bash
x=0
while [ $x -lt 5432 ]; do
prev=$(expr $x \* 1000000)
next=$(expr $(expr $x + 1) \* 1000000)
echo $(date +'%Y-%m-%d %H:%M:%S'), $prev, $next
x=$(expr $x + 1)
psql -c "UPDATE followships SET is_deleted = false WHERE is_deleted IS NULL and id >= $prev and id <= $next;"
done
@siteshen
siteshen / pili_client.go
Created April 20, 2015 06:24
Pili client example
package main
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"io"
"log"
"net/http"
@siteshen
siteshen / add_column.sql
Created January 23, 2015 06:07
Example of adding default value to column having millions of rows.
-- ALTER TABLE users ADD COLUMN deleted boolean NOT NULL DEFAULT false;
ALTER TABLE users ADD COLUMN deleted boolean;
ALTER TABLE users ALTER COLUMN deleted SET DEFAULT false;
UPDATE users SET deleted = false WHERE deleted IS NULL;
ALTER TABLE users ALTER COLUMN deleted SET NOT NULL;
@siteshen
siteshen / slack_client.sh
Created January 17, 2015 04:54
Slack client in bash
#!/bin/bash
# https://your-team.slack.com/services/new/incoming-webhook
WEBHOOK_URL=''
CHANNEL='#dev'
USERNAME='sentry'
iCON_URL='https://slack.global.ssl.fastly.net/17635/img/services/sentry_128.png'
function post_message() {
payload="{'username': '$USERNAME', 'text': '$AT_USER $1', 'channel': '$CHANNEL', 'icon_url': '$ICON_URL'}"
@siteshen
siteshen / search.go
Created May 24, 2014 01:31
Binary search algorithm with some recommended testcase in Go.
package search
// binary search 1: low ≤ mid < high
func BinarySearch1(list []int, v int) int {
low, high := 0, len(list)
for low < high {
mid := (high-low)/2 + low
// low ≤ mid < high
if list[mid] < v {
low = mid + 1