Completed
Pull Request — master (#858)
by Eddie
10:07 queued 01:13
created

check_uint_safe()   A

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 5
rs 9.4286
1
from numpy import (
2
    float64,
3
    uint32
4
)
5
from bcolz import ctable
6
7
from zipline.data.us_equity_pricing import (
8
    BcolzDailyBarWriter,
9
    OHLC,
10
    UINT32_MAX
11
)
12
13
14
class DailyBarWriterFromDataFrames(BcolzDailyBarWriter):
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...
15
    _csv_dtypes = {
16
        'open': float64,
17
        'high': float64,
18
        'low': float64,
19
        'close': float64,
20
        'volume': float64,
21
    }
22
23
    def __init__(self, asset_map):
24
        self._asset_map = asset_map
25
26
    def gen_tables(self, assets):
27
        for asset in assets:
28
            yield asset, ctable.fromdataframe(assets[asset])
29
30
    def to_uint32(self, array, colname):
31
        arrmax = array.max()
32
        if colname in OHLC:
33
            self.check_uint_safe(arrmax * 1000, colname)
34
            return (array * 1000).astype(uint32)
35
        elif colname == 'volume':
36
            self.check_uint_safe(arrmax, colname)
37
            return array.astype(uint32)
38
        elif colname == 'day':
39
            nanos_per_second = (1000 * 1000 * 1000)
40
            self.check_uint_safe(arrmax.view(int) / nanos_per_second, colname)
41
            return (array.view(int) / nanos_per_second).astype(uint32)
42
43
    @staticmethod
44
    def check_uint_safe(value, colname):
45
        if value >= UINT32_MAX:
46
            raise ValueError(
47
                "Value %s from column '%s' is too large" % (value, colname)
48
            )
49