Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctest Involving Escape Characters

Tags:

python

doctest

Have a function fix(), as a helper function to an output function which writes strings to a text file.

def fix(line):
    """
    returns the corrected line, with all apostrophes prefixed by an escape character

    >>> fix('DOUG\'S')
    'DOUG\\\'S'

    """
    if '\'' in line:
        return line.replace('\'', '\\\'')
    return line

Turning on doctests, I get the following error:

Failed example:
    fix('DOUG'S')
Exception raised:
    Traceback (most recent call last):
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/doctest.py", line 1254, in __run
        compileflags, 1) in test.globs
      File "<doctest convert.fix[0]>", line 1
        fix('DOUG'S')
                  ^

No matter what combination of \ and 's I use, the doctest doesn't seem to to want to work, even though the function itself works perfectly. Have a suspicion that it is a result of the doctest being in a block comment, but any tips to resolve this.

like image 783
zhuyxn Avatar asked Aug 01 '12 18:08

zhuyxn


People also ask

Can doctest be used to test Docstrings?

There are several common ways to use doctest: To check that a module's docstrings are up-to-date by verifying that all interactive examples still work as documented. To perform regression testing by verifying that interactive examples from a test file or a test object work as expected.

Can we handle unpredictable output using doctest?

When the tests include values that are likely to change in unpredictable ways, and where the actual value is not important to the test results, you can use the ELLIPSIS option to tell doctest to ignore portions of the verification value.


2 Answers

Is this what you want?:

def fix(line):
    r"""
    returns the corrected line, with all apostrophes prefixed by an escape character

    >>> fix("DOUG\'S")
    "DOUG\\'S"
    >>> fix("DOUG'S") == r"DOUG\'S"
    True
    >>> fix("DOUG'S")
    "DOUG\\'S"

    """
    return line.replace("'", r"\'")

import doctest
doctest.testmod()

raw strings are your friend...

like image 130
mgilson Avatar answered Sep 28 '22 22:09

mgilson


First, this is what happens if you actually call your function in the interactive interpreter:

>>> fix("Doug's")
"Doug\\'s"

Note that you don't need to escape single quotes in double-quoted strings, and that Python does not do this in the representation of the resulting string – only the back slash gets escaped.

This means the correct docstring should be (untested!)

"""
returns the corrected line, with all apostrophes prefixed by an escape character

>>> fix("DOUG'S")
"DOUG\\\\'S"

"""

I'd use a raw string literal for this docstring to make this more readable:

r"""
returns the corrected line, with all apostrophes prefixed by an escape character

>>> fix("DOUG'S")
"DOUG\\'S"

"""
like image 45
Sven Marnach Avatar answered Sep 29 '22 00:09

Sven Marnach