This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def contextify(cls): | |
| @contextmanager | |
| def wrapper(*args, **kwargs): | |
| yield cls(*args, **kwargs) | |
| return wrapper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| cat ~/.ssh/id_rsa.pub | ssh user@host 'cat - >> ~/.ssh/authorized_keys' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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() |
OlderNewer