Example Read FilesΒΆ

This script reads the contents of both example_write*.py scripts (which should both be run prior to running this script) and prints the file contents:

> python example_read.py

Opening file(s): foo*.pdb
x: 3
tm: [0, 1, 2]
y: [0.1, 0.2, 0.3]
z: [array([0, 1, 2]), array([1, 2, 3]), array([2, 3, 4])]
a: 10

Opening file(s): foo.h5
a: 10
tm: [0, 1, 2]
x: 3
y: [0.1, 0.2, 0.3]
z: [array([0, 1, 2]), array([1, 2, 3]), array([2, 3, 4])]

It is located at [QND repo]/qnd/examples/example_read.py.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
'''
example_read.py opens and lists the contents of files produced by
example_write_pdb.py and example_write_h5.py
'''
import numpy as np
from qnd.lazy import openb
from qnd.frontend import QList

fnames = ["foo*.pdb", "foo.h5"]

for fname in fnames:
    print("\nOpening file(s): {}".format(fname))

    with openb(fname, "r") as f:
        for k in list(f):
            val = f[k]
            if type(f[k]) is QList:
                val = val[:]
            print(f"{k}: {val}")