New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[doc] Add examples to int.to_bytes and int.from_bytes #60784
Comments
http://docs.python.org/3.3/library/stdtypes.html?highlight=to_bytes#int.to_bytes and http://docs.python.org/3.3/library/stdtypes.html?highlight=to_bytes#int.to_bytes would benefit from an example showing what they do based on simpler coding. I have such an example that I wrote here: http://paddy3118.blogspot.co.uk/2012/11/some-identities-for-python-inttobytes.html that you can use. I.e.
|
Your example is comprehensive but not simple and obvious. |
I agree. The examples in the doc seem clear to me, whereas the ones you proposed are not as clear. Do you think there's something that they don't currently cover that should be added? |
On 06/12/2012 14:31, Ezio Melotti wrote:
First, Thanks Ezio and Andrew for your replies. My problem was that when working on bitcoin address validation I saw I therefore thought that what would have helped me was code that showed If we split my request into two:
I can work on the code a bit more. Thanks, Paddy. P.S. I guess what is currently present shows the result of the methods |
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). 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. |
On 09/12/2012 10:55, Ezio Melotti wrote:
The second example looks great. I like the dual use for testing too and Thanks guys. It's appreciated! |
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 |
I don't call it the examples. Here is the examples: >>> (1234).to_bytes(2, 'big')
b'\x04\xd2'
>>> (1234).to_bytes(2, 'little')
b'\xd2\x04'
>>> (-1234).to_bytes(2, 'big', signed=True)
b'\xfb.'
>>> int.from_bytes(b'\xde\xad\xbe\xef', 'big')
3735928559
>>> int.from_bytes(b'\xde\xad\xbe\xef', 'little')
4022250974
>>> int.from_bytes(b'\xde\xad\xbe\xef', 'big', signed=True)
-559038737 |
The sum(b << 8*i for i, b in enumerate(little_ordered)) instead of sum(little_ordered[i] << i*8 for i in range(len(little_ordered))) Also, in: if signed and little_ordered and (little_ordered[-1] & 0x80): I wondered why you needed the The |
I've rewritten woparry's patch so that it targets the current codebase as well as fixing the issues that Mark mentioned. It appears that the range checks on Should a separate issue be raised for the behaviour of |
Agreed. It was just a note in passing. I don't think it's worth opening a new issue, though, unless this is genuinely causing problems for someone. Just let it lie. |
Merged. Thank you for the contribution! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: