Skip to content

Instantly share code, notes, and snippets.

@dongliu
dongliu / .bash_profile
Last active November 2, 2017 16:52 — forked from natelandau/.bash_profile
Mac OSX Bash Profile
# ---------------------------------------------------------------------------
#
# Description: This file holds all my BASH configurations and aliases
#
# Sections:
# 1. Environment Configuration
# 2. Make Terminal Better (remapping defaults and adding functionality)
# 3. File and Folder Management
# 4. Searching
# 5. Process Management
@dongliu
dongliu / exist.js
Created March 23, 2016 18:04
Express middleware to find an item and decide if go next
/**
* A middleware to check if id exists in collection
* app.get('/resources/:id/', exist('id', collection), function(req, res){...}}
* @param {String} id the parameter name of item id in req object
* @param {Model} collection the collection model
* @return {Function} the middleware
*/
function exist(id, collection) {
return function (req, res, next) {
collection.findById(req.params[id]).exec(function (err, item) {
/*!
* Bootstrap v3.3.5 (http://getbootstrap.com)
* Copyright 2011-2015 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
*/
/*! normalize.css v3.0.3 | MIT License | github.com/necolas/normalize.css */
html {
font-family: sans-serif;
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
@dongliu
dongliu / page.js
Last active August 29, 2015 14:20
typeahead example and bootstrap style
travelerGlobal.usernames.initialize();
$('#username').typeahead({
minLength: 1,
highlight: true,
hint: true
}, {
name: 'usernames',
display: 'displayName',
limit: 20,
@dongliu
dongliu / example
Last active August 29, 2015 14:14
bootstrap progress bar with markers and labels
mixin progress(id)
.bar-wrap
.progress.no-extra-space(id=id)
.progress-marker(style='right:25%')
.progress-marker(style='right:50%')
.progress-marker(style='right:75%')
mixin time-axis
.time-axis
.time-spot-end now
.time-spot.text-center 6 hours ago
@dongliu
dongliu / proxy.js
Last active August 29, 2015 14:10
reverse proxy express
/**
* @fileOverview a middleware for express deployment behind a reverse proxy
* @param {string} proxy - the known reverse proxy host
* @param {string} proxied_service - the url assigned by proxy
* @return {function} the middleware
* /
function proxied (proxy, proxied_service) {
return function (req, res, next) {
req.proxied = false;
req.proxied_prefix = '';
@dongliu
dongliu / gist:4350238
Created December 21, 2012 02:12
incremental average
function average(array) {
return array.reduce(function(p, c, i, a) {
return (p * i + c) / (i + 1);
});
}
@dongliu
dongliu / snapshot.js
Created November 16, 2012 21:31
snapshot node
start = Date.now();
size = pv_list.length;
pv_list.forEach(function(pv) {
ca.exec('caget', pv, function(err, result) {
complete = complete + 1;
if (err) {
results[pv] = {
name: pv,
value: 'unavailable'
@dongliu
dongliu / snapshot_ca_threading.py
Created November 16, 2012 21:30
snapshot ca multi-threading
def get(d, pv_name, size, start, pid):
ch = ca.create_channel(pv_name, connect=False, auto_cb=False)
if ca.connect_channel(ch, timeout=1.0):
d[pv_name] = ca.get(ch, wait=True)
else:
d[pv_name] = 'not connected'
if len(d) == size:
print int(round((time.time() - start) * 1000))
os.kill(pid, signal.SIGTERM)
@dongliu
dongliu / pv_threading.py
Created November 16, 2012 21:28
snapshot pv multi-threading
# ...
from multiprocessing import Process, Manager
def get(d, pv_name, size, start, pid):
pv = PV(pv_name)
if pv.wait_for_connection(timeout=1.0):
d[pv_name] = pv.get(use_monitor=False)
else:
d[pv_name] = 'not connected'
if len(d) == size: