Skip to content

Instantly share code, notes, and snippets.

View rileypeterson's full-sized avatar

rileypeterson

View GitHub Profile
@rileypeterson
rileypeterson / mf_balancer.py
Last active June 18, 2024 22:26
Code to rebalance a portfolio of assets with the limitations of buying a minimum dollar amount and not being able to sell. Not sure if this will always work, but seems to do pretty good for the most part. It was crazy to me I couldn't find something like this online.
import numpy as np
from scipy.optimize import minimize
def no_sell_reallocate(x, t, desired_spend=None, names=None):
"""
Optimally (I hope so) realign the current values of investments x to some
portfolio target allocation t, with the limitation that you cannot reduce the
amount of any current investment (i.e. you can't sell).
@rileypeterson
rileypeterson / pandas defaults
Created June 10, 2023 00:33
pandas defaults
import pandas as pd
pd.set_option("display.max_columns", None)
pd.set_option("display.width", 1000)
@rileypeterson
rileypeterson / django_annotate_date_time_convert_tz.py
Created October 8, 2022 22:40
Convert timezone in django annotate with date and time fields
# Insane Django operation I had to do
# Inspired by: https://stackoverflow.com/a/70552577/8927098
# Need to filter for a date, but source table date is in UTC and it's separated into date and time fields
# Example:
# Date to find (America/New_York): 2005-09-08
# Date in table (UTC): 2005-09-09
# Time in table (UTC): 01:07:00
@rileypeterson
rileypeterson / ssh_utils.md
Created June 29, 2022 03:35 — forked from nitred/ssh_utils.md
SSH Tunneling (port forwarding + reverse port forwarding + SOCK5)

About

SSH tunneling and port forwarding snippets and utils

Table of Contents

  • SOCK5 tunnel everything: Link
  • Local port forwarding: Link
  • Remote port forwarding: Link
@rileypeterson
rileypeterson / redact_pdf.py
Created March 15, 2021 08:00
Redact phrases of text from a PDF using Python and (only) PyPDF2. Would need to modify (trivial) for multiple pages.
import PyPDF2
from PyPDF2 import PdfFileReader, PdfFileWriter
from PyPDF2.generic import StreamObject, DecodedStreamObject
from PyPDF2 import filters
# You change the phrases/text you want redacted and the file paths:
phrases = ["123-45-6789", "YOUR_SECRET_PASSWORD", "YOUR_SECRET_ID"]
read_path = "/path/to/yourpdf.pdf"
write_path = "/path/to/yourpdf-redacted.pdf"
@rileypeterson
rileypeterson / keras_sequence.py
Last active February 23, 2021 08:49
Work in progress
# Keras Sequence
import os
import tempfile
from itertools import product
import numpy as np
from random import shuffle
import time
import sys
f_rows = 30000
@rileypeterson
rileypeterson / tmux_remote_execution.sh
Created February 16, 2021 08:37
Execute remote scripts via ssh and tmux (detached). This is much more convenient than nohup, etc. and it lets you actually re-attach to session if you want to ssh in and check on it :)
ssh username@your.machine.address -t 'tmux new-session -d "sleep 20"'
# or change "sleep 20" --> "python my_time_consuming_python_script.py"
@rileypeterson
rileypeterson / recurse.py
Created August 21, 2020 03:49
Left truncatable primes
import math
def pt(x):
x = int(x)
for i in range(2, math.floor(math.sqrt(x))):
if x % i == 0:
return False
return True
def checker(left=None, res=None):
@rileypeterson
rileypeterson / setup.md
Created June 30, 2020 20:16
Backtrader setup

From terminal run jupyter qtconsole & # Start ipython terminal

In python: %matplotlib qt5

Then when using cerebro.plot make sure iplot kwarg is False (i.e. cerebro.plot(iplot=False)).

@rileypeterson
rileypeterson / Image to b64 string
Created June 19, 2020 21:06
Insert an image into html img tag
from io import BytesIO
import matplotlib.pyplot as plt
import base64
b = BytesIO()
plt.plot([1,2,3])
plt.savefig(b)
b64_img_str = base64.encodebytes(b.getvalue()).decode()
# In js
# $("#graph1").attr("src", "data:image/png;base64, ".concat(b64_img_str));