def createNode(doc_, **param_):
cache = {‘p’:’property’,’l’:’label’,’td’:’totalDelay’,’rd’:’routeDelay’,’ld’:’logicDelay’}
for index in param_:
newIndex = cache[index]
value = param_[index]
print newIndex, ‘=’, valuedoc = 10
createNode(doc, p=’path’, l=’ifft4k_radix4_noUnolling_core.vi’, td=’3.0′, ld=’1.0′, rd=’2.0′)
Running this code on Python 2.6 gives me the following result.
routeDelay = 2.0
property = path
totalDelay = 3.0
logicDelay = 1.0
label = ifft4k_radix4_noUnolling_core.vi
I need to keep the order of the parameters, I mean, property comes first, then label until I get routeDelay last.
Q : What’s the way to keep the dictionary parameter order in Python?
Then you’re simply doing things in the wrong order — no need for ordered dictionaries! Try, instead, a tuple of pairs for cache, as follows:
This has exactly the same semantics as your version of
createNode, plus the desired property of maintaining the order you wish, and performance is at least as good. By focusing on “keeping the dictionary ordered” you’re focusing on the wrong problem (as is the case, alas, for most cases where people reach for “ordered dictionaries”!-).