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


Groups > comp.lang.python > #80859

encode and decode builtins

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <googberg@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.075
X-Spam-Evidence '*H*': 0.85; '*S*': 0.00; 'explicitly': 0.05; 'string': 0.09; 'stating': 0.09; 'url:github': 0.09; 'python': 0.11; 'builtins.': 0.16; 'caring': 0.16; 'pythonic': 0.16; 'demonstrate': 0.16; 'written': 0.21; 'input': 0.22; 'this?': 0.23; 'bytes': 0.24; 'propose': 0.24; 'switch': 0.26; 'point': 0.28; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'with,': 0.31; 'cases': 0.33; 'something': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'data,': 0.36; 'edge': 0.36; 'doing': 0.36; 'method': 0.36; 'two': 0.37; 'to:addr:python-list': 0.38; 'expect': 0.39; 'embedded': 0.39; 'to:addr:python.org': 0.39; 'is.': 0.60; 'ago,': 0.61; 'simple': 0.61; 'times': 0.62; 'myself': 0.63; 'more': 0.64; 'love': 0.65; 'great': 0.65; 'devices,': 0.84; 'or...': 0.84; 'write:': 0.91; 'this!': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=rDvxB8drCe4vbTBMeGL5HxOlHEh9e+HyuEqb0feqxg8=; b=m2Q6bHnUHIIc+8K7tfmAhNbHHf3v7ufmhZb0MCpr4X0tMBSUXKz0bqsIaueaSMBfka 3ELDU4B5XHHK9eI6I7ECeluqNZu7z7Fzjz37dbJ8l2thE48CU+iFQaAIAb8dnV5XxvYo vOXWnem8+BxyGoNXkIKcBRsAvTUWRFgB3b+JfYvMAWQgTHCzp1vCRaoLuTBHoevJFkcI jiJLfqjCQErD/dXpXjFw30weaRf5+XPn7Ofmg/RYnLMXs+naI8RKueDVM9Y6fGCM+bQ6 LnQHTNoOsf01cL/onEoD9XO8NCEmNNGsqmPt9RD4kyx46um7qOwvgVgXmEzr4uFyy080 kT/A==
X-Received by 10.112.201.6 with SMTP id jw6mr10247024lbc.30.1416123610841; Sat, 15 Nov 2014 23:40:10 -0800 (PST)
MIME-Version 1.0
From Garrett Berg <googberg@gmail.com>
Date Sun, 16 Nov 2014 00:39:50 -0700
Subject encode and decode builtins
To python-list@python.org
Content-Type multipart/alternative; boundary=001a11c372b0c848f90507f4f836
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.15872.1416123614.18130.python-list@python.org> (permalink)
Lines 91
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1416123614 news.xs4all.nl 2897 [2001:888:2000:d::a6]:47708
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:80859

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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.

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 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

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

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.

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


Thread

encode and decode builtins Garrett Berg <googberg@gmail.com> - 2014-11-16 00:39 -0700

csiph-web