Applies PEP-8 and pyflakes style to tests and zipline.

Mostly whitespace, line width and other spacing changes.
Also, removes use of deprecated has_key in favor of `in`

Going forward new patches should pass running `flake8` before
submission.
This commit is contained in:
Eddie Hebert
2012-10-05 12:14:09 -04:00
parent 0cd8931a5b
commit 77af1ca632
36 changed files with 1461 additions and 868 deletions
+21 -18
View File
@@ -1,6 +1,5 @@
"""Tests for the zipline.finance package"""
from unittest2 import TestCase, skip
from nose.tools import timed
from collections import defaultdict
import numpy as np
@@ -9,6 +8,7 @@ from zipline.optimize.factory import create_predictable_zipline
from zipline.utils.test_utils import setup_logger, teardown_logger
class TestUpDown(TestCase):
"""This unittest verifies that the BuySellAlgorithm in
combination with the UpDownSource are suitable for usage in an
@@ -19,14 +19,13 @@ class TestUpDown(TestCase):
def setUp(self):
self.zipline_test_config = {
'sid' : [0],
'trade_count' : 5,
'amplitude' : 30,
'base_price' : 50
'sid': [0],
'trade_count': 5,
'amplitude': 30,
'base_price': 50
}
setup_logger(self, '/var/log/qexec/qexec.log')
def tearDown(self):
teardown_logger(self)
@@ -49,18 +48,18 @@ class TestUpDown(TestCase):
amplitude = self.zipline_test_config['amplitude']
prices = config['trade_source'][0].values
max_price_idx = np.where(prices==prices.max())[0]
min_price_idx = np.where(prices==prices.min())[0]
max_price_idx = np.where(prices == prices.max())[0]
min_price_idx = np.where(prices == prices.min())[0]
self.assertTrue(np.all(max_price_idx % 2 == 1),
"Maximum prices are not periodic."
)
self.assertTrue(np.all(min_price_idx % 2 == 0),
"Minimum prices are not periodic."
)
self.assertEqual(prices.max(), base_price+amplitude/2.,
self.assertEqual(prices.max(), base_price + amplitude / 2.,
"Maximum price does not equal expected maximum price."
)
self.assertEqual(prices.min(), base_price-amplitude/2.,
self.assertEqual(prices.min(), base_price - amplitude / 2.,
"Minimum price does not equal expected maximum price."
)
@@ -69,8 +68,8 @@ class TestUpDown(TestCase):
self.assertTrue(len(stats) != 0)
orders = np.asarray(algo.orders)
max_order_idx = np.where(orders==orders.max())[0]
min_order_idx = np.where(orders==orders.min())[0]
max_order_idx = np.where(orders == orders.max())[0]
min_order_idx = np.where(orders == orders.min())[0]
self.assertTrue(np.all(max_order_idx % 2 == 1),
"Maximum orders are not periodic."
@@ -111,20 +110,23 @@ class TestUpDown(TestCase):
compound_returns[i] = results.returns.sum()
self.assertTrue(np.all(compound_returns[supposed_max] > compound_returns[np.logical_not(supposed_max)]),
self.assertTrue(np.all(
compound_returns[supposed_max] >
compound_returns[np.logical_not(supposed_max)]),
"Maximum compound returns are not where they are supposed to be."
)
# test for concavity
max_idx = np.where(supposed_max)[0][0]
idx = np.array([max_idx, max_idx])
for i in range((len(test_offsets)-1)/2):
for i in range((len(test_offsets) - 1) / 2):
# going outwards, returns must decrease
self.assertTrue(compound_returns[idx[0]-1] < compound_returns[idx[0]],
self.assertTrue(compound_returns[idx[0] - 1] <
compound_returns[idx[0]],
"Compound returns are not convex."
)
self.assertTrue(compound_returns[idx[1]+1] < compound_returns[idx[1]],
self.assertTrue(compound_returns[idx[1] + 1] <
compound_returns[idx[1]],
"Compound returns are not convex."
)
idx[0] -= 1
@@ -142,7 +144,8 @@ class TestUpDown(TestCase):
self.zipline_test_config,
offset=offset,
)
#function is getting minimized, so have to return negative cum returns.
# function is getting minimized,
# so have to return negative cum returns.
return -zipline.get_cumulative_performance()['returns']
from scipy import optimize