Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DevNet Help
#1
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.
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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?
Reply
#4
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'].
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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?
Reply
#6
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)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
Thanks everyone for the input. I need to evaluate it and see how it makes sense to me. Give me a few
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020