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


Groups > comp.lang.python > #33131

Re: Printing characters outside of the ASCII range

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'none:': 0.05; 'none):': 0.07; 'python': 0.09; 'bytes)': 0.09; 'encode': 0.09; 'non- string': 0.09; 'operator,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'self.data': 0.09; 'subject:characters': 0.09; 'typeerror:': 0.09; 'url:unicode': 0.09; 'def': 0.10; ':-)': 0.13; 'encoding': 0.15; 'emanuele': 0.16; 'pytest': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'stuff.': 0.16; 'string': 0.17; 'comparing': 0.17; 'string,': 0.17; 'trying': 0.21; 'import': 0.21; '"",': 0.22; '2.x': 0.22; "haven't": 0.23; 'header:User-Agent:1': 0.26; '(most': 0.27; 'plain': 0.27; 'correct': 0.28; 'header:X-Complaints-To:1': 0.28; 'skip:( 20': 0.28; '>>>>': 0.29; 'writes:': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'returned': 0.30; '(and': 0.32; 'file': 0.32; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'there': 0.35; 'received:org': 0.36; 'subject:': 0.36; 'but': 0.36; 'method': 0.36; 'two': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'notice': 0.39; 'called': 0.39; 'header:Received:5': 0.40; 'received:79': 0.61; 'kind': 0.61; 'between': 0.63; '8bit%:43': 0.65; 'surprise': 0.65; 'skip:\xe2 10': 0.66; 'quando': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Lele Gaifax <lele@metapensiero.it>
Subject Re: Printing characters outside of the ASCII range
Date Sun, 11 Nov 2012 18:09:31 +0100
Organization Nautilus Entertainments
References <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> <mailman.3508.1352483285.27098.python-list@python.org> <99d5bd83-35ab-4801-b953-391c497c35bf@googlegroups.com> <mailman.3517.1352496859.27098.python-list@python.org> <ba72452f-fac6-4b18-9b22-d1854eecbffb@googlegroups.com> <mailman.3521.1352499071.27098.python-list@python.org> <90c86fc7-a462-4a19-b883-17c64244c806@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host host183-110-dynamic.10-79-r.retail.telecomitalia.it
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.2.50 (gnu/linux)
Cancel-Lock sha1:5gj0vijQg4d+qgDQDTInxV7Kbek=
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3560.1352653786.27098.python-list@python.org> (permalink)
Lines 46
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352653786 news.xs4all.nl 6949 [2001:888:2000:d::a6]:55124
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33131

Show key headers only | View raw


danielk <danielkleinad@gmail.com> writes:

> Ian's solution gives me what I need (thanks Ian!). But I notice a
> difference between '__str__' and '__repr__'.
>
> class Pytest(str):
>     def __init__(self, data = None):
>         if data == None: data = ""
>         self.data = data
>
>     def __repr__(self):
>         return (self.data).encode('cp437')
>

The correct way of comparing with None (and in general with
“singletons”) is with the “is” operator, not with “==”.

> If I change '__repr__' to '__str__' then I get:
>
>>>> import pytest
>>>> p = pytest.Pytest("abc" + chr(178) + "def")
>>>> print(p)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: __str__ returned non-string (type bytes)

In Python 3.3 there is one kind of string, the one that under Python 2.x
was called “unicode”. When you encode such a string with a specific
encoding you obtain a plain “bytes array”. No surprise that the
__str__() method complains, it's called like that for a reason :)

> I'm trying to get my head around all this codecs/unicode stuff. I
> haven't had to deal with it until now but I'm determined to not let it
> get the best of me :-)

Two good readings on the subject:

- http://nedbatchelder.com/text/unipain.html
- http://www.joelonsoftware.com/articles/Unicode.html

ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
lele@metapensiero.it  |                 -- Fortunato Depero, 1929.

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


Thread

Printing characters outside of the ASCII range danielk <danielkleinad@gmail.com> - 2012-11-09 09:17 -0800
  Re: Printing characters outside of the ASCII range Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-09 10:34 -0700
  Re: Printing characters outside of the ASCII range Andrew Berg <bahamutzero8825@gmail.com> - 2012-11-09 11:39 -0600
  Re: Printing characters outside of the ASCII range Dave Angel <d@davea.name> - 2012-11-09 12:47 -0500
    Re: Printing characters outside of the ASCII range danielk <danielkleinad@gmail.com> - 2012-11-09 13:17 -0800
      RE: Printing characters outside of the ASCII range "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-11-09 21:34 +0000
        Re: Printing characters outside of the ASCII range danielk <danielkleinad@gmail.com> - 2012-11-09 13:46 -0800
          Re: Printing characters outside of the ASCII range Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-09 15:10 -0700
            Re: Printing characters outside of the ASCII range danielk <danielkleinad@gmail.com> - 2012-11-11 05:42 -0800
              Re: Printing characters outside of the ASCII range Lele Gaifax <lele@metapensiero.it> - 2012-11-11 18:09 +0100
            Re: Printing characters outside of the ASCII range danielk <danielkleinad@gmail.com> - 2012-11-11 05:42 -0800
        Re: Printing characters outside of the ASCII range danielk <danielkleinad@gmail.com> - 2012-11-09 13:46 -0800
      Re: Printing characters outside of the ASCII range Andrew Berg <bahamutzero8825@gmail.com> - 2012-11-09 15:39 -0600
    Re: Printing characters outside of the ASCII range danielk <danielkleinad@gmail.com> - 2012-11-09 13:17 -0800
  Re: Printing characters outside of the ASCII range wxjmfauth@gmail.com - 2012-11-10 02:09 -0800
  Re: Printing characters outside of the ASCII range Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-11-11 15:40 +0100

csiph-web