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


Groups > comp.lang.python > #80880

Re: encode and decode builtins

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'explicitly': 0.05; 'string.': 0.05; 'string': 0.09; 'converted': 0.09; 'properly.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'stating': 0.09; 'string;': 0.09; 'strings.': 0.09; 'url:github': 0.09; 'python': 0.11; 'useful,': 0.14; 'changes': 0.15; 'btw:': 0.16; 'builtins.': 0.16; 'caring': 0.16; 'garrett': 0.16; 'happily': 0.16; 'pythonic': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'str,': 0.16; 'unicode,': 0.16; 'demonstrate': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'library': 0.18; 'written': 0.21; 'input': 0.22; 'this?': 0.23; 'header:User- Agent:1': 0.23; 'byte': 0.24; 'bytes': 0.24; 'propose': 0.24; 'received:comcast.net': 0.24; 'unicode': 0.24; 'looks': 0.24; 'switch': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'am,': 0.29; 'have,': 0.30; "i'm": 0.30; 'code': 0.31; 'getting': 0.31; 'lines': 0.31; 'that.': 0.31; 'with,': 0.31; 'cases': 0.33; 'not.': 0.33; 'something': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'data,': 0.36; 'edge': 0.36; 'representing': 0.36; 'doing': 0.36; 'method': 0.36; 'should': 0.36; 'two': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'little': 0.38; 'expect': 0.39; 'does': 0.39; 'embedded': 0.39; 'extremely': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'is.': 0.60; 'free': 0.61; 'ago,': 0.61; 'excited': 0.61; 'simple': 0.61; 'times': 0.62; 'hear': 0.63; 'myself': 0.63; 'more': 0.64; 'love': 0.65; 'great': 0.65; 'between': 0.67; '20,': 0.68; 'glad': 0.83; 'devices,': 0.84; 'or...': 0.84; 'write:': 0.91; 'this!': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ned Batchelder <ned@nedbatchelder.com>
Subject Re: encode and decode builtins
Date Sun, 16 Nov 2014 08:43:49 -0500
References <CALW7ahxWU_N87oAz0JJoXEiyLO0Erx-YggVmQgggzxST6Ra6XQ@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host c-50-133-228-126.hsd1.ma.comcast.net
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
In-Reply-To <CALW7ahxWU_N87oAz0JJoXEiyLO0Erx-YggVmQgggzxST6Ra6XQ@mail.gmail.com>
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.15885.1416145445.18130.python-list@python.org> (permalink)
Lines 62
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1416145445 news.xs4all.nl 2949 [2001:888:2000:d::a6]:56419
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:80880

Show key headers only | View raw


On 11/16/14 2:39 AM, Garrett Berg wrote:
> I made the switch to python 3 about two months ago, and I have to say I
> love everything about it, /especially/ the change to using only bytes
> and str (no more unicode! or... everything is unicode!) As someone who
> works with embedded devices, it is great to know what data I am working
> with.

I am glad that you are excited about Python 3.  But I'm a little 
surprised to hear your characterization of the changes it brought.  Both 
Python 2 and Python 3 are the same in that they have two types for 
representing strings: one for byte strings, and one for Unicode strings.

The difference is that Python 2 called them str and unicode, with "" 
being a byte string; Python 3 calls them bytes and str, with "" being a 
unicode string.  Also, Python 2 happily converted between them 
implicitly, while Python 3 does not.

>
> However, there are times that I do not care what data I am working with,
> and I find myself writing something like:
>
>     if isinstance(data, bytes): data = data.decode()
>

This goes against a fundamental tenet of both Python 2 and 3: you should 
know what data you have, and deal with it properly.

> This is tedious and breaks the pythonic method of not caring about what
> your input is. If I expect that my input can always be decoded into
> valid data, then why do I have to write this?
>
> Instead, I would like to propose to add *encode* and *decode* as
> builtins. I have written simple code to demonstrate my desire:
>
> https://gist.github.com/cloudformdesign/d8065a32cdd76d1b3230

If you find these functions useful, by all means use them in your code. 
  BTW: looks to me like you have infinite recursion on lines 9 and 20, 
so that must be a simple oversight.

>
> There may be a few edge cases I am missing, which would all the more
> prove my point -- we need a function like this!

You are free to have a function like that.  Getting them added to the 
standard library is extremely unlikely.

>
> Basically, if I expect my data to be a string I can just write:
>
>     data = decode(data)
>
> ​Which would accomplish two goals: explicitly stating what I expect of
> my data, and doing so concisely and cleanly.
>
>
>


-- 
Ned Batchelder, http://nedbatchelder.com

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


Thread

Re: encode and decode builtins Ned Batchelder <ned@nedbatchelder.com> - 2014-11-16 08:43 -0500

csiph-web