File: simple_pickly.py

package info (click to toggle)
python-boost-histogram 1.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,232 kB
  • sloc: python: 7,745; cpp: 3,243; makefile: 22; sh: 1
file content (36 lines) | stat: -rwxr-xr-x 622 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
#!/usr/bin/env python3

from __future__ import annotations

import pickle
from pathlib import Path

import boost_histogram as bh

h1 = bh.Histogram(bh.axis.Regular(2, -1, 1))
h2 = h1.copy()

h1.fill(-0.5)
h2.fill(0.5)

# Arithmetic operators
h3 = h1 + h2
h4 = h3 * 2

print(f"h4[0] = {h4[0]}, h4[1] = {h4[1]}")

h4_saved = Path("h4_saved.pkl")

# Now save the histogram
with h4_saved.open("wb") as f:
    pickle.dump(h4, f, protocol=-1)

# And load
with h4_saved.open("rb") as f:
    h5 = pickle.load(f)

assert h4 == h5
print("Succeeded in pickling a histogram!")

# Delete the file to keep things tidy
h4_saved.unlink()