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


Groups > comp.lang.python > #64156

Re: doctests compatibility for python 2 & python 3

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '"""': 0.07; 'versions.': 0.07; 'string': 0.09; 'subject:skip:c 10': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'ascii,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'nameerror:': 0.16; 'non-ascii': 0.16; 'repr': 0.16; 'throw': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'do.': 0.18; 'not,': 0.20; '>>>': 0.22; 'appears': 0.22; 'cc:addr:python.org': 0.22; 'helper': 0.24; 'unicode': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'this:': 0.26; 'header:In-Reply- To:1': 0.27; 'chris': 0.29; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; 'though.': 0.31; 'class': 0.32; 'probably': 0.32; 'text': 0.33; 'fri,': 0.33; 'skip:_ 10': 0.34; 'except': 0.35; 'skip:u 20': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'being': 0.38; 'pm,': 0.38; 'little': 0.38; 'does': 0.39; 'subject: & ': 0.68; 'actually,': 0.84; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=fZce4MbCV1Md3Tz34EOdD9s/su+2fVOmRzD7a3nO+b8=; b=EMHis7Vie2dOUSwYIvow6O8Jt2nCiJbXPbi8uxNQq+/LkGBFo0NJZQ4jod8pZs3ypo +ima79SBLxoeh323xPrJkr4WNxUSCQ3d3Rd9msylOHIkY7t+5fCIfRPzn81dRhh7sHxL paxL4aVWCXthHZYnYQ9reb4+ChJMrOmP2xogO41gJSV77cCm2uaY4niCmHHee1134y0W Xbj4ReZZ5UpiEy2EwFSo6il2RYmdgKHDq1feaTa8Es87RTfiPA88M3nlA2OAThMK6aTP AX415OLc63dmx4Z10oYr5+uolHVX1NQ2sSXAcFgQ3ZsUU2Y4RLwtg1BngxgmkEJH8FVg zO9g==
MIME-Version 1.0
X-Received by 10.68.98.3 with SMTP id ee3mr1492423pbb.31.1389958246618; Fri, 17 Jan 2014 03:30:46 -0800 (PST)
In-Reply-To <CAPTjJmqhr9V42ncZHKKGnE=N8KuUmV2t6M3eNYfZzkPNTYQF9A@mail.gmail.com>
References <52D91101.7000202@chamonix.reportlab.co.uk> <CAPTjJmqhr9V42ncZHKKGnE=N8KuUmV2t6M3eNYfZzkPNTYQF9A@mail.gmail.com>
Date Fri, 17 Jan 2014 22:30:46 +1100
Subject Re: doctests compatibility for python 2 & python 3
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5636.1389958255.18130.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1389958255 news.xs4all.nl 2912 [2001:888:2000:d::a6]:53667
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64156

Show key headers only | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web