"""Small example showing the use of nested types in PyTables.

The program creates an output file, 'nested-tut.h5'.  You can view it
with ptdump or any HDF5 generic utility.

:Author: F. Altet
:Date: 2005/06/10
"""

import numpy

from tables import *

        #'-**-**-**-**- The sample nested class description  -**-**-**-**-**-'

class Info(IsDescription):
    """A sub-structure of Test"""
    _v_pos = 2   # The position in the whole structure
    name = StringCol(15, pos=1)
    value = Float64Col(pos=2)

colors = Enum(['red', 'green', 'blue'])

class NestedDescr(IsDescription):
    """A description that has several nested columns"""
    color = EnumCol(colors, 'red', base='uint32',pos=1)
    info1 = Info()
    class info2(IsDescription):
        _v_pos = 3
        name = StringCol(15, pos=1)
        value = Float64Col(pos=2)
        class info3(IsDescription):
			_v_pos = 3
			x = Float64Col(dflt=1, pos=1)
			y = UInt8Col(dflt=1, pos=2)
	info4 = Info()

print
print   '-**-**-**-**-**-**- file creation  -**-**-**-**-**-**-**-'

filename = "nested-tut_AF.h5"

print "Creating file:", filename
fileh = openFile(filename, "w")

print
print   '-**-**-**-**-**- nested table creation  -**-**-**-**-**-'

table = fileh.createTable(fileh.root, 'table', NestedDescr)
# Fill the table with some rows
row = table.row
for i in range(10):
    row['color'] = colors[['red', 'green', 'blue'][i%3]]
    row['info1/name'] = "name1-%s" % i
    row['info2/name'] = "name2-%s" % i
    row['info2/info3/y'] =  i
    # All the rest will be filled with defaults
    row.append()

table.flush()  # flush the row buffer to disk


# Read the data
print
print "**** table data contents:\n", table[:]

print 'table.cols.info1.name[2] = ', table.cols.info1.name[2]
table.cols.info1.name[2] = 'CHANGED_STRING'
table.flush()
print 'table.cols.info1.name[2]', table.cols.info1.name[2]

fileh.flush()

# Remember to always close the file
fileh.close()
