0
"""
>>> ip_tuple = process("2\t1.001451000\t192.168.0.24\t\t10.0.0.5\t\t98\t84\t\t\t\t\t\t\t\t1")

"""
import doctest
def process(line):
    list_lines = line.split('\t')
    return (list_lines[2], int(list_lines[7]))

doctest.testmod()

When I run it, I would expect it to pass all tests, but the following output is produced:

e/lab4/Q9# python3 solution.py
**********************************************************************
File "solution.py", line 2, in __main__
Failed example:
    ip_tuple = process("2       1.001451000     192.168.0.24            10.0.0.5                98      84
                                              1")
Exception raised:
    Traceback (most recent call last):
      File "/usr/lib/python3.6/doctest.py", line 1330, in __run
        compileflags, 1), test.globs)
      File "<doctest __main__[0]>", line 1, in <module>
        ip_tuple = process("2       1.001451000     192.168.0.24            10.0.0.5                98      84
                                                  1")
      File "solution.py", line 8, in process
        return (list_lines[2], int(list_lines[7]))
    IndexError: list index out of range
**********************************************************************
1 items had failures:
   1 of   1 in __main__
***Test Failed*** 1 failures.

Strangely when I get rid of the docstring and run the same code with the test, producing the following file:

def process(line):
    list_lines = line.split('\t')
    return (list_lines[2], int(list_lines[7]))

ip_tuple = process("2\t1.001451000\t192.168.0.24\t\t10.0.0.5\t\t98\t84\t\t\t\t\t\t\t\t1")
print(ip_tuple)

No error is produced and the following output is printed:

('192.168.0.24', 84)

Please explain why this is so.

0

1 Answer 1

1

docstring is provided with the raw string, so all the \t are treated as the escaped \\t, so in fact, split('\t') doesn't really work as you expected.

This can also be confirmed by adding debug prints in the docstring:

"""
>>> test_string = "2\t1.001451000\t192.168.0.24\t\t10.0.0.5\t\t98\t84\t\t\t\t\t\t\t\t1"
>>> print(test_string.split('\t'))
"""

Outputs

['2', ' 1.001451000     192.168.0.24', '     10.0.0.5', '', '  98      84', '', '', '', '', '', '', '', '      1']

Escaping the \t in the docstring works:

import doctest

def process(line):
    """
    >>> ip_tuple = process("2\\t1.001451000\\t192.168.0.24\\t\\t10.0.0.5\\t\\t98\\t84\\t\\t\\t\\t\\t\\t\\t\\t1")
    """
    list_lines = line.split('\t')
    return (list_lines[2], int(list_lines[7]))


doctest.testmod()

passes with no errors.

EDIT

Making the entire docstring a raw string also fixes the problem.

import doctest

def process(line):
#   V note the r for raw string
    r"""
    >>> ip_tuple = process("2\t1.001451000\t192.168.0.24\t\t10.0.0.5\t\t98\t84\t\t\t\t\t\t\t\t1")
    """
    list_lines = line.split('\t')
    return (list_lines[2], int(list_lines[7]))

doctest.testmod()

passes with no errors.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.