Skip to content

Instantly share code, notes, and snippets.

View sunliwen's full-sized avatar

Liwen S sunliwen

View GitHub Profile
@sunliwen
sunliwen / getQueryParam.js
Created September 22, 2013 12:27
Found this useful snippet in http://getbootstrap.com/customize/
function getQueryParam(key) {
key = key.replace(/[*+?^$.\[\]{}()|\\\/]/g, "\\$&"); // escape RegEx meta chars
var match = location.search.match(new RegExp("[?&]"+key+"=([^&]+)(&|$)"));
return match && decodeURIComponent(match[1].replace(/\+/g, " "));
}
// ?id=123
// getQueryParam('id') --> 123
@sunliwen
sunliwen / south_custom_field.py
Last active December 22, 2015 08:49
how to make south aware of custom field
"""
Following error will occur if there's custom field.
```
! Cannot freeze field 'myapp.mymodel.entity_type'
! (this field has class myapp.fields.EntityTypeField)
! South cannot introspect some fields; this is probably because they are custom
! fields. If they worked in 0.6 or below, this is because we have removed the
! models parser (it often broke things).
@sunliwen
sunliwen / shorten_uuid.py
Created September 5, 2013 07:41
uuid and shortuuid in python
# `pip install shortuuid`
import uuid
long = uuid.uuid5(namespace=uuid.NAMESPACE_URL, name="http://example.com")
print long # UUID('8c9ddcb0-8084-5a7f-a988-1095ab18b5df')
import shortuuid
short = shortuuid.uuid(name="http://example.com")
print short # 'c8sh5y9hdSMS6zVnrvf53T'
# Ubuntu upstart file at /etc/init/mongodb.conf
limit nofile 32768 32768
kill timeout 300 # wait 300s between SIGTERM and SIGKILL.
pre-start script
mkdir -p /data/mongodb &> /dev/null
mkdir -p /data/logs/mongo &> /dev/null
chown mongodb:nogroup /data/mongodb &> /dev/null
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0-rc.3/handlebars.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/ember.js/1.0.0-rc.6/ember.min.js"></script>
<link href="http://getbootstrap.com/dist/css/bootstrap.css" rel="stylesheet" type="text/css" />
<script src="http://getbootstrap.com/dist/js/bootstrap.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
@sunliwen
sunliwen / Preferences.sublime-settings
Created May 24, 2013 16:12
My Sublime Text 2 user settings
{
"color_scheme": "Packages/Color Scheme - Default/Solarized (Dark).tmTheme",
"font_size": 15.0,
"draw_white_space": "selection",
"trim_trailing_white_space_on_save": true,
"tab_size": 2,
"translate_tabs_to_spaces": true,
"tab_completion": true,
"save_on_focus_lost": true,
"highlight_line": true
@sunliwen
sunliwen / keys_memcached
Last active December 16, 2015 13:48
redis command "KEYS *" for memcached
# It's not possible to have a "KEYS *" command in memcached directly
# but could achieve by two steps
➜ ~ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
set one 0 60 1
1
STORED
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.mongodb.mongod</string>
<key>ProgramArguments</key>
<array>
<string>/opt/local/bin/mongod</string>
@sunliwen
sunliwen / init_nmp.py
Created September 17, 2011 17:53
Fabric install Nginx+PHP-FPM+MySQL on Ubuntu 10.04 LTS
def init_nmp():
install_nginx()
install_php()
install_phpfpm()
def install_nginx():
sudo("add-apt-repository ppa:nginx/stable")
sudo("apt-get update")
sudo("apt-get -y -q install nginx-full nginx-common")