Python Forum
DevNet Help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: General Coding Help (https://python-forum.io/Forum-General-Coding-Help)
+--- Thread: DevNet Help (/Thread-DevNet-Help)



DevNet Help - benniehanas - Aug-07-2019

I am begenning DevNet from a pure Networking background. I have some code I'm looking at from 'Network Programmability and Automation' book. Don't know how '
temp['name'] = name
' works.

vlans_list = [
    'vlan 10', 'name DATA', 'vlan 20', 'name VOICE', 'vlan 30', 'name WIRELESS'
]

vlans = []

for item in vlans_list:
    if 'vlan' in item: 
        temp = {}
        id = item.strip().strip('vlan').strip()
        temp['id'] = id
    elif 'name' in item:
        name = item.strip().strip('name').strip()
        temp['name'] = name
        vlans.append(temp)
I see strip stripping off vlan and whitespace with the string of strips. Then the string is left with only '10' as in the first string in vlans_list. I don't know where id comes from. Does
var['string'] = 'string'
write the value declared for that line in the iteration? I cannot find this usage in documentation. Basically this script is replacing/stripping 'vlan' for 'id', but code confuses me no matter how simple it seems.


RE: DevNet Help - ichabod801 - Aug-07-2019

temp is a dict. A dict can have any hashable object as a key, including strings. temp['name'] = name assigns the value associated with the variable name to the key 'name' in temp. Stepping through the code:

vlans_list = [
    'vlan 10', 'name DATA', 'vlan 20', 'name VOICE', 'vlan 30', 'name WIRELESS'
]
 
vlans = []
 
for item in vlans_list:                           # loop through vlans_list, assigning each member to item
    if 'vlan' in item:                            # check for vlan being in the string
        temp = {}                                 # create a new dictionary
        id = item.strip().strip('vlan').strip()   # remove vlan from the string, assign the result to id
        temp['id'] = id                           # store id in the new dictionary (key = 'id')
    elif 'name' in item:                          # check for name being in the string
        name = item.strip().strip('name').strip() # remove 'name' from the string, assign it to variable name
        temp['name'] = name                       # store name in the dictionary.
        vlans.append(temp)                        # store the dictionary with the vlan info in a list
Each vlan ends up with it's own dictionary in the list vlans.


RE: DevNet Help - benniehanas - Aug-07-2019

(Aug-07-2019, 05:25 PM)ichabod801 Wrote: temp['id'] = id                           # store id in the new dictionary (key = 'id')

So if you state dictionary['string'] = 'string', Python will insert your string in the line you are on in the iteration? In other words, It is stating the string 'id' is == to the var id from the previous line because dictionaries are key/value pairs and the key == the value?


RE: DevNet Help - ichabod801 - Aug-08-2019

No. The part you got right is that dictionaries are key/value pairs. temp['id'] = id associates (the value in the variable id) with the key 'id' in the dictionary temp. Now that value will be returned when you use temp['id'].


RE: DevNet Help - benniehanas - Aug-08-2019

one more question: why isn't it instead written as
temp{'id'} = id
since temp is a dictionary and not a list and the iteration is for one dictionary obj in the overall vlans list?


RE: DevNet Help - ichabod801 - Aug-08-2019

Why would it be written like that? It's written the way it is because that made sense to Guido.

You keep using the work iteration, I don't think you understand what it means. This:

temp['id'] = id
Is assigning to an index. That assigns a single value to a single key in the dictionary. This is plain old indexing (getting a single value out of a dictionary):

id = temp['id']
Iteration is for loops. So this would be iteration:

for key in temp:
    print(key, temp[key])
That gets all the keys out of the dictionary. Generally, you get all the key/value pairs out of the dictionary together with the items method:

for key, value in temp.items():
    print(key, value)