Skip to content

Instantly share code, notes, and snippets.

View nomeyer's full-sized avatar

Nick Omeyer nomeyer

View GitHub Profile
@nomeyer
nomeyer / hist_plot.py
Last active February 12, 2016 13:39
Normalised histogram using matplotlib.pyplot.hist and numpy weights
import numpy as np
import matplotlib.pyplot as plt
counts = [10, 8, 6, 14]
weights = np.ones_like(counts) / float(len(counts))
plt.figure(figsize=(10, 5))
plt.hist(counts, bins=range(1, max(counts)+2), align='left', weights=weights)
plt.xticks(range(1, max(counts)+1))
plt.show()
@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 / 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);
@nomeyer
nomeyer / axerror_print.m
Created February 24, 2016 17:41
If chain to log the outcome (AXError) of an accessibility operation
#include <CoreFoundation/CoreFoundation.h>
AXError rtn = AXUIElementCopyAttributeValue(someApp, someAttribute, (CFTypeRef *)&someRef);
if (rtn == kAXErrorSuccess) {
printf("\nSuccess");
} else if (rtn == kAXErrorInvalidUIElement){
printf("\nInvalid UI Element");
} else if (rtn == kAXErrorIllegalArgument) {
printf("\nIllegal argument");
} else if (rtn == kAXErrorCannotComplete) {
printf("\nCannot complete");