-3

I have data like this:

data = [{'1234': [{u'gateway_ip': u'172.20.21.1',
                u'ipv6_block': None,
                u'private_block': u'172.20.21.0/24',
                u'segment_name': u'VLAN1',
                u'switch_ports': [{u'name': u'fa0/32',
                                   u'switch_name': u'switch1.local',
                                   u'switch_port_interface_type_name': u'eth'},
                                  {u'name': u'fa0/15',
                                   u'switch_name': u'switch2.local',
                                   u'switch_port_interface_type_name': u'eth'}],
                u'vlan_name': u'INSIDE',
                u'vlan_number': 2031},
               {u'gateway_ip': u'172.20.31.1',
                u'ipv6_block': None,
                u'private_block': u'172.20.31.0/24',
                u'segment_name': u'VLAN2',
                u'switch_ports': [{u'name': u'fa0/32',
                                   u'switch_name': u'switch1.local',
                                   u'switch_port_interface_type_name': u'eth'},
                                  {u'name': u'fa0/15',
                                   u'switch_name': u'switch2.local',
                                   u'switch_port_interface_type_name': u'eth'}],
                u'vlan_name': u'DMZ',
                u'vlan_number': 2037}]}]

I want to parse this data to get something like this:

[{1234:[{switch1.local:[{fa0/32:[2031,2037],{fa0/15:[2031,2037]}}]},{switch2.local:[{fa0/32:[2031,2037],{fa0/15:[2031,2037]}}]}]}]

basically I want to get list of vlans assigned to specific switchport and device

[{device:[{switch1:{port1:[vlans-list]}},{switch1:{port2:[vlans-list]}}]}] 

when I do it with append on list what I know now is not correct I get result like that :

[{device:[{switch1:{port1:[vlan1]}},{device:[{switch1:{port2:[vlan2]}]}}]}]

I'm sure this will not make sense to you in first place so please ask questions I will try answer them best I can.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
miro20
  • 1
  • 2
  • SO is about not "us" asking questions about your topic but you formulating a question according to this [guideline](https://stackoverflow.com/help/how-to-ask). – decltype_auto Dec 13 '15 at 17:41
  • 1
    I wrote the code because I liked the "challenge", however, Stackoverflow is not site to request for codes, but to ask questions. – macabeus Dec 13 '15 at 18:01

2 Answers2

0

The return is not exactly what you suggested, however, that way there is less redundant data.

dict_out = {'vlan_number': {}}

for i in data:
    for k, v in i.items():
        dict_out[k] = [{sp['switch_name']: sp['name'] for sp in i['switch_ports']}
                       for i in v]
        dict_out['vlan_number'][k] = [i['vlan_number'] for i in v]

Return:

{'vlan_number': {'1234': [2031, 2037]}, '1234': [{'switch1.local': 'fa0/32', 'switch2.local': 'fa0/15'}, {'switch1.local': 'fa0/32', 'switch2.local': 'fa0/15'}]}
macabeus
  • 4,156
  • 5
  • 37
  • 66
  • Thank you Macabeus for this, I will try this on more data if this works for me, do not get me wrong i did not want answer or code but i have tried everything I know and I looked through forum/google but did not find anything what would solve my problem also my knowledge in programming/scripting is very limited as you can see :) – miro20 Dec 13 '15 at 18:17
  • I have tried this on more data, unlucky this does not work as expected it assumes that all ports are configured same, but some may have 3 vlans and some may have 10, output data will have same vlans for all ports. – miro20 Dec 13 '15 at 19:01
0

New code, other did not work exactly I was expecting

def f_create_port_data(a_swichport_data):
    v_port_data=[{}]
    v_switch_ports=[]
    for l_device_port_list in a_swichport_data:
        v_device_port_data=[]
        for device,key in l_device_port_list.iteritems():
            for vlan in l_device_port_list[device]:
                for port in l_device_port_list[device][0]['switch_ports']:
                    v_switch_ports.append(port['switch_name']+"="+port['name'])
            v_switch_ports_list=list(set(v_switch_ports))
            for l_port in v_switch_ports_list:
                v_vlan_list=[]
                v_location=l_port.split('=')
                for l_vlan in l_device_port_list[device]:
                    for l_switchport in l_vlan['switch_ports']:
                        if (v_location[0]==l_switchport['switch_name'] and v_location[1]==l_switchport['name']):
                            v_vlan_list.append(l_vlan['vlan_number'])
                if (not v_port_data[0].has_key(device)):
                    v_port_data[0][device]=list()
                    v_port_data[0][device].append(dict())
                if (not v_port_data[0][device][0].has_key(v_location[0])):
                    v_port_data[0][device][0][v_location[0]]=list()
                    v_port_data[0][device][0][v_location[0]].append(dict())
                if (not v_port_data[0][device][0][v_location[0]][0].has_key(v_location[1])):
                    v_port_data[0][device][0][v_location[0]][0][v_location[1]]=v_vlan_list


    return v_port_data
skrrgwasme
  • 9,358
  • 11
  • 54
  • 84
miro20
  • 1
  • 2
  • I have edited your answer to show you how to use indentation for code formatting. You should also add some explanatory comments or a paragraph that explains what your code does and how it solves the original poster's problem. Also, there is an edit button just below your answer on the left side - use that next time to edit your answer if you find an issue with it, instead of adding a second answer. In the meantime, you should delete your other answer on this question. It has no value any more. – skrrgwasme Dec 16 '15 at 20:10