Skip to content

Instantly share code, notes, and snippets.

@olooney
olooney / update_known_hosts.bash
Created Mar 23, 2020
Shell script to update the .ssh/known_hosts file for all users on a given machine when the host key of a target machine has changed
View update_known_hosts.bash
TARGET_HOST=prometheus
# for each user with a home directory
for USER in `ls /home`; do
# if the user has a known_hosts files
echo "checking /home/$USER/.ssh/known_hosts..."
if [ -f /home/$USER/.ssh/known_hosts ]; then
# remove the outdated key
@olooney
olooney / api.oranlooney.com
Last active Nov 20, 2019
Nginx site configuration for a secure REST API reverse proxy served on HTTPS
View api.oranlooney.com
server {
server_name api.oranlooney.com;
listen 80;
# additional security
server_tokens off;
add_header X-Frame-Options "DENY";
add_header X-Content-Type-Options nosniff;
# performance
View maximize_trace.py
from scipy.optimize import linear_sum_assignment
import numpy as np
def maximize_trace(X):
"""
Maximize the trace of a square matrix using only row and
column permutations. Furthermore, sort the trace
in desending order so that largest value ends
up the upper left and the smallest in the lower right.
@olooney
olooney / parallel_steps.py
Created Oct 25, 2019
Python parallel worker threads with back-pressure
View parallel_steps.py
import os
import sys
from time import sleep
from threading import Thread
from queue import Queue
import codecs
def hard_work(x):
for n in range(100001):
x = codecs.encode(x, 'rot_13')
View skew_heap.py
from typing import NamedTuple, Any, Optional
class Node(NamedTuple):
"""A single Node in a binary tree."""
value: Any
left: Node
right: Node
def count(node: Optional[Node]) -> int:
"""Count the total number of nodes in a tree rooted at this node."""
View normal_equations.py
X = np.hstack([np.ones(shape=(23, 1)), np.random.normal(size=(23, 2))])
Theta = np.array([0.5, +0.1, -0.2]).reshape( (3, 1) )
Y = X @ Theta + np.random.normal(0, 0.1, size=(23, 1))
Theta_hat = np.linalg.inv(X.T @ X) @ X.T @ Y
Theta_hat
View harmonic.py
# https://www.youtube.com/watch?v=d3mHfqd0VZY
def harmonic(x, y, n=6, epsilon=1e-3):
r = sqrt(x/y)
for a in range(1, n):
for b in range(1, n):
if abs(r - a/b) < epsilon:
return True
return False
for x in np.arange(1.6, 6.4, 0.2):
View README.md
@olooney
olooney / algorithm_u.py
Created Jul 11, 2018
Python 3 conversion of a Python 2 implementation of Knuth's "algorithm u" I found online
View algorithm_u.py
# Knuth's algorithm to partition ns into m sets.
# https://codereview.stackexchange.com/questions/1526/finding-all-k-subset-partitions
# http://cs.utsa.edu/~wagner/knuth/
# http://cs.utsa.edu/~wagner/knuth/fasc3b.pdf
# ns must be a list of integers
# m must be an integer less than len(ns)
# this is a generator expression
def algorithm_u(ns, m):
def visit(n, a):
@olooney
olooney / sim.R
Created Jul 9, 2018 — forked from mikeguggis/sim.R
Stack Exchange
View sim.R
library(MASS)
library(ggplot2)
results <- NULL
# Generate simulated data
for ( seed in 1:30 ) {
set.seed(seed+42)
mu = c(0,-1+seed/10)
Sigma = matrix(c(1.5,-.5,-.5,.7),2,2)
You can’t perform that action at this time.