File: test_subclassing.py

package info (click to toggle)
python-boost-histogram 1.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,236 kB
  • sloc: python: 7,940; cpp: 3,243; makefile: 22; sh: 1
file content (53 lines) | stat: -rw-r--r-- 1,251 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from __future__ import annotations

import boost_histogram as bh


def test_subclass():
    NEW_FAMILY = object()

    class MyHist(bh.Histogram, family=NEW_FAMILY):
        pass

    class MyRegular(bh.axis.Regular, family=NEW_FAMILY):
        __slots__ = ()

    class MyIntStorage(bh.storage.Int64, family=NEW_FAMILY):
        pass

    class MyPowTransform(bh.axis.transform.Pow, family=NEW_FAMILY):
        pass

    h = MyHist(MyRegular(10, 0, 2, transform=MyPowTransform(2)), storage=MyIntStorage())

    assert type(h) is MyHist
    assert h.storage_type == MyIntStorage
    assert type(h.axes[0]) is MyRegular
    assert type(h.axes[0].transform) is MyPowTransform


def test_subclass_hist_only():
    class MyHist(bh.Histogram):
        pass

    h = MyHist(bh.axis.Regular(10, 0, 2))

    assert type(h) is MyHist
    assert type(h.axes[0]) is bh.axis.Regular


def test_copy():
    class MyHist(bh.Histogram):
        def __init__(self, var, bins, weight, **kwargs):
            super().__init__(
                bh.axis.Regular(*bins), storage=bh.storage.Weight(), **kwargs
            )

            self.fill(var, weight=weight)

    b = (2, 0, 1)
    v = [0.1, 0.5, 0.9]
    w = [1, 0.5, 1]
    hist = MyHist(v, b, w)

    hist.copy()