Skip to content

Instantly share code, notes, and snippets.

View nomeyer's full-sized avatar

Nick Omeyer nomeyer

View GitHub Profile
@nomeyer
nomeyer / append_file.js
Created February 10, 2016 16:36
Append to a file in node.js
var fs = require('fs');
fs.appendFile(filePath, text, function(err) {
console.log('Error writing to file');
});
@nomeyer
nomeyer / flask_catchall.py
Created February 12, 2016 15:06
Flask catchall endpoint for optional path parameter
@app.route('/', methods=['GET'], defaults={'var': 'default'})
@app.route('/<string:var>', methods=['GET'])
def page(var):
# do stuff with the given var or default
return render_template('page.html', var=var)
@nomeyer
nomeyer / user_permissions.sql
Created February 12, 2016 15:23
PostgresSQL query to show tables on which a given user has permissions of a certain type
select schemaname||'.'||tablename
from pg_tables
where has_table_privilege (
'~~user_name~~',
schemaname||'.'||tablename,
'~~permission_type~~'
)
and schemaname not in (
'pg_catalog',
'information_schema'
@nomeyer
nomeyer / pg_db_size.sql
Created February 12, 2016 15:26
Query to get the size of a PostgreSQL database (public schema)
select
schemaname,
tablename,
pg_size_pretty(size) as size_pretty,
pg_size_pretty(total_size) as total_size_pretty
from (
select
*,
pg_relation_size(schemaname ||'.'|| tablename) as size,
@nomeyer
nomeyer / performance.js
Last active March 24, 2016 19:49
JavaScript to measure the performance of something
var start = performance.now();
var end = performance.now();
console.log('Doing stuff took ' + (end-start) + 'ms');
@nomeyer
nomeyer / branch_old_commit.sh
Last active February 12, 2016 15:43
Git create a new branch from an old commit hash
git fetch origin 8fd8e58c2599a9d31f9ca8cad1fdb7524dc794e5:refs/remotes/origin/~~branch_name~~
@nomeyer
nomeyer / pg_add_day_date.sql
Created February 12, 2016 17:41
PostgreSQL syntax to add the day to a date column
case extract(isodow from ~~date~~)
when 1 then 'Mon ' || cast(~~date~~ as date)
when 2 then 'Tue ' || cast(~~date~~ as date)
when 3 then 'Wed ' || cast(~~date~~ as date)
when 4 then 'Thu ' || cast(~~date~~ as date)
when 5 then 'Fri ' || cast(~~date~~ as date)
when 6 then 'Sat ' || cast(~~date~~ as date)
when 7 then 'Sun ' || cast(~~date~~ as date)
end date
/*dragon*/var b=X=Y=T=L=0;document.addEventListener("click",function(a){a.preventDefault()},!0);document.addEventListener("mousedown",c);document.addEventListener("touchstart",c);function c(a){a.preventDefault();a.target!==document.documentElement&&a.target!==document.body&&(b=Date.now(),a.target.setAttribute("data-drag",b),a.target.style.position="relative",T=a.target.style.top.split("px")[0]||0,L=a.target.style.left.split("px")[0]||0);X=a.clientX||a.touches[0].clientX;Y=a.clientY||a.touches[0].clientY}document.addEventListener("mousemove",d);document.addEventListener("touchmove",d);function d(a){if(""!==b){var e=document.querySelector('[data-drag="'+b+'"]');e.style.top=parseInt(T)+parseInt((a.clientY||a.touches[0].clientY)-Y)+"px";e.style.left=parseInt(L)+parseInt((a.clientX||a.touches[0].clientX)-X)+"px"}}document.addEventListener("mouseup",f);document.addEventListener("touchend",f);function f(){b=""}document.addEventListener("mouseover",g);function g(a){a.target.style.cursor="move";a.target.style.boxShado
@nomeyer
nomeyer / tsne_vis.py
Created February 19, 2016 11:39
Visualise data using the t-SNE algorithm in Python
# Visualise given word embeddings
# words is a list of words
# data is the vector representation of each word
# Train the algorithm
from sklearn.manifold import TSNE
vis_algo = TSNE(random_state=0, verbose=10, init='pca', n_iter=200)
vis = vis_algo.fit_transform(data)
# Plot the resulting visualisation
@nomeyer
nomeyer / front_app_pid.m
Last active February 24, 2016 17:38
Get the PID of the frontmost app on Mac OS X
#import <AppKit/AppKit.h>
NSRunningApplication* frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
pid_t pid = [frontApp processIdentifier];
// fprintf(stdout, "%s\n", [[NSString stringWithFormat:@"%ld", (long)pid] UTF8String]);
// fflush(stdout);