1

I have a data structure (created from a YAML document) that looks like this:

{
    'SD_TX_EN_CFG[5]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 5)', 'Offset': 43024},
    'SD_TX_EN_CFG[4]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 4)', 'Offset': 40976},
    'SD_RX_EN_CFG[5]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 5)', 'Offset': 43008},
    'SD_RX_EN_CFG[3]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 3)', 'Offset': 38912},
    'SD_RX_EN_CFG[2]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 2)', 'Offset': 36864},
    'SD_RX_EN_CFG[0]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 0)', 'Offset': 32768}, 
    'aif2_scratch': {'Offset': 4, 'Bits': {'0-31': 'scratch'}, 'Description': None, 'Name': 'aif2_scratch'},
    'SD_RX_R2_CFG[0]': {'Bits': {'15-16': 'RXLOOPBACK // 00 = Disabled, 11 = Enabled'}, 'Name': 'RX Configuration Register 2 (Link 0)', 'Offset': 32776},
    'aif2_pid': {'Offset': 0, 'Bits': {'6-7': 'Custom version code', '8-10': 'Major revision X code // \n    Should be 1\n', '0-5': 'Minor revision Y code'}, 'Description': None, 'Name': 'aif2_pid'},
    'SD_TX_EN_CFG[1]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 1)', 'Offset': 34832},
    'SD_TX_EN_CFG[0]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 0)', 'Offset': 32784},
    'SD_RX_EN_CFG[1]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 1)', 'Offset': 34816},
    'SD_RX_EN_CFG[4]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 4)', 'Offset': 40960},
    'SD_TX_R1_CFG[0]': {'Bits': {'15-16': 'TXLOOPBACK // 00 = Disabled, 11 = Enabled'}, 'Name': 'TX Configuration Register 1 (Link 0)', 'Offset': 32788}, 
    'SD_TX_EN_CFG[2]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 2)', 'Offset': 36880},
    'RM_LK_CFG0[0]': {'Bits': {0: 'Short frame mode // 0 = CPRI, 1 = OBSAI', '2-3': 'Link Rate // 0 = 8x, 1 = 4x, 2 = 2x, 3 = 1x', 1: 'Enable RM Link // 0 = Disabled, 1 = Enabled'}, 'Name': 'RM Link Configuration Register 0 (Link 0)', 'Offset': 327680},
    'SD_TX_EN_CFG[3]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 3)', 'Offset': 38928}
}

The YAML definition looks like

SD_RX_EN_CFG[0]:
  Name: RX Enable Register (Link 0)
  Offset: 0x8000
  Bits:
    0: ENRX // 0 = Disabled, 1 = Enabled

etc.

I would like to handle each element in the order defined by 'Offset' but I can't seem to manage to do this. I have read and re-read question 72899 but this seems a bit more complicated.

1

4 Answers 4

9
for k, v in sorted(data.iteritems(), key=lambda (k,v): v['Offset']):
    print k, v['Offset']

prints:

aif2_pid 0
aif2_scratch 4
SD_RX_EN_CFG[0] 32768
SD_RX_R2_CFG[0] 32776
SD_TX_EN_CFG[0] 32784
SD_TX_R1_CFG[0] 32788
SD_RX_EN_CFG[1] 34816
SD_TX_EN_CFG[1] 34832
SD_RX_EN_CFG[2] 36864
SD_TX_EN_CFG[2] 36880
SD_RX_EN_CFG[3] 38912
SD_TX_EN_CFG[3] 38928
SD_RX_EN_CFG[4] 40960
SD_TX_EN_CFG[4] 40976
SD_RX_EN_CFG[5] 43008
SD_TX_EN_CFG[5] 43024
RM_LK_CFG0[0] 327680

UPDATE: in Python 3.x you would use (because of iteritems and lambda changes):

for k, v in sorted(data.items(), key=lambda x: x[1]['Offset']):
    print k, v['Offset']

(This works in Python 2.x too.)

Sign up to request clarification or add additional context in comments.

2 Comments

+1, I was thinking along the same lines. Note that lambda (k,v): v['Offset'] is not supported in Python 3 and for that it'll be written as lambda p: p[1]['Offset'].
@DanD. - I am always thinking in Python 2.x, that's why I used .iteritems() too.
2

Where data is the dict you posted:

sorted(data.items(), key=lambda i: i[1]['Offset'])

1 Comment

Since this works with Python 3 as well, I chose this as the best answer. Plus it was 1 minute before phihag's answer.
2

You can use the key argument of sorted. Assuming ds is the dictionary you read from YAML:

sortedByOffset = sorted(ds.items(), key=lambda item: item[1]['Offset'])

Comments

1

Here is how I played around with it:

d={'SD_TX_EN_CFG[5]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 5)', 'Offset': 43024}, 'SD_TX_EN_CFG[4]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 4)', 'Offset': 40976}, 'SD_RX_EN_CFG[5]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 5)', 'Offset': 43008}, 'SD_RX_EN_CFG[3]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 3)', 'Offset': 38912}, 'SD_RX_EN_CFG[2]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 2)', 'Offset': 36864}, 'SD_RX_EN_CFG[0]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 0)', 'Offset': 32768}, 'aif2_scratch': {'Offset': 4, 'Bits': {'0-31': 'scratch'}, 'Description': None, 'Name': 'aif2_scratch'}, 'SD_RX_R2_CFG[0]': {'Bits': {'15-16': 'RXLOOPBACK // 00 = Disabled, 11 = Enabled'}, 'Name': 'RX Configuration Register 2 (Link 0)', 'Offset': 32776}, 'aif2_pid': {'Offset': 0, 'Bits': {'6-7': 'Custom version code', '8-10': 'Major revision X code // \n    Should be 1\n', '0-5': 'Minor revision Y code'}, 'Description': None, 'Name': 'aif2_pid'}, 'SD_TX_EN_CFG[1]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 1)', 'Offset': 34832}, 'SD_TX_EN_CFG[0]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 0)', 'Offset': 32784}, 'SD_RX_EN_CFG[1]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 1)', 'Offset': 34816}, 'SD_RX_EN_CFG[4]': {'Bits': {0: 'ENRX // 0 = Disabled, 1 = Enabled'}, 'Name': 'RX Enable Register (Link 4)', 'Offset': 40960}, 'SD_TX_R1_CFG[0]': {'Bits': {'15-16': 'TXLOOPBACK // 00 = Disabled, 11 = Enabled'}, 'Name': 'TX Configuration Register 1 (Link 0)', 'Offset': 32788}, 'SD_TX_EN_CFG[2]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 2)', 'Offset': 36880}, 'RM_LK_CFG0[0]': {'Bits': {0: 'Short frame mode // 0 = CPRI, 1 = OBSAI', '2-3': 'Link Rate // 0 = 8x, 1 = 4x, 2 = 2x, 3 = 1x', 1: 'Enable RM Link // 0 = Disabled, 1 = Enabled'}, 'Name': 'RM Link Configuration Register 0 (Link 0)', 'Offset': 327680}, 'SD_TX_EN_CFG[3]': {'Bits': {0: 'ENTX // 0 = Disabled, 1 = Enabled'}, 'Name': 'TX Enable Register (Link 3)', 'Offset': 38928}}
import pprint
pprint.pprint(d)

def f(i):
    return d[i]['Offset']

for i in d:
    print f(i)

_list=sorted(d,key=f)

def process(thing):
    pass

for k in _list:
    process(d[k])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.