[TIP] I need a DataSet fixture that points into its own table

Mark Sienkiewicz sienkiew at stsci.edu
Mon Dec 27 12:34:41 PST 2010


Phlip wrote:
> This is a basic tree configuration:
> 
> class TreeData(fixture.DataSet):
> 
>     class branch:
>         tag = 'branch'
> 
>     class leaf_one:
>         tag = 'leaf_one'
>         owner = TreeData.branch
> 
>     class leaf_two:
>         tag = 'leaf_two'
>         owner = TreeData.branch
> 
> Python doesn't like that because (unlike real languages) TreeData 
> doesn't appear in the lookup namespace until it's fully defined.
> 
> The work-around is to build a different suite for each tier of the tree:
...
> That's really tacky - is there a simpler fix?

This is a little simpler:

class TreeData(fixture.DataSet):

    class branch:
        tag = 'branch'

    class leaf_one:
        tag = 'leaf_one'

    class leaf_two:
        tag = 'leaf_two'

TreeData.leaf_one.owner = TreeData.branch
TreeData.leaf_two.owner = TreeData.branch

This works because your original "owner = TreeData.branch" lines are executable assignment statements in python, not declarations like they would be in java or c++.  The value you want isn't available yet when you are still executing the class definition code, so I've just moved the assignment to someplace after the class definition.  As long as you don't use the class until after the assignments are done, you get the same data structure as you might have expected from your original code.

Mark S.



More information about the testing-in-python mailing list