Skip to content

Instantly share code, notes, and snippets.

why-not /
Created Sep 13, 2019
pandas data reader for stock data.
# command to install this library.
# conda install pandas-datareader
# example 1
import pandas_datareader as pdr
tsla = pdr.DataReader('tsla', data_source='google', start='2017-1-1')
# example 2
# calculates the trailing stop order price, can be used for weight loss like time series as well.
why-not /
Last active Aug 6, 2018
When you have a Sublime Text SFTP remote folder mapping to an Amazon AWS machine, everytime you reboot you have to update your Sublime SFTP config file so it knows where the folder/files are now. This ofcourse is massively tragic and should not be permitted in a civilized society. Hence this script. It even prints a connection string, so you can…
import json
import subprocess
FNAME = "/PATH/To/SUBLIME/SFTP/FILE/sftp-config.json"
def update_json_file(given_ip, filename):
jsonfile = open(filename, "r") # open the json file for reading
data = json.load(jsonfile) # read the json into the buffer
why-not /
Last active Aug 6, 2018
Automatically stopping an AWS machine when no one is logged in via ssh, and the CPU load avg over the past 5 mins is less than THRESHOLD. This will avoid getting billed for the machine when not in use (except ofcourse your storage will still be billed unless you delete everything and decommission those as well, which this script does not do)
#!/usr/bin/env python
# shell commands being automated.
# netstat -tnpa | grep 'ESTABLISHED.*sshd'
# aws ec2 stop-instances --instance-id INSTANCE_ID
add this script to cron like this:
PATH="What ever path you need to make sure the code runs without failing for want of proper execurable or lib paths"
why-not /
Last active May 24, 2021
Automatically Shutting Down and Deallocating an AZURE VM (when idle AND no one is logged in via SSH)
# This is how the cron file should look like.
# PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/sn$
# * * * * * /home/script/ > /tmp/user_error.log
## Watchout, for some reason cron files seem to require an extra new line at the end,
## consult this SO:
## Also take care to setup a PATH variable for the cron explicitly. It doesn't see
## your regular PATH variable.
why-not /
Created Dec 11, 2016 — forked from hellpanderrr/
Python find intersection of two vectors using matplotlib and numpy
from numpy import dot,array,empty_like
from matplotlib.path import Path

def make_path(x1,y1,x2,y2):
    return Path([[x1,y1],[x1,y2],[x2,y2],[x2,y1]])

def perp( a ) :
    b = empty_like(a)
    b[0] = -a[1]

d3.unconf example gist. Fork it here.

why-not / gist:5379296
Last active Dec 16, 2015
Given a folder full of jpegs, this gist prints out if you have a face in it which is frontal (not profile), and its ratio between face size and image size. If it finds more than one face, it treats it as if no faces were found. (Use case: profile pictures)
View gist:5379296
import sys, os
from cv import *
from cv2 import *
import glob
def detect_face(image):
"""Converts an image to grayscale and prints the ratio of face to image if _one_ face is found"""
grayscale = CreateImage((image.width, image.height), 8, 1)
CvtColor(image, grayscale, CV_BGR2GRAY)
why-not / gist:4582705
Last active May 22, 2021
Pandas recipe. I find pandas indexing counter intuitive, perhaps my intuitions were shaped by many years in the imperative world. I am collecting some recipes to do things quickly in pandas & to jog my memory.
View gist:4582705
"""making a dataframe"""
df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
"""quick way to create an interesting data frame to try things out"""
df = pd.DataFrame(np.random.randn(5, 4), columns=['a', 'b', 'c', 'd'])
"""convert a dictionary into a DataFrame"""
"""make the keys into columns"""
df = pd.DataFrame(dic, index=[0])