Skip to content

Instantly share code, notes, and snippets.

@robdmc
robdmc / contextify.py
Created September 9, 2014 14:54
A python decorator to use a class as a context manager that yields an instance
def contextify(cls):
@contextmanager
def wrapper(*args, **kwargs):
yield cls(*args, **kwargs)
return wrapper
@robdmc
robdmc / options_plotting.py
Last active January 6, 2016 18:12
options data from yahoo
# content for ipython notebook
%matplotlib inline
execfile('/Users/rob/pandasSetup.py')
import mpld3
mpld3.enable_notebook()
from pandas_datareader.data import Options
options = Options('spy', 'yahoo')
expiry_dates = options.expiry_dates
df = options.get_options_data(expiry=expiry_dates[0]).reset_index()
@robdmc
robdmc / queryset_chunker.py
Last active August 29, 2015 14:17
django queryset chunker
from sys import maxint
def queryset_chunker(queryset, chunksize=10000):
pk = -maxint
last_pk = queryset.order_by('-pk')[0][0]
queryset = queryset.order_by('pk')
while pk < last_pk:
for row in queryset.filter(pk__gt=pk)[:chunksize]:
pk = row[0]
yield row
@robdmc
robdmc / values_list_queryset_chunker.py
Created March 23, 2015 20:16
values_list_queryset_chunker
def values_list_queryset_chunker(queryset, chunk_size=10000):
"""
Yields batched querysets from a values_list queryset.
First element of each record must always be pk
"""
queryset = queryset.order_by('pk')
pk = queryset.order_by('pk')[0][0] - 1
batch = queryset.filter(pk__gt=pk)[:chunk_size]
while batch.count() > 0:
chunk_size = min([chunk_size, batch.count()])
@robdmc
robdmc / values_queryset_chunker.py
Last active August 29, 2015 14:18
Queryset chunker for values queries
def chunk_values_queryset(queryset, chunksize):
if queryset.count() > 0:
ending_pk = queryset.aggregate(Min('id'))['id__min'] - 1
out = ['dummy']
while len(out) > 0:
out = list(queryset.filter(pk__gt=ending_pk).order_by('id')[:chunksize])
if out:
ending_pk = out[-1]['id']
yield out
@robdmc
robdmc / log_normal_abstractions.py
Last active January 8, 2016 18:07
Abstractions for working with lognormal distributions
"""
This collection of functions provides convenience abstractions
for working with lognormal distributions. Sums of lognormals result
in a distribution with no closed form solution. The functions provided
here assume the Fenton-Wilkinson approximation for such sums.
Running this google search should get you a good reference paper:
cobb "Approximating the Distribution of a Sum of Log-normal Random Variables"
Note that this package looks like an interesting add-on to the scipy
@robdmc
robdmc / ssh_key_push.sh
Created May 25, 2015 23:35
ssh key push for remote login
cat ~/.ssh/id_rsa.pub | ssh user@host 'cat - >> ~/.ssh/authorized_keys'
@robdmc
robdmc / conda_commands.sh
Created June 4, 2015 19:19
conda common commands
# create new environment
conda create -n <env-name> [<package_name=version>] [anaconda]
# list current environments
conda info -e
# activate an environment
. activate <env_name>
# deactivate environment
@robdmc
robdmc / docker_cheat_sheet.sh
Last active August 22, 2022 13:45
Docker Commands
# list images
docker images
# list only image ids
docker images -q
# build an image from a dockerfile with a tag name
docker build -t image_tag_name
# list running containers
@robdmc
robdmc / matplotlib_date_format.py
Last active February 1, 2021 02:34
matplotlib date format
# Here is a simple version
def make_plots_simple():
import pandas as pd
import pylab as pl
import matplotlib.dates as mdates
x = pd.date_range('4/1/2020', '4/20/2020', freq='H')
y = [n for n in range(len(x))]
fig, ax = pl.subplots()