I am trying to nest nametuples trying to create a data structure where the data could ultimately be accesed with this nomenclature:
parent.child.var1.a i.e. dot notation without brackets.
i know that might be better coded in other ways but I am just dong this metal exercice ttring to find out if that is possible with nested namedtupples.
so far I have:
sub=namedtuple('sub',('var1','var2'))
first =sub(5,6)
second = sub(7,8)
third = sub(9,10)
fleet = {'first':first,'second':second,'third':third}
# ideally here the nametupple would be create on the fly with
# def create_parent(name, fleet)
filing = namedtuple('filing',fleet.keys())
for i,value in enumerate(fleet.values()):
filing[i] = value
this does not work because the assignment filing[i] gives error.
basically I create a first namedtuple called sub and I try to create on the fly another named tupple with a variable amount of "subs" in it. I created a function as constructor adding the variables as the keys of the dictionary. so far so good. but the problem arises when i want to assign the content of the variables. My intention was to look over the dict and "somehow" coding in in the following pseudo code:
for every key value of the dict asign the value to the var called key of the named tuple called filing. To be more precise, when looping I pretend to get the value of every key of the dictionary, which is a variable name in the namedtuple, and pass some data to it.
any idea?
NOTE: if this works the intention is to further nest named tuples and being able to access the deepest data as parent.second.var1 etc.
NOTE2: according to the first answer of course I can not change the values of the tuple. A tupple is unmutable. What I want to create is a nametuple on the fly with the name of the variables being the keys of a dictionary and the values of those variables the dict.values().
NOTE3: The proposed answer, as far as I can see, uses nested nametuples but not tuples created on the fly. i.e. the key and difficult there is nesting the namedtuples. In my question the key point is to create a namedtuple based on other object, a dictionary, such that both, the name of the variables depend on other object (the dict.keys) and the values as well (dict.values()).