Back to SDS/2 Parametric Scripts
class DataDict:
"""This class initializes a
dictionary for data storage (Python 2.4)"""
def __init__(self,
n, *section):
self.block =
{}
for s in
section:
self.block[s]
= dict(['v'+str(i), 0] for i in range(n))
def __iter__(self):
yield self.block.keys()
def __str__(self):
return '\n'.join(['%s = %s' % (key, self.block[key])
for key in self.block])
def dd(self, sect):
# dd.items() returns a list, dd.iteritems()
returns an iterator
return [i for i in self.block[sect].iteritems()]
if __name__ == '__main__':
D = DataDict(3, 'Qsection', 'Psection', 'Zsection')
print D.dd('Qsection')
print D.dd('Psection')
'''
>>>
[('v0', 0), ('v1',
0), ('v2', 0)]
[('v0', 0), ('v1',
0), ('v2', 0)]
>>> print D
Qsection = {'v0': 0, 'v1': 0, 'v2': 0}
Zsection = {'v0': 0, 'v1': 0, 'v2': 0}
Psection = {'v0': 0, 'v1': 0, 'v2': 0}
>>> print [x for x in D.block]
['Qsection', 'Zsection', 'Psection']
>>> print
[x for x in D.block.iteritems()]
[('Qsection', {'v0': 0, 'v1': 0, 'v2': 0}), ('Zsection', {'v0': 0, 'v1': 0, 'v2': 0}), ('Psection', {'v0': 0, 'v1': 0, 'v2': 0})]
>>> for x in D.block:
print '%s = %s' %
(x, D.block[x])
Qsection = {'v0': 0, 'v1': 0, 'v2': 0}
Zsection = {'v0': 0, 'v1': 0, 'v2': 0}
Psection = {'v0': 0, 'v1': 0, 'v2': 0}
>>>
'''
'''
# Update the local
namespace
>>> for i in range(25):
... exec "%s = %d" % ('var'+str(i), 0) in None
# Update the global
namespace
>>> dd = {}
>>> for i in range(25):
... dd['var'+str(i)] = 0
>>> globals.update(dd)
# Update the local
namespace with dictionary dd
>>> for key in dd:
... exec "%s = %s" % (key, repr(dd[key])) in None
...
>>>
# Update a module
namespace with dd
>>> import
P3D
>>>
P3D.__dict__.update(dd)
>>> var0
0
>>> var1
0
>>> var24
0
>>>
'''