[docs] [issue16580] Add examples to int.to_bytes and int.from_bytes

W. Owen Parry report at bugs.python.org
Sat May 25 23:05:07 CEST 2013


W. Owen Parry added the comment:

Patch adding examples + tests for equivalence. Comments appreciated.

In particular, I'm not sure that the from_bytes example is simple enough to be useful:

def from_bytes(bytes, byteorder, signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
     n = sum(little_ordered[i] << i*8 for i in range(len(little_ordered)))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
         n -= 1 << 8*len(little_ordered)
    return n

----------
keywords: +patch
nosy: +woparry
Added file: http://bugs.python.org/file30372/issue16580.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16580>
_______________________________________


More information about the docs mailing list