Completed
Pull Request — master (#912)
by Eddie
01:31
created

tests.utils.DailyBarWriterFromDataFrames   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 34
Duplicated Lines 0 %
Metric Value
dl 0
loc 34
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A gen_tables() 0 3 2
A __init__() 0 2 1
A check_uint_safe() 0 5 2
A to_uint32() 0 12 4
1
# Copyright 2015 Quantopian, Inc.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14
from numpy import (
15
    float64,
16
    uint32
17
)
18
from bcolz import ctable
19
20
from zipline.data.us_equity_pricing import (
21
    BcolzDailyBarWriter,
22
    OHLC,
23
    UINT32_MAX
24
)
25
26
27
class DailyBarWriterFromDataFrames(BcolzDailyBarWriter):
28
    _csv_dtypes = {
29
        'open': float64,
30
        'high': float64,
31
        'low': float64,
32
        'close': float64,
33
        'volume': float64,
34
    }
35
36
    def __init__(self, asset_map):
37
        self._asset_map = asset_map
38
39
    def gen_tables(self, assets):
40
        for asset in assets:
41
            yield asset, ctable.fromdataframe(assets[asset])
42
43
    def to_uint32(self, array, colname):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
        arrmax = array.max()
45
        if colname in OHLC:
46
            self.check_uint_safe(arrmax * 1000, colname)
47
            return (array * 1000).astype(uint32)
48
        elif colname == 'volume':
49
            self.check_uint_safe(arrmax, colname)
50
            return array.astype(uint32)
51
        elif colname == 'day':
52
            nanos_per_second = (1000 * 1000 * 1000)
53
            self.check_uint_safe(arrmax.view(int) / nanos_per_second, colname)
54
            return (array.view(int) / nanos_per_second).astype(uint32)
55
56
    @staticmethod
57
    def check_uint_safe(value, colname):
58
        if value >= UINT32_MAX:
59
            raise ValueError(
60
                "Value %s from column '%s' is too large" % (value, colname)
61
            )
62