File: vlarray1.py

package info (click to toggle)
pytables 3.10.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,228 kB
  • sloc: ansic: 82,212; python: 65,296; cpp: 753; sh: 394; makefile: 100
file content (44 lines) | stat: -rw-r--r-- 1,086 bytes parent folder | download | duplicates (2)
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
import numpy as np

import tables as tb

# Create a VLArray:
fileh = tb.open_file("vlarray1.h5", mode="w")
vlarray = fileh.create_vlarray(
    fileh.root,
    "vlarray1",
    tb.Int32Atom(shape=()),
    "ragged array of ints",
    filters=tb.Filters(1),
)
# Append some (variable length) rows:
vlarray.append(np.array([5, 6]))
vlarray.append(np.array([5, 6, 7]))
vlarray.append([5, 6, 9, 8])

# Now, read it through an iterator:
print("-->", vlarray.title)
for x in vlarray:
    print("%s[%d]--> %s" % (vlarray.name, vlarray.nrow, x))

# Now, do the same with native Python strings.
vlarray2 = fileh.create_vlarray(
    fileh.root,
    "vlarray2",
    tb.StringAtom(itemsize=2),
    "ragged array of strings",
    filters=tb.Filters(1),
)
vlarray2.flavor = "python"
# Append some (variable length) rows:
print("-->", vlarray2.title)
vlarray2.append(["5", "66"])
vlarray2.append(["5", "6", "77"])
vlarray2.append(["5", "6", "9", "88"])

# Now, read it through an iterator:
for x in vlarray2:
    print("%s[%d]--> %s" % (vlarray2.name, vlarray2.nrow, x))

# Close the file.
fileh.close()