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) {
@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:
@dongliu
dongliu / snapshot_ca.py
Created November 16, 2012 17:53
snapshot ca
start = time.time()
for pv_name in pv_list:
ch = ca.create_channel(pv_name, connect=False, auto_cb=False)
result[pv_name] = [ch, None, None]
for pv_name, data in result.items():
result[pv_name][1] = ca.connect_channel(data[0], timeout=1.0)
ca.poll()
for pv_name, data in result.items():
if result[pv_name][1]:
ca.get(data[0], wait=False)
@dongliu
dongliu / snapshot_pv.py
Created November 16, 2012 17:50
snapshot pv
start = time.time()
for pv_name in pv_list:
pv = PV(pv_name)
if pv.wait_for_connection(timeout=1.0):
result[pv_name] = pv.get(use_monitor=False)
else:
result[pv_name] = 'not connected'
duration = time.time() - start
print int(round(duration * 1000))
@dongliu
dongliu / gist:3130674
Created July 17, 2012 17:22
sublime 2 text replace -(dash) in id with _(underscore)
find what: #(\w+_)*(\w+)-
replace with: #$1$2_
@dongliu
dongliu / json2list.js
Created June 18, 2012 22:53
Convert a json object to an html dl list
function Json2List(json) {
var output = '';
for (var k in json) {
if (json.hasOwnProperty(k)) {
if (typeof(json[k]) == 'object') {
output = output + '<dl>' + '<dt>' + k + '</dt>' + '<dd>' + Json2List(json[k]) + '</dd>' + '</dl>';
} else {
output = output + '<b>' + k + '</b>' + ' : ' + json[k] + '<br/>';
}
}