0

Based on the PyTables documentation, it appears that the only way to define nested types is either to create a class level/static field with the nested type instance or to define the nested class in the parent class.

The problem is, a very common tree representation simply uses a Node type with child Node instances. Normally, this is not a problem for a Python class since the dynamic typing does not force me define the type of the child(ren), and at runtime, A Node instance can be added to another Node instance as a child.

PyTables on the other hand, requires that the type of the field is defined. Since a class definition in Python can't be used in a class level field initialization, the most common way of parent - child structure definition becomes unavailable. I am not sure if this is a constraint that exists at the HDF5 level though (did not check). Here is an example of the problem:

class A(IsDescription):
    valstring = StringCol(250, pos=1)
    child = A()# IMPOSSIBLE 

class A(IsDescription):
    valstring = StringCol(250, pos=1)
    #the following would work, but now I can't define
    #another AChild as child, so I got stuck with depth 1
    class AChild(IsDescription):
        valstring = StringCol(250, pos=1)
        class ANewChild(IsDescription):
            valstring = ....#useless

I have data that fits the type Node with Node children definition, and I can't represent it with PyTables at the moment. Is there a trick I'm missing?

mahonya
  • 9,247
  • 7
  • 39
  • 68

1 Answers1

0

A table is not the right place to put recursive structures. Try if you can get what want by using the group tree. Since this works analogous to directories and subdirectories, it may solve your problem. You can always put subgroups and tables into one group. In addition you can use attributes to groups. This allows you to store shorter strings and such.

This has nothing to do with PyTables. Try this in pure Python

>>> class A(object):
...     a = A()
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in A
NameError: name 'A' is not defined

You cannot use a name that is not defined yet. Likely, you think about this:

>>> class A(object):
...     def __init__(self):
...         self.a = A()

No Problem here. The difference is that in the frist case we have a class variable and in the second an instance variable. While a = A() will be executed when Python complies the source code, self.a = A() will only be executed when you make an instance of A, i.e. at run time.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • I am aware of this. I've written "Since a class definition in Python can't be used in a class level field initialization..." in my question. pyTables uses class variables. So I have to use class variables, hence: the problem I've explained in my question. – mahonya May 30 '13 at 08:58
  • Kind of over read this. Added some hints that may help in the answer. – Mike Müller May 30 '13 at 09:26