Conditions | 3 |
Total Lines | 143 |
Lines | 0 |
Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | # |
||
88 | |||
89 | self.asset_info = EQUITY_INFO |
||
90 | self.writer = SyntheticDailyBarWriter( |
||
91 | self.asset_info, |
||
92 | self.trading_days, |
||
93 | ) |
||
94 | |||
95 | self.dir_ = TempDirectory() |
||
96 | self.dir_.create() |
||
97 | self.dest = self.dir_.getpath('daily_equity_pricing.bcolz') |
||
98 | |||
99 | def tearDown(self): |
||
100 | self.dir_.cleanup() |
||
101 | |||
102 | @property |
||
103 | def assets(self): |
||
104 | return self.asset_info.index |
||
105 | |||
106 | def trading_days_between(self, start, end): |
||
107 | return self.trading_days[self.trading_days.slice_indexer(start, end)] |
||
108 | |||
109 | def asset_start(self, asset_id): |
||
110 | return self.writer.asset_start(asset_id) |
||
111 | |||
112 | def asset_end(self, asset_id): |
||
113 | return self.writer.asset_end(asset_id) |
||
114 | |||
115 | def dates_for_asset(self, asset_id): |
||
116 | start, end = self.asset_start(asset_id), self.asset_end(asset_id) |
||
117 | return self.trading_days_between(start, end) |
||
118 | |||
119 | def test_write_ohlcv_content(self): |
||
120 | result = self.writer.write(self.dest, self.trading_days, self.assets) |
||
121 | for column in SyntheticDailyBarWriter.OHLCV: |
||
122 | idx = 0 |
||
123 | data = result[column][:] |
||
124 | multiplier = 1 if column == 'volume' else 1000 |
||
125 | for asset_id in self.assets: |
||
126 | for date in self.dates_for_asset(asset_id): |
||
127 | self.assertEqual( |
||
128 | SyntheticDailyBarWriter.expected_value( |
||
129 | asset_id, |
||
130 | date, |
||
131 | column |
||
132 | ) * multiplier, |
||
133 | data[idx], |
||
134 | ) |
||
135 | idx += 1 |
||
136 | self.assertEqual(idx, len(data)) |
||
137 | |||
138 | def test_write_day_and_id(self): |
||
139 | result = self.writer.write(self.dest, self.trading_days, self.assets) |
||
140 | idx = 0 |
||
141 | ids = result['id'] |
||
142 | days = result['day'] |
||
143 | for asset_id in self.assets: |
||
144 | for date in self.dates_for_asset(asset_id): |
||
145 | self.assertEqual(ids[idx], asset_id) |
||
146 | self.assertEqual(date, seconds_to_timestamp(days[idx])) |
||
147 | idx += 1 |
||
148 | |||
149 | def test_write_attrs(self): |
||
150 | result = self.writer.write(self.dest, self.trading_days, self.assets) |
||
151 | expected_first_row = { |
||
152 | '1': 0, |
||
153 | '2': 5, # Asset 1 has 5 trading days. |
||
154 | '3': 12, # Asset 2 has 7 trading days. |
||
155 | '4': 33, # Asset 3 has 21 trading days. |
||
156 | '5': 44, # Asset 4 has 11 trading days. |
||
157 | '6': 49, # Asset 5 has 5 trading days. |
||
158 | } |
||
159 | expected_last_row = { |
||
160 | '1': 4, |
||
161 | '2': 11, |
||
162 | '3': 32, |
||
163 | '4': 43, |
||
164 | '5': 48, |
||
165 | '6': 57, # Asset 6 has 9 trading days. |
||
166 | } |
||
167 | expected_calendar_offset = { |
||
168 | '1': 0, # Starts on 6-01, 1st trading day of month. |
||
169 | '2': 15, # Starts on 6-22, 16th trading day of month. |
||
170 | '3': 1, # Starts on 6-02, 2nd trading day of month. |
||
171 | '4': 0, # Starts on 6-01, 1st trading day of month. |
||
172 | '5': 9, # Starts on 6-12, 10th trading day of month. |
||
173 | '6': 10, # Starts on 6-15, 11th trading day of month. |
||
174 | } |
||
175 | self.assertEqual(result.attrs['first_row'], expected_first_row) |
||
176 | self.assertEqual(result.attrs['last_row'], expected_last_row) |
||
177 | self.assertEqual( |
||
178 | result.attrs['calendar_offset'], |
||
179 | expected_calendar_offset, |
||
180 | ) |
||
181 | assert_index_equal( |
||
182 | self.trading_days, |
||
183 | DatetimeIndex(result.attrs['calendar'], tz='UTC'), |
||
184 | ) |
||
185 | |||
186 | def _check_read_results(self, columns, assets, start_date, end_date): |
||
187 | table = self.writer.write(self.dest, self.trading_days, self.assets) |
||
188 | reader = BcolzDailyBarReader(table) |
||
189 | results = reader.load_raw_arrays(columns, start_date, end_date, assets) |
||
190 | dates = self.trading_days_between(start_date, end_date) |
||
191 | for column, result in zip(columns, results): |
||
192 | assert_array_equal( |
||
193 | result, |
||
194 | self.writer.expected_values_2d( |
||
195 | dates, |
||
196 | assets, |
||
197 | column.name, |
||
198 | ) |
||
199 | ) |
||
200 | |||
201 | @parameterized.expand([ |
||
202 | ([USEquityPricing.open],), |
||
203 | ([USEquityPricing.close, USEquityPricing.volume],), |
||
204 | ([USEquityPricing.volume, USEquityPricing.high, USEquityPricing.low],), |
||
205 | (USEquityPricing.columns,), |
||
206 | ]) |
||
207 | def test_read(self, columns): |
||
208 | self._check_read_results( |
||
209 | columns, |
||
210 | self.assets, |
||
211 | TEST_QUERY_START, |
||
212 | TEST_QUERY_STOP, |
||
213 | ) |
||
214 | |||
215 | def test_start_on_asset_start(self): |
||
216 | """ |
||
217 | Test loading with queries that starts on the first day of each asset's |
||
218 | lifetime. |
||
219 | """ |
||
220 | columns = [USEquityPricing.high, USEquityPricing.volume] |
||
221 | for asset in self.assets: |
||
222 | self._check_read_results( |
||
223 | columns, |
||
224 | self.assets, |
||
225 | start_date=self.asset_start(asset), |
||
226 | end_date=self.trading_days[-1], |
||
227 | ) |
||
228 | |||
229 | def test_start_on_asset_end(self): |
||
230 | """ |
||
231 | Test loading with queries that start on the last day of each asset's |
||
326 |