Skip to content

Instantly share code, notes, and snippets.

@shihpeng
shihpeng / gist:115dc911a67963ea3af8
Created September 29, 2014 09:51
Best way to do Python string format
# Python 2.6+
# After Python 2.6, you can choose to use .format() function or the old %.
>>> '{0}, {1}, {2}'.format('a', 'b', 'c')
'a, b, c'
>>> '{2}, {1}, {0}'.format('a', 'b', 'c')
'c, b, a'
>>> '{2}, {1}, {0}'.format(*'abc') # unpacking argument sequence
'c, b, a'
>>> '{0}{1}{0}'.format('abra', 'cad') # arguments' indices can be repeated
'abracadabra'
@shihpeng
shihpeng / Main.java
Last active November 8, 2022 18:23
LogStash-Forwarder (Lumberjack) client Java implementation
import javax.net.ssl.*;
import java.io.*;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.Deflater;
/*
* This is a simple implementation of the Lumberjack client.
@shihpeng
shihpeng / gist:74274172b47649e528fc
Created October 7, 2014 09:38
str() and repr() examples
# flask/config.py
def __repr__(self):
return '<%s %s>' % (self.__class__.__name__, dict.__repr__(self))
@shihpeng
shihpeng / cpu_count.py
Last active August 29, 2015 14:07
Frequently used Python code snippets
import os
import re
import subprocess
def available_cpu_count():
""" Number of available virtual or physical CPUs on this system, i.e.
user/real as output by time(1) when called with an optimally scaling
userspace-only program"""
@shihpeng
shihpeng / gist:5235172196f348cf6ac7
Last active August 29, 2015 14:08
build-nginx-mac-os-x-mavericks.sh
#!/bin/bash
# Build Nginx 1.5.7 on Mac OS X Mavericks (10.9)
# This script was created by Kevin Worthington - http://kevinworthington.com/ - 12 December 2013
# Original article at: http://kevinworthington.com/nginx-for-mac-os-x-mavericks-in-2-minutes/
# This useful script is provided for free, but without warranty. Use at your own risk.
# By downloading this script you agree to the terms above.
# create, then go into the build directory
@shihpeng
shihpeng / elasticsearch-for-write-heavy-cluster.yml
Last active December 1, 2015 06:49
Elasticsearch configs
#
# Cluster and Node
# Note: We should always set the cluster name and nodes' name specifically.
#
cluster.name: elasticsearch_prod
node.name: elasticsearch_001
#node.(attribute): (attibute_value)
#
# Index
import math
import scipy.sparse as sparse
import numpy as np
def plsa(n_dw, n_z=2, iter_num=100):
"""
Fit a PLSA model on given data,
in which n_dw(d, w) is the number of occurrence of word w
in document d, d, n_z is the number of topic to be discovered
:param n_dw:
# In Python 2.6 and earlier, the dict constructor can receive an iterable of key/value pairs:
d = dict((key, value) for (key, value) in iterable)
# From Python 2.7 and 3 onwards, you can just use the dict comprehension syntax directly:
d = {key: value for (key, value) in iterable}
# Of course, you can use the iterable in any way you want (tuples and lists literals, generator comprehensions, list comprehensions, generator functions, functional composition... feel creative) as long as each element is an iterable itself of two elements:
@shihpeng
shihpeng / EchoWhatYouGiveMe.scala
Last active August 29, 2015 14:22
Scala Control Structures
def echoWhatYouGiveMe(x: Any): String = x match {
// constant patterns
case 0 => "zero"
case true => "true"
case "hello" => "you said 'hello'"
case Nil => "an empty List"
// sequence patterns
case List(0, _, _) => "a three-element list with 0 as the first element"
@shihpeng
shihpeng / upsert.sql
Last active October 5, 2018 17:25
Upsert in SQLite
-- Reference: http://stackoverflow.com/questions/418898/sqlite-upsert-not-insert-or-replace
-- Approach #1: Select original values before insertion.
-- demo table 'emplyee'
CREATE TABLE employee (
id INTEGER PRIMARY KEY,
role TEXT,
name TEXT);
-- This will update 2 of the columns. When ID=1 exists, the NAME will be unaffected. When ID=1 does not exist, the name will be default (NULL).