Skip to content

Instantly share code, notes, and snippets.

View rileypeterson's full-sized avatar

rileypeterson

View GitHub Profile
@rileypeterson
rileypeterson / slick_code
Created February 21, 2020 07:54
Code snippets I think are cool, but frowned upon or difficult to understand
import numpy as np
1/(0 or np.nan) # Avoid ZeroDivisionError
def get_odds(bet, profit):
_ = lambda x: x + min(-(x % 5), 5 - (x % 5), key=abs)
return round(_(100*(max(profit, bet) / min(bet, -profit, key=abs))))
assert get_odds(1, 1.25) == 125
assert get_odds(1, 1.32) == 130
assert get_odds(1, 0.8) == -125
@rileypeterson
rileypeterson / helpers.sh
Created February 11, 2020 17:40
Useful unix commands
watch -d -n 0.5 free -m
tail -lf /var/log/log.txt
sudo service service@{1..10} restart
@rileypeterson
rileypeterson / socket_blocker.py
Created February 6, 2020 19:56
Socket blocker decorator
# Based on https://github.com/miketheman/pytest-socket/blob/master/pytest_socket.py
import socket
import requests
_true_socket = socket.socket
def _blank(*args, **kwargs):
raise ValueError('socket')
@rileypeterson
rileypeterson / bottom_up_demo.py
Last active January 31, 2020 07:01
Bottom Up Algorithm Demo
import random
import tempfile
import os
import numpy as np
import matplotlib.pyplot as plt
def r_sqr(yi, fi):
ss_res = np.sum((yi - fi) ** 2)
ss_tot = np.sum((yi - np.mean(yi)) ** 2)
if ss_tot == 0:
@rileypeterson
rileypeterson / cool_links.txt
Last active January 30, 2020 03:05
Some cool things
pts = [(-1, 1), (0, 1), (1, 0), (2, 2), (3, 3), (4, 2)]
def fd(*points, values=None):
if values is None:
values = []
if len(points) == 2:
x0, y0 = points[0]
x1, y1 = points[1]
val = (y1 - y0) / (x1 - x0)
values.append(val)
@rileypeterson
rileypeterson / kd_tree.py
Created October 28, 2019 16:30
KD Tree Demo
from scipy.spatial import cKDTree
import time
import numpy as np
np.random.seed(42)
st = time.time()
a = np.random.rand(40000, 25)
b = np.random.rand(40000, 25)
t0 = cKDTree(a)
# Automatically sets upstream tracking
git config --global push.default current
# Autocomplete
brew install git bash-completion
# Create local branch which tracks existing remote branch
# https://stackoverflow.com/a/9537923/8927098
git checkout --track origin/<branch_name>
@rileypeterson
rileypeterson / pyspark_setup.sh
Last active December 10, 2019 21:58
Ubuntu/vagrant pyspark download
wget http://ftp.wayne.edu/apache/spark/spark-2.4.4/spark-2.4.4-bin-hadoop2.7.tgz
# or another mirror from https://www.apache.org/dyn/closer.lua/spark/spark-2.4.4/spark-2.4.4-bin-hadoop2.7.tgz
tar -xzf spark-2.4.4-bin-hadoop2.7.tgz
export SPARK_HOME=/vagrant/spark-2.4.4-bin-hadoop2.7
export PATH=$SPARK_HOME/bin:$PATH
@rileypeterson
rileypeterson / ellipse_plot.py
Last active May 10, 2019 22:04
Plot rotated ellipse
""" From: https://math.stackexchange.com/a/2647450/635480 """
import numpy as np
import matplotlib.pyplot as plt
def plot_rotated_ellipse(cx=1, cy=2, rx=3, ry=0.5, theta=np.pi/4):
alpha = np.linspace(0, 2*np.pi, 100)
x = rx*np.cos(alpha)*np.cos(theta) - ry*np.sin(alpha)*np.sin(theta) + cx
y = rx*np.cos(alpha)*np.sin(theta) + ry*np.sin(alpha)*np.cos(theta) + cy
plt.plot(x, y)