Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64156 > unrolled thread

Re: doctests compatibility for python 2 & python 3

Started byChris Angelico <rosuav@gmail.com>
First post2014-01-17 22:30 +1100
Last post2014-01-17 22:30 +1100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: doctests compatibility for python 2 & python 3 Chris Angelico <rosuav@gmail.com> - 2014-01-17 22:30 +1100

#64156 — Re: doctests compatibility for python 2 & python 3

FromChris Angelico <rosuav@gmail.com>
Date2014-01-17 22:30 +1100
SubjectRe: doctests compatibility for python 2 & python 3
Message-ID<mailman.5636.1389958255.18130.python-list@python.org>
On Fri, Jan 17, 2014 at 10:24 PM, Chris Angelico <rosuav@gmail.com> wrote:
> Do your test strings contain any non-ASCII characters? If not, you
> might be able to do this:
>
> def func(a):
>     """
>     >>> str(func(u'aaa'))
>     'aaa'
>     """
>     return a

Actually, probably better than that:

def func(a):
    """
    >>> text(func(u'aaa'))
    'aaa'
    """
    return a

try:
    class text(unicode): # Will throw NameError in Py3
        def __repr__(self):
            return unicode.__repr__(self)[1:]
except NameError:
    # Python 3 doesn't need this wrapper.
    text = str

Little helper class that does what I don't think monkey-patching will
do. I've tested this and it appears to work in both 2.7.4 and 3.4.0b2,
but that doesn't necessarily mean that the repr of any given Unicode
string will be exactly the same on both versions. It's likely to be
better than depending on the strings being ASCII, though.

ChrisA

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web