Issue16580
Created on 2012-11-29 19:25 by paddy3118, last changed 2013-01-13 16:10 by Ramchandra Apte.
| Messages (6) | |||
|---|---|---|---|
| msg176671 - (view) | Author: Paddy McCarthy (paddy3118) | Date: 2012-11-29 19:25 | |
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. >>> n = 2491969579123783355964723219455906992268673266682165637887 >>> length = 25 >>> n2bytesbig = n.to_bytes(length, 'big') >>> n2byteslittle = n.to_bytes(length, 'little') >>> assert n2bytesbig == bytes( (n >> i*8) & 0xff for i in reversed(range(length))) >>> assert n2byteslittle == bytes( (n >> i*8) & 0xff for i in range(length)) >>> assert n == sum( n2bytesbig[::-1][i] << i*8 for i in range(length) ) >>> assert n == sum( n2byteslittle[i] << i*8 for i in range(length) ) >>> assert n == int.from_bytes(n2bytesbig, byteorder='big') >>> assert n == int.from_bytes(n2byteslittle, byteorder='little') >>> |
|||
| msg177042 - (view) | Author: Andrew Svetlov (asvetlov) * ![]() |
Date: 2012-12-06 14:26 | |
Your example is comprehensive but not simple and obvious. I think better to keep it out of doc. |
|||
| msg177043 - (view) | Author: Ezio Melotti (ezio.melotti) * ![]() |
Date: 2012-12-06 14:31 | |
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? |
|||
| msg177207 - (view) | Author: Paddy McCarthy (paddy3118) | Date: 2012-12-09 10:34 | |
On 06/12/2012 14:31, Ezio Melotti wrote: > Ezio Melotti added the comment: > > 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? > > ---------- > nosy: +ezio.melotti > > _______________________________________ > Python tracker <report@bugs.python.org> > <http://bugs.python.org/issue16580> > _______________________________________ > First, Thanks Ezio and Andrew for your replies. My problem was that when working on bitcoin address validation I saw code that was shifting and &'ing with 0xFF to convert to multiple bytes and half remembered that there might be a Python function to do that. On finding the .to_bytes method and its parameter "big" or "little", the only way I had of working out which to use was to try each until I found out which worked. I therefore thought that what would have helped me was code that showed the equivalent "expanded Python" for the method in a similar way to what is done for some of the itertools functions etc. If we split my request into two: 1. Is such extra explanation necessary. 2. Is my specific code that extra explanation. I can work on the code a bit more. Have I persuaded you that an extra explanation is necessary? Thanks, Paddy. P.S. I guess what is currently present shows the result of the methods but nothing on how it could be generated. I am stating that the generation can aid comprehension. |
|||
| msg177208 - (view) | Author: Ezio Melotti (ezio.melotti) * ![]() |
Date: 2012-12-09 10:55 | |
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. |
|||
| msg177262 - (view) | Author: Paddy McCarthy (paddy3118) | Date: 2012-12-10 05:57 | |
On 09/12/2012 10:55, Ezio Melotti wrote: > Ezio Melotti added the comment: > > 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. > > ---------- > keywords: +easy > stage: -> needs patch > versions: +Python 2.7, Python 3.2, Python 3.4 > > _______________________________________ > Python tracker <report@bugs.python.org> > <http://bugs.python.org/issue16580> > _______________________________________ > The second example looks great. I like the dual use for testing too and will try and remember both the next time I find I have ireas about the documentation. Thanks guys. It's appreciated! |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2013-01-13 16:10:59 | Ramchandra Apte | set | title: Add examples to int.to_bytres and int.from_bytes -> Add examples to int.to_bytes and int.from_bytes |
| 2012-12-10 05:57:39 | paddy3118 | set | messages: + msg177262 |
| 2012-12-09 10:55:36 | ezio.melotti | set | keywords:
+ easy stage: needs patch messages: + msg177208 versions: + Python 2.7, Python 3.2, Python 3.4 |
| 2012-12-09 10:34:49 | paddy3118 | set | messages: + msg177207 |
| 2012-12-06 14:31:38 | ezio.melotti | set | nosy:
+ ezio.melotti messages: + msg177043 |
| 2012-12-06 14:26:29 | asvetlov | set | nosy:
+ asvetlov messages: + msg177042 |
| 2012-11-29 19:25:09 | paddy3118 | create | |
