From 5d8a915d15b7ccebcf01eb6781a15c376e329f77 Mon Sep 17 00:00:00 2001 From: Scott Sanderson Date: Tue, 10 Nov 2015 16:34:23 -0500 Subject: [PATCH] ENH: Add inspect() function to adjusted_array. --- tests/pipeline/test_adjusted_array.py | 27 ++++++++++++++++++++++++ zipline/lib/adjusted_array.pyx | 30 +++++++++++++++++++++------ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/tests/pipeline/test_adjusted_array.py b/tests/pipeline/test_adjusted_array.py index 858db648..a047469c 100644 --- a/tests/pipeline/test_adjusted_array.py +++ b/tests/pipeline/test_adjusted_array.py @@ -1,6 +1,7 @@ """ Tests for chunked adjustments. """ +from textwrap import dedent from unittest import TestCase from nose_parameterized import parameterized @@ -323,3 +324,29 @@ class AdjustedArrayTestCase(TestCase): with self.assertRaisesRegexp(ValueError, msg): adjusted_array(data, bad_mask, {}) + + def test_inspect(self): + data = arange(15, dtype=float).reshape(5, 3) + adj_array = adjusted_array( + data, + NOMASK, + {4: [Float64Multiply(2, 3, 0, 0, 4.0)]}, + ) + + expected = dedent( + """\ + Adjusted Array: + + Data: + array([[ 0., 1., 2.], + [ 3., 4., 5.], + [ 6., 7., 8.], + [ 9., 10., 11.], + [ 12., 13., 14.]]) + + Adjustments: + {4: [Float64Multiply(first_row=2, last_row=3, first_col=0, \ +last_col=0, value=4.000000)]} + """ + ) + self.assertEqual(expected, adj_array.inspect()) diff --git a/zipline/lib/adjusted_array.pyx b/zipline/lib/adjusted_array.pyx index 525af772..64a8a87d 100644 --- a/zipline/lib/adjusted_array.pyx +++ b/zipline/lib/adjusted_array.pyx @@ -5,6 +5,8 @@ from cpython cimport ( Py_EQ, PyObject_RichCompare, ) +from pprint import pformat + from numpy import ( asarray, bool_, @@ -128,6 +130,15 @@ cdef class Float64AdjustedArray(AdjustedArray): self._data = data self.adjustments = adjustments + def inspect(self): + return ( + "Adjusted Array:\n\nData:\n" + "{data}\n\nAdjustments:\n{adjustments}\n".format( + data=repr(asarray(self._data)), + adjustments=pformat(self.adjustments), + ) + ) + property dtype: def __get__(self): return float64 @@ -216,10 +227,17 @@ cdef class _Float64AdjustedArrayWindow: self.anchor += 1 return out - def __repr__(self): - return "%s(window_length=%d, anchor=%d, max_anchor=%d)" % ( - type(self).__name__, - self.window_length, - self.anchor, - self.max_anchor, + def inspect(self): + return ( + "{type_}\n" + "Window Length: {window_length}\n" + "Current Buffer:\n" + "{data}\n" + "Remaining Adjustments:\n" + "{adjustments}\n" + ).format( + type_=type(self).__name__, + window_length=self.window_length, + data=asarray(self.data[self.anchor - self.window_length:self.anchor]), + adjustments=pformat(self.adjustments), )