classification
Title: Make bytes and bytearray maketrans accept dictionaries as first argument as it's done in str
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: oleksandr.suvorov, serhiy.storchaka, terry.reedy
Priority: normal Keywords:

Created on 2017-09-26 16:04 by oleksandr.suvorov, last changed 2017-09-30 00:16 by terry.reedy.

Messages (6)
msg303048 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-26 16:12
Could you please provide more information? What the problem you want to solve?
msg303049 - (view) Author: Oleksandr Suvorov (oleksandr.suvorov) Date: 2017-09-26 16:25
str.maketrans in python3 is able to accept just one argument which is a
mapping of translations.
While bytearray and bytes are still using only old way where you should
pass two iterables from and to.

static str.maketrans(x [,y [,z]])
static bytes.maketrans(from, to)
static bytearray.maketrans(from, to)

On Tue, Sep 26, 2017 at 6:12 PM, Serhiy Storchaka <report@bugs.python.org>
wrote:

>
> New submission from Serhiy Storchaka:
>
> Could you please provide more information? What the problem you want to
> solve?
>
> ----------
> nosy: +serhiy.storchaka
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue31594>
> _______________________________________
>
msg303051 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-26 16:40
Why do you need this functionality?
msg303053 - (view) Author: Oleksandr Suvorov (oleksandr.suvorov) Date: 2017-09-26 16:46
This change requested just because I find it inconsistent and passing
translation as key-value is much easier to read rather than two lists.
When translation table gets bigger than 10 pairs it gets annoying to match
in the head between from and to while reading it.
Could be marked as minor.
----------_________________________

> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue31594>
> _______________________________________
>
msg303056 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-26 17:17
Inconsistency is a weak argument. It itself is not enough to add a new feature. str.translate() accepts a mapping instead of a string (as it would be for consistency with bytes.translate()) because it would be inconvenient to provide a 1114112-character string. str.maketrans() just compile a mapping in the optimized representation, because using a general mapping is not very efficient.

A bytes object of length 256 already is the most efficient representation of the translation table for bytes. And you can create it from a mapping in Python:

def mymaketrans(mapping):
    table = bytearray(range(256))
    for x, y in mapping.items():
        table[x] = y
    return table
msg303377 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2017-09-30 00:16
Consistency between bytes and strings is not much of a concern here, or the change would have been make for bytes when it was made for strings.  I would not approve the request without knowing who chose not to and why.

I think an example like
t = bytes.maketrans(
    b'ABCDEVGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
    b'vcccvcccvcccccvcccccvcscscvcccvcccvcccccvcccccvcscsc')
is *much* better as it is than as a dict.
History
Date User Action Args
2017-09-30 00:16:03terry.reedysetnosy: + terry.reedy
messages: + msg303377
2017-09-26 17:17:57serhiy.storchakasetmessages: + msg303056
2017-09-26 16:46:44oleksandr.suvorovsetmessages: + msg303053
2017-09-26 16:40:46serhiy.storchakasetmessages: + msg303051
2017-09-26 16:25:33oleksandr.suvorovsetmessages: + msg303049
2017-09-26 16:12:50serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg303048
2017-09-26 16:04:49oleksandr.suvorovcreate