task 2.2 · chapter 2 of 8: strings, lists and dicts · 30 min
A whole show ip interface brief
objective
Turn all of show ip interface brief into a list of dicts, one per interface, by hand with split: skip the header and blank lines, keep administratively down in one piece, and make an unassigned address None.
netlab/parse.pyopen a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter
| The output | SAMPLE in netlab/parse.py: r1’s real show ip interface brief |
|---|---|
| Your function | netlab/parse.py: interfaces_to_dicts() at TODO 2.2 |
| Each dict's keys | interface, ip_address, ok, method, status, protocol |
new to this? step by stepbeginner
python, thenfrom netlab.parse import SAMPLEandprint(SAMPLE).- Split each line with
split()and count the words. Which line has seven? - Challenge: write
interfaces_to_dicts()underTODO 2.2: one dict per interface line. make check T=2.2. If it fails, read theElines: we expected against and got.
more in meet the lab
Columns are a picture, not data
What split() sees
show ip interface brief lines its columns up with spaces so a person can read it. split(), with nothing in
the parentheses, cuts a line wherever there are spaces, however many. That works for every column but one:
Status. An interface that someone has shutdown shows administratively down, which is two words, so that
line splits into seven pieces instead of six.
The way out is to count from both ends. The first four columns (interface, IP address, OK?, method) and the last one (protocol) are always one word each. Whatever is in between is the status.
in one sentence Take the first four words and the last word by position; the status is everything left in the middle.
A loop, a first look
This task needs one thing from lesson 3, a loop: the same lines run once for every item of a list.
for line in text.splitlines():
words = line.split()
...
text.splitlines() is a list of the lines; for line in ... takes each in turn and runs the indented lines
under it. Inside a loop, continue skips to the next line, and list.append(x) adds x to the end of a list.
Lesson 3 covers loops properly.
Two lines that aren’t interfaces
The header (Interface IP-Address ...) isn’t an interface. A blank line isn’t either, and "".split() is an
empty list, [], so asking for its first word fails. Skip both.
No address is None
An interface without an address says unassigned. That’s a word the CLI prints, not an address. In the
dicts, write it as None, Python’s value for nothing, so that later code can ask ip_address is None and
never has to know which word one OS uses.
in one sentence Translate CLI words into Python values once, where you parse. Everything after that works with clean data.
Text in, a list of dicts out
r1’s output, the lab’s SAMPLE:
Interface IP-Address OK? Method Status Protocol
GigabitEthernet0 172.80.111.11 YES DHCP up up
GigabitEthernet1 192.168.10.1 YES manual up up
GigabitEthernet2 10.0.12.1 YES manual up up
GigabitEthernet3 10.0.13.1 YES manual up up
GigabitEthernet4 unassigned YES unset administratively down down
Loopback0 10.255.0.1 YES manual up up
What interfaces_to_dicts(SAMPLE) returns when it’s done: six dicts, one per interface. The first and the fifth:
>>> rows = interfaces_to_dicts(SAMPLE)
>>> len(rows)
6
>>> rows[0]
{'interface': 'GigabitEthernet0', 'ip_address': '172.80.111.11', 'ok': 'YES', 'method': 'DHCP', 'status': 'up', 'protocol': 'up'}
>>> rows[4]
{'interface': 'GigabitEthernet4', 'ip_address': None, 'ok': 'YES', 'method': 'unset', 'status': 'administratively down', 'protocol': 'down'}
And with no output at all, no interfaces:
>>> interfaces_to_dicts("")
[]
The check also makes up outputs of its own, 30 of them, with sub-interfaces, VLAN interfaces, interfaces up with the protocol down, and blank lines in odd places.
Count the words
Start the REPL in the repo’s folder and look at the output as Python sees it:
run in the terminalpython>>> from netlab.parse import SAMPLE >>> lines = SAMPLE.splitlines() >>> len(lines) 7 >>> lines[0].split() ['Interface', 'IP-Address', 'OK?', 'Method', 'Status', 'Protocol']Seven lines: the header and six interfaces. The header splits like any other line, so you’ll need to tell it apart. Its first word is a good clue.
Count the words on every line, with a loop:
>>> for line in lines: ... print(len(line.split()), line.split()[0]) ... 6 Interface 6 GigabitEthernet0 6 GigabitEthernet1 6 GigabitEthernet2 6 GigabitEthernet3 7 GigabitEthernet4 6 Loopback0Type the first line, then the indented one (four spaces), then press Enter on an empty line to run it. GigabitEthernet4 is the one with seven.
Look at GigabitEthernet4’s words, and at what a blank line gives:
>>> gi4 = lines[5].split() >>> gi4[-1] 'down' >>> "".split() [][-1]is the last word, the protocol, whatever the line’s length. An empty list has no first word, so"".split()[0]would be anIndexError: a blank line has to be skipped before you look inside it.
in one sentence Positions from the front and from the back are both fixed; only the middle changes length.
interfaces_to_dicts()
Write interfaces_to_dicts(text) under TODO 2.2, in place of the raise NotImplementedError(...) line. It
returns a list with one dict per interface line, each with the keys interface, ip_address, ok,
method, status and protocol:
- every value is a string, except that an
unassignedaddress isNone; - administratively down is one status, not two;
- the header and blank lines aren’t interfaces;
- an output with no interface lines gives
[].
hint
Start with an empty list, rows = []. Loop over text.splitlines(), split each line, and if the line has no
words, or its first word is "Interface", continue. At the end, return rows.
hint
words[:4] is the first four words, words[-1] the last, and words[4:-1] everything between them. On
most lines that middle is ['up'] or ['down']; on one it’s ['administratively', 'down'].
" ".join(...) turns either back into one string.
hint
If the check says assert 'unassigned' is None, the word went straight into the dict.
None if address == "unassigned" else address gives None for that word and the address for anything else.
hint
ValueError: too many values to unpack (expected 6) means a line like a, b, c, d, e, f = line.split() met
a line with seven words. not enough values to unpack (expected 6, got 0) is the same line meeting a blank
one.
show the solutionnetlab/parse.py
or pull it into your lab: make solve T=2.2, then git diff --staged shows what changed
def interfaces_to_dicts(text):
rows = []
for line in text.splitlines():
words = line.split()
if not words or words[0] == "Interface":
continue
name, address, ok, method = words[:4]
status = " ".join(words[4:-1]) # "up", "down" or "administratively down"
rows.append({
"interface": name,
"ip_address": None if address == "unassigned" else address,
"ok": ok,
"method": method,
"status": status,
"protocol": words[-1],
})
return rows
not words is true for an empty list, so blank lines and lines of only spaces are skipped before anything
looks inside them. The first four words are fixed, so they can be unpacked into four names at once. The
status is everything between them and the protocol, joined back up with a space, so one line of code covers
up, down and administratively down. This is screen-scraping by hand, and it’s fragile on purpose: in
chapter 5 you’ll get the same table from a regular expression and from TextFSM, and compare.
5 check: run it, then prove it
$ make check T=2.2when it's right, you'll see
task 2.2 checks/test_2_2.py
checks/test_2_2.py::test_r1 PASSED [ 33%]
checks/test_2_2.py::test_unseen_outputs PASSED [ 66%]
checks/test_2_2.py::test_no_interfaces PASSED [100%]
============================== 3 passed in 0.01s ===============================
✓ task 2.2 done