This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ezio.melotti
Recipients asvetlov, docs@python, ezio.melotti, paddy3118
Date 2012-12-09.10:55:35
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1355050536.14.0.511880853298.issue16580@psf.upfronthosting.co.za>
In-reply-to
Content
Usually we add plain Python equivalents when they are simple enough that the code equivalent is as understandable as the prose or more (see for example http://docs.python.org/3/library/functions.html#all, or the itertools functions you mentioned).
For this case I think it would help if you presented an equivalent function, e.g.:

def to_bytes(n, length, order):
    if order == 'little':
        return bytes((n >> i*8) & 0xff for i in range(length))
    elif order == 'big':
        return bytes((n >> i*8) & 0xff for i in reversed(range(length)))

or even:

def to_bytes(n, length, order):
    indexes = range(length) if order == 'little' else reversed(range(length))
    return bytes((n >> i*8) & 0xff for i in indexes)

This is also done for http://docs.python.org/3.3/library/stdtypes.html#int.bit_length just above to/from_bytes, so it might be a good addition.
If this is done, the equivalent function can also be added to the test suite, so we can verify that it's indeed equivalent.
History
Date User Action Args
2012-12-09 10:55:36ezio.melottisetrecipients: + ezio.melotti, paddy3118, asvetlov, docs@python
2012-12-09 10:55:36ezio.melottisetmessageid: <1355050536.14.0.511880853298.issue16580@psf.upfronthosting.co.za>
2012-12-09 10:55:36ezio.melottilinkissue16580 messages
2012-12-09 10:55:35ezio.melotticreate