online posts 16
python 101 for network engineers

task 2.1 · chapter 2 of 8: strings, lists and dicts · 30 min

A line of show version

objective

Take the first line of show version apart with string methods, return it as a dict, and put the dict back into a line. On the way, learn to read what make check says when a check fails.

you edit, in the labnetlab/parse.py

open a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter

given
The lineCapeOS Software (CO-BOLT), Version 0.11.2, LAB USE ONLY: the first line of r1’s show version
Your functionsnetlab/parse.py: parse_version_line() at TODO 2.1a, version_line() at TODO 2.1b
Try thempython in the repo’s folder, then from netlab.parse import parse_version_line
new to this? step by stepbeginner
  1. python, then line = "CapeOS Software (CO-BOLT), Version 0.11.2, LAB USE ONLY" and line.split(","). exit().
  2. In netlab/parse.py, replace the raise NotImplementedError under TODO 2.1a with the code in step 4 of the follow-along.
  3. make check T=2.1: three checks pass and one fails. Read the failure: it names the function still to write.
  4. Challenge: write version_line() under TODO 2.1b, then make check T=2.1 again.

more in meet the lab

1 explain

Text in, a dict out

Everything from a device is a string

When code reads a device’s output, it gets text: a string. Before you can ask which version is r1 running?, you have to cut that text into pieces and name them. The first line of show version has three parts, split by commas:

CapeOS Software (CO-BOLT), Version 0.11.2, LAB USE ONLY
└──── software ──┘└image┘  └─ version ──┘  └─ ignored ─┘

The function you write returns the three you need as a dict, a set of named values:

{'software': 'CapeOS Software', 'image': 'CO-BOLT', 'version': '0.11.2'}

in one sentence Parse once, into a dict, and the rest of your code asks for info["version"] instead of counting commas.

Four string methods

MethodWhat it does
split(",")cuts the string at every comma and gives a list of the pieces
strip()removes spaces from both ends
partition("(")cuts once, at the first (, and gives three parts: before, the (, after
removeprefix("Version")removes Version from the front

Each one returns a new string and leaves the old one as it was, so you can chain them: piece.strip().removeprefix("Version").strip().

What a check is

make check T=2.1 runs pytest, the test runner most Python projects use, on the file checks/test_2_1.py. Each function in that file whose name starts with test_ is one check. It imports your function, calls it, and uses assert to say what must be true of the answer. Some checks use the lab’s own line; others make up a fresh show version line on every run (other software names, other versions, nothing after the second comma), so code that only works for 0.11.2 doesn’t pass.

in one sentence The checks are code you can read: checks/test_2_1.py is the spec for this task.

2 show

How to read make check

Every task in this lab ends with make check, so it’s worth knowing how to read it now. There are three kinds of failure. Here’s the first, on the starter code, before you’ve written anything:

task 2.1  checks/test_2_1.py
============================= test session starts ==============================
collecting ... collected 4 items

checks/test_2_1.py::test_the_lab_line FAILED                             [ 25%]
checks/test_2_1.py::test_lines_from_the_lab_devices FAILED               [ 50%]
checks/test_2_1.py::test_unseen_lines FAILED                             [ 75%]
checks/test_2_1.py::test_back_into_a_line FAILED                         [100%]

=================================== FAILURES ===================================
______________________________ test_the_lab_line _______________________________
TODO 2.1a is still open: write parse_version_line() in netlab/parse.py, then check again
...
____________________________ test_back_into_a_line _____________________________
TODO 2.1b is still open: write version_line() in netlab/parse.py, then check again
============================== 4 failed in 0.02s ===============================

✗ not yet: 2.1. Each failure above says what's missing (pytest marks the line with E); the checks themselves are in checks/.

make: *** [Makefile:63: check] Error 1

Read it from the top:

PartWhat it tells you
task 2.1 checks/test_2_1.pywhich task, and the file its checks are in
collected 4 itemspytest found four checks
checks/test_2_1.py::test_the_lab_line FAILEDone line per check: its file, its name, and PASSED or FAILED
[ 25%]how far through the file it is, nothing more
FAILURESone block per failed check, each headed by its name between ____ lines
4 failed in 0.02sthe count
make: *** ... Error 1make passing on that the check failed. Nothing is wrong with make

1. A TODO that’s still open is one plain line, naming the function and the file. Nothing to decode.

2. A wrong answer is longer. This is a parse_version_line() that forgot to remove the word Version:

______________________________ test_unseen_lines _______________________________
checks/test_2_1.py:27: in test_unseen_lines
    assert got == want, f"for the line\n    {line}\nwe expected {want}\nand got      {got}"
E   AssertionError: for the line
E         Example Router Software (EX-K9), Version 18.3.0a
E     we expected {'software': 'Example Router Software', 'image': 'EX-K9', 'version': '18.3.0a'}
E     and got      {'software': 'Example Router Software', 'image': 'EX-K9', 'version': 'Version 18.3.0a'}
E   assert {'software': ...sion 18.3.0a'} == {'software': ...n': '18.3.0a'}
E     
E     Omitting 2 identical items, use -vv to show
E     Differing items:
E     {'version': 'Version 18.3.0a'} != {'version': '18.3.0a'}
E     ...
  • checks/test_2_1.py:27: in test_unseen_lines is where in the check it failed: file, line 27, which check.
  • The next line is that line of the check: the assert that wasn’t true.
  • The lines starting with E are the error. The first few are the check’s own message, written for you: the input, we expected, and and got. Compare those two lines: the only difference is Version .
  • After that, pytest adds its own comparison. In Differing items, your value is on the left and the expected one on the right. Omitting 2 identical items means the other two keys were right.
  • use -vv and Full output truncated are pytest offering more detail. The check’s message above it is usually enough.

Because this check makes up its input, the line in the message is different on every run. That’s on purpose.

3. Your code crashed before it could answer. Then the block is a short traceback that walks from the check into your code, and the last line before the E is the line of yours that failed. From task 2.2, a version that unpacks every line into six names:

___________________________________ test_r1 ____________________________________
checks/test_2_2.py:14: in test_r1
    got = call(interfaces_to_dicts, text)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
checks/conftest.py:20: in call
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
netlab/parse.py:41: in interfaces_to_dicts
    name, address, ok, method, status, protocol = line.split()
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E   ValueError: too many values to unpack (expected 6)

netlab/parse.py:41 is your file and line, and the E line is Python’s error. (conftest.py is the lab’s code that calls your function; you can skip past it.)

in one sentence Find the FAILED lines, then for each block read the E lines: the check’s message first, expected against got.

3 follow along

Take the line apart

  1. Open a terminal. It starts in the repo’s folder, which matters: that’s where Python finds netlab. Start the REPL and try the line:

    run in the terminal
    python
    
    >>> line = "CapeOS Software (CO-BOLT), Version 0.11.2, LAB USE ONLY"
    >>> parts = line.split(",")
    >>> parts
    ['CapeOS Software (CO-BOLT)', ' Version 0.11.2', ' LAB USE ONLY']
    

    Three pieces. The second and third start with a space, the one that followed each comma.

  2. Clean up the version, one method at a time, and watch what each one does:

    >>> parts[1].strip()
    'Version 0.11.2'
    >>> parts[1].strip().removeprefix("Version")
    ' 0.11.2'
    >>> parts[1].strip().removeprefix("Version").strip()
    '0.11.2'
    
  3. And the first piece, which holds the software and the image:

    >>> first = parts[0]
    >>> first.partition("(")
    ('CapeOS Software ', '(', 'CO-BOLT)')
    

    The software has a space on the end, and the image a ). strip() and rstrip(")") (strip from the right only) take care of those. Leave with exit().

  4. Open netlab/parse.py and find TODO 2.1a. Replace the raise NotImplementedError(...) line, and only that line, with:

    editnetlab/parse.pyCtrl+P → parse.py
        first, second = line.split(",")[:2]
        software, _, image = first.partition("(")
        return {
            "software": software.strip(),
            "image": image.strip().rstrip(")"),
            "version": second.strip().removeprefix("Version").strip(),
        }
    

    [:2] keeps the first two pieces, so a line with nothing after the second comma, or with more commas, works too. first, second = ... gives the two a name each. partition gives three parts; the middle one is the (, and _ is the usual name for a value you don’t need. Keep the indentation: four spaces, inside the function.

  5. Save, start a new REPL (it reads netlab once, when you import it, so changes need a fresh start), and try your function on two lines:

    >>> from netlab.parse import parse_version_line, version_line
    >>> parse_version_line("CapeOS Software (CO-BOLT), Version 0.11.2, LAB USE ONLY")
    {'software': 'CapeOS Software', 'image': 'CO-BOLT', 'version': '0.11.2'}
    >>> parse_version_line("Example Router Software (EX-K9), Version 18.3.0a")
    {'software': 'Example Router Software', 'image': 'EX-K9', 'version': '18.3.0a'}
    
  6. Run the check:

    run in the terminal
    make check T=2.1
    
    checks/test_2_1.py::test_the_lab_line PASSED                             [ 25%]
    checks/test_2_1.py::test_lines_from_the_lab_devices PASSED               [ 50%]
    checks/test_2_1.py::test_unseen_lines PASSED                             [ 75%]
    checks/test_2_1.py::test_back_into_a_line FAILED                         [100%]
    
    =================================== FAILURES ===================================
    ____________________________ test_back_into_a_line _____________________________
    TODO 2.1b is still open: write version_line() in netlab/parse.py, then check again
    ========================= 1 failed, 3 passed in 0.10s ==========================
    

    Three passed, including 40 lines your function had never seen. The fourth is the challenge.

in one sentence Try each method in the REPL first, then put the ones that worked into the function.

4 challenge

And back into a line

Write version_line(info), under TODO 2.1b. It takes the dict that parse_version_line() returns and gives back one line: the software, the image and the version, with one space between each.

>>> info = parse_version_line("CapeOS Software (CO-BOLT), Version 0.11.2, LAB USE ONLY")
>>> info["version"]
'0.11.2'
>>> version_line(info)
'CapeOS Software CO-BOLT 0.11.2'
hint

An f-string builds a line from values: f"{a} {b}" puts a, a space, then b. Anything inside the curly braces is worked out first, and that includes looking up a key in a dict.

hint

Inside an f-string written with double quotes, write the dict’s keys with single quotes: f"{info['software']} ...". Two sets of double quotes would end the string early.

hint

test_back_into_a_line makes up 20 dicts and calls your function on each. If it fails, the message says what the line should be made of.

show the solutionnetlab/parse.py

or pull it into your lab: make solve T=2.1, then git diff --staged shows what changed

def version_line(info):
    return f"{info['software']} {info['image']} {info['version']}"

The f-string looks up each key and puts a space between them. A function this short is still worth having: any code that needs to print a version calls it, and the format is decided in one place. An equally good answer is " ".join([info["software"], info["image"], info["version"]]), which glues a list of strings together with a space.

5 check: run it, then prove it

$ make check T=2.1

when it's right, you'll see

task 2.1  checks/test_2_1.py
checks/test_2_1.py::test_the_lab_line PASSED                             [ 25%]
checks/test_2_1.py::test_lines_from_the_lab_devices PASSED               [ 50%]
checks/test_2_1.py::test_unseen_lines PASSED                             [ 75%]
checks/test_2_1.py::test_back_into_a_line PASSED                         [100%]
============================== 4 passed in 0.06s ===============================
✓ task 2.1 done