Skip to content

Instantly share code, notes, and snippets.

@jaredjburgess
jaredjburgess / progress_bar.py
Last active March 17, 2016 18:25
Template for interactive progress bar.
import progressbar
pbar = progressbar.ProgressBar(maxval=len(object))
pbar.start()
for i in range(len(object)):
pbar.update(i)
pbar.finish()
@jaredjburgess
jaredjburgess / pretty_print.py
Last active March 26, 2016 17:58
Pretty print in python
from pprint
import pprintpprint(example_json)
@jaredjburgess
jaredjburgess / pg_create_fkey.sql
Created February 21, 2016 14:35
PostgreSQL syntax to create a foreign key on an existing table
alter table table_name add constraint table_name_column_name_fkey foreign key (column_name) references other_table_name (other_column_name) on delete cascade;
@jaredjburgess
jaredjburgess / pg_dump_table.sql
Created February 21, 2016 14:36
PostgreSQL pg_dump syntax for a single table
pg_dump -Fc -v -U user_name -d db_name -t table_name > /path/to/file.bak
@jaredjburgess
jaredjburgess / pg_array_overlap.sql
Created February 21, 2016 14:37
PostgreSQL syntax to condition on the overlap of an array column with a given array
where array_column && array['string1', 'string2']::text[]
@jaredjburgess
jaredjburgess / pg_drop_col.sql
Created February 21, 2016 14:57
PostgreSQL syntax to drop a column from a table
alter table table_name drop column_name;
@jaredjburgess
jaredjburgess / write_json.py
Created February 21, 2016 14:57
Write to a JSON file in Python
open(path_to_file, 'w').write(json.dumps(data))
@jaredjburgess
jaredjburgess / read_json.py
Created February 21, 2016 14:58
Read a JSON file in Python
data = json.loads(open(path_to_file, 'r').read())
@jaredjburgess
jaredjburgess / hist_plot.py
Last active February 23, 2016 19:38
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()
@jaredjburgess
jaredjburgess / tsne_vis.py
Created February 21, 2016 15:04
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