Dictionaries -- ahh the fun.. (newbie help)
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Tue May 9 22:34:54 EDT 2006
rh0dium a écrit :
> Hi all,
>
> Can someone help me out. I am trying to determing for each run whether
> or not the test should pass or fail but I can't seem to access the
> results ..
>
(snip)
> cells={}
>
> cells["NOR3X1"]= {
> 'run' : [ 'lvs', 'drc' ],
> 'results' : [{ 'lvs' : 'pass' },
> { 'drc' : 'fail' }]
> }
>
> cells["OR3X1"] = {
> 'run' : [ 'lvs' ],
> 'results' : [{ 'lvs' : 'pass' }]
> }
>
> cells["AND3X1"] = {
> 'run' : [ 'drc' ],
> 'results' : [{ 'drc' : 'fail' }]
> }
>
>
> def main():
>
> for cell in cells:
> print cell
> for run in cells[cell]['run']:
> print cell, run, "should",
> cells[cell]['results'].index(run)
>
>
> I would expect the following
>
> OR3X1
> OR3X1 lvs should pass
> NOR3X1
> NOR3X1 lvs should pass
> NOR3X1 drc should fail
> AND3X1
> AND3X1 drc should fail
>
Congratulations, almost a perfect post - you just forgot to tell the
actual result !-)
>>> ## working on region in file /usr/tmp/python-99973O0...
>>> main()
OR3X1
OR3X1 lvs should
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/tmp/python-99973O0", line 24, in main
ValueError: list.index(x): x not in list
May I suggest that you type "help(list.index)" in your python shell ?
> Alternatively can someone suggest a better structure ( and a lesson as
> to the reasoning ) that would be great too!!
What seems flawed is the cell['results'] : you use a list of dicts, each
one having a single entry - which is somewhat useless. Since you want to
use the values of cell['run'] to lookup the expected results, using a
single dict for results makes things a bit more usable:
cells={}
cells["NOR3X1"]= {
'run' : [ 'lvs', 'drc' ],
'results' : {'lvs' : 'pass',
'drc' : 'fail' }
}
cells["OR3X1"] = {
'run' : [ 'lvs' ],
'results' : { 'lvs' : 'pass' }
}
cells["AND3X1"] = {
'run' : [ 'drc' ],
'results' : { 'drc' : 'fail' }
}
def main():
for cellname, cellcontent in cells.items():
print cellname
for run in cellcontent['run']:
print cellname, run, "should", cellcontent['results'][run]
This runs and produces the expected output. Now let's simplify. The
values of cell['run'] are uselessly duplicated as keys in
cell['results'] - and duplication is Bad(tm). So let's get rid of this:
cells={}
cells["NOR3X1"]= {
'results' : {'lvs' : 'pass',
'drc' : 'fail' }
}
cells["OR3X1"] = {
'results' : { 'lvs' : 'pass' }
}
cells["AND3X1"] = {
'results' : { 'drc' : 'fail' }
}
Now we only have a single entry ('results') for each cell. This is a
useless indirection level, so let's get rid of this too:
cells={
"NOR3X1": {
'lvs' : 'pass',
'drc' : 'fail',
},
"OR3X1" : {
'lvs' : 'pass',
},
"AND3X1" : {
'drc' : 'fail',
},
}
Looks simpler, isn't it ?-)
def main():
for cellname, cellcontent in cells.items():
print cellname
for run, result in cellcontent.items():
print cellname, run, "should", result
If you want to get the equivalent of cells[XXX]['runs'], just use
cells[XXX].keys().
HTH
More information about the Python-list
mailing list