Skip to content

Instantly share code, notes, and snippets.

@ssanderson
ssanderson / tsc_gen.py
Created August 1, 2012 01:36
top-level draft of trade_simulation_client generator
from zipline.gens import stateful_transform
from zipline.finance.trading import TransactionSimulator
from zipline.finance.performance import PerformanceTracker
def trade_simulation_client(stream_in, algo, environment, sim_style):
"""
Generator that takes the expected output of a merge, a user
algorithm, a trading environment, and a simulator style as
arguments. Pipes the merge stream through a TransactionSimulator
and a PerformanceTracker, which keep track of the current state of
@ssanderson
ssanderson / make_function_global.py
Created October 16, 2015 22:11
STORE_GLOBAL from MAKE_FUNCTION
In [2]: x = 3
In [3]: def foo():
...: global x
...: def x():
...: pass
...:
In [4]: x
Out[4]: 3
@ssanderson
ssanderson / assert.py
Created October 25, 2015 04:50
assert statements dynamically look up the value of AssertionError in the current context
>>> AssertionError = ValueError
>>> assert False, "Assert Failed!"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Assert Failed!
>>> AssertionError = None
>>> assert False, "Assert Failed!"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
@ssanderson
ssanderson / Exception Name Clearing
Created December 9, 2015 01:15
except-blocks unconditionally delete the bound name in Python 3
In [2]: e = 3
In [3]: x = {}
In [4]: try:
...: x['a']
...: except Exception as e:
...: pass
...:
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# td is a TemporaryDirectory that's being deleted earlier than
# expected somewhere in my test suite.
import shutil
old_rmtree = shutil.rmtree
def rmtree(path, *args, **kwargs):
if path == td.name:
import nose.tools; nose.tools.set_trace()
return old_rmtree(path, *args, **kwargs)
@ssanderson
ssanderson / machete_mode_redux.py
Last active September 21, 2016 04:01
More Machete Mode Debugging
import os
from unittest import TestCase
import psutil
import humanize
pid = os.getpid()
proc = psutil.Process(pid)
real_doCleanups = TestCase.doCleanups
OUTPUT_FILE = open('/home/ssanderson/pandas16_memory_usage.txt', 'w')
@ssanderson
ssanderson / avocadownload.py
Last active May 16, 2017 21:51
Avocado Scraper
from __future__ import print_function
import requests
import pandas as pd
from six.moves.urllib_parse import urlencode
def download_avocado_data(dest, start_date, end_date):
base_url = 'https://www.marketnews.usda.gov/mnp/fv-report-retail'
@ssanderson
ssanderson / switch_demo.py
Created September 14, 2017 13:25
"Functional" Switchcase Example
from codetransformer.transformers.switchcase import switch
def foo(x):
print("Start: x={}".format(x))
with switch(x) as case:
@case(1)
def _1():
# Read and write x.
@ssanderson
ssanderson / wrapping_context.py
Last active October 12, 2017 19:36
Context manager that wraps another context manager
from __future__ import print_function
from contextlib import contextmanager
@contextmanager
def some_context_manager(x):
print("Entering", x)
yield
print("Exiting", x)