Re: [Python-ideas] String and bytes bitwise operations

2018-06-22 Thread INADA Naoki
Hi Terry, Thanks, but I didn't care because my password is not so long. I just want to illustrate real world bytes xor usage. BTW, New MySQL auth methods (sha256 and caching_sha2) use bytes xor too. For performance point of view, websocket masking is performance critical. Tornado uses extension

Re: [Python-ideas] String and bytes bitwise operations

2018-06-22 Thread Terry Reedy
On 6/22/2018 7:08 AM, INADA Naoki wrote: Bitwise xor is used for "masking" code like these: https://github.com/PyMySQL/PyMySQL/blob/37eba60439039eff17b32ef1a63b45c25ea28cec/pymysql/connections.py#L139-L146 This points to a function _my_crypt that is O(n*n) because of using bytes.append. Usin

Re: [Python-ideas] String and bytes bitwise operations

2018-06-22 Thread INADA Naoki
Bitwise xor is used for "masking" code like these: https://github.com/PyMySQL/PyMySQL/blob/37eba60439039eff17b32ef1a63b45c25ea28cec/pymysql/connections.py#L139-L146 https://github.com/tornadoweb/tornado/blob/0b2b055061eb4754c80a8d6bc28614b86954e336/tornado/util.py#L470-L471 https://github.com/torn

Re: [Python-ideas] String and bytes bitwise operations

2018-05-21 Thread Steven D'Aprano
On Mon, May 21, 2018 at 11:22:17AM -0700, Chris Barker wrote: > On Sat, May 19, 2018 at 6:52 AM, Steven D'Aprano > wrote: > > > Philosophical arguments about the nature of computer memory aside, byte > > objects in Python are collections of ints. > > > > not when you start talking about bit-wise

Re: [Python-ideas] String and bytes bitwise operations

2018-05-21 Thread Chris Barker via Python-ideas
On Sat, May 19, 2018 at 6:52 AM, Steven D'Aprano wrote: > Philosophical arguments about the nature of computer memory aside, byte > objects in Python are collections of ints. > not when you start talking about bit-wise operations :-) If a "byte" in python was an integer, then we'd use b**2 rath

Re: [Python-ideas] String and bytes bitwise operations

2018-05-19 Thread Steven D'Aprano
On Fri, May 18, 2018 at 06:25:19PM -0400, Chris Barker - NOAA Federal via Python-ideas wrote: > > I suppose you could argue that a "byte" is a patch of > > storage capable of holding a number from 0 to 255, as opposed to being > > the number itself, but that's getting rather existential :) > > N

Re: [Python-ideas] String and bytes bitwise operations

2018-05-18 Thread Steve Barnes
On 18/05/2018 07:07, Greg Ewing wrote: > Steven D'Aprano wrote: >> But XORing bytes seems perfectly reasonable. Bytes are numbers, even >> if we display them as ASCII characters. > > Yep. Implement it for bytes, then the user can decode/encode > as appropriate for the application. > Personall

Re: [Python-ideas] String and bytes bitwise operations

2018-05-18 Thread Ken Hilton
On Fri, 18 May 2018 13:49:58 -0300, Facundo Batista wrote: > Once you think as the whole sequence of bytes as a sequence of bits (eight times longer, of course), all questions are easly answered, see below... I had never considered this before, but it seems very interesting. Essentially that would

Re: [Python-ideas] String and bytes bitwise operations

2018-05-18 Thread Chris Angelico
On Sat, May 19, 2018 at 8:25 AM, Chris Barker - NOAA Federal wrote: >> I suppose you could argue that a "byte" is a patch of >> storage capable of holding a number from 0 to 255, as opposed to being >> the number itself, but that's getting rather existential :) > > No, I’m making the distinction t

Re: [Python-ideas] String and bytes bitwise operations

2018-05-18 Thread Chris Barker - NOAA Federal via Python-ideas
>> actually, bytes are, well, bytes ;-) -- that is, 8 bits. > > Grammatically, you appear to be disagreeing with the assertion that > bytes are numbers. Is that the case? Um, yes. Though I think for the rest of the conversation, it’s a distinction that doesn’t matter. > If you want to be extremel

Re: [Python-ideas] String and bytes bitwise operations

2018-05-18 Thread Chris Angelico
On Sat, May 19, 2018 at 2:32 AM, Chris Barker via Python-ideas wrote: > On Thu, May 17, 2018 at 11:07 PM, Greg Ewing > wrote: >> >> Steven D'Aprano wrote: >>> >>> But XORing bytes seems perfectly reasonable. Bytes are numbers, even if >>> we display them as ASCII characters. > > > actually, bytes

Re: [Python-ideas] String and bytes bitwise operations

2018-05-18 Thread Richard Damon
On 5/18/18 12:32 PM, Chris Barker via Python-ideas wrote: > actually, bytes are, well, bytes ;-) -- that is, 8 bits. But the point > is that "bitwise" operations make all the sense in the world for > bytes, but not for unicode text -- did anyone have a use case for > bitwise operation on unicode st

Re: [Python-ideas] String and bytes bitwise operations

2018-05-18 Thread Facundo Batista
2018-05-18 13:32 GMT-03:00 Chris Barker via Python-ideas : > or would it operate on the whole sequence as a single collection of bits? Once you think as the whole sequence of bytes as a sequence of bits (eight times longer, of course), all questions are easly answered, see below... > Would it b

Re: [Python-ideas] String and bytes bitwise operations

2018-05-18 Thread Chris Barker via Python-ideas
On Thu, May 17, 2018 at 11:07 PM, Greg Ewing wrote: > Steven D'Aprano wrote: > >> But XORing bytes seems perfectly reasonable. Bytes are numbers, even if >> we display them as ASCII characters. > > actually, bytes are, well, bytes ;-) -- that is, 8 bits. But the point is that "bitwise" operations

Re: [Python-ideas] String and bytes bitwise operations

2018-05-17 Thread Greg Ewing
Steven D'Aprano wrote: But XORing bytes seems perfectly reasonable. Bytes are numbers, even if we display them as ASCII characters. Yep. Implement it for bytes, then the user can decode/encode as appropriate for the application. -- Greg ___ Python-i

Re: [Python-ideas] String and bytes bitwise operations

2018-05-17 Thread Terry Reedy
On 5/17/2018 6:53 AM, Ken Hilton wrote: Hi all, We all know the bitwise operators: & (and), | (or), ^ (xor), and ~ (not). We know how they work with numbers: 420 ^ 502 110100100 10110 == XOR == 001010010 = 82 But it might be useful in some cases to (let's say) xor a string (or bytestri

Re: [Python-ideas] String and bytes bitwise operations

2018-05-17 Thread Antoine Pitrou
I agree with Steven. XORing unicode strings doesn't make sense, and is pointless anyway. The only interesting question is whether we want to add bytewise operations to the stdlib. Regards Antoine. On Thu, 17 May 2018 23:13:22 +1000 Steven D'Aprano wrote: > On Thu, May 17, 2018 at 03:49:02PM

Re: [Python-ideas] String and bytes bitwise operations

2018-05-17 Thread Ken Hilton
On Thu, 17 May 2018 23:13:22 +1000, Steven D'Aprano wrote: > No, he didn't explain the meaning. He gave an example, but not a reason why it should do what he showed. > > Why should the *abstract character* 'H' XORed with the abstract character 'w' return the abstract character '?'? Why shouldn't t

Re: [Python-ideas] String and bytes bitwise operations

2018-05-17 Thread Antoine Pitrou
Indeed, at this point Numpy sounds like the ideal solution for such use cases. Regards Antoine. On Thu, 17 May 2018 13:06:59 +0200 Stephan Houben wrote: > Seems you want numpy: > > >>> import numpy > >>> numpy.frombuffer(b"Hello", dtype=numpy.uint8) ^ > numpy.frombuffer(b"World", dtype=num

Re: [Python-ideas] String and bytes bitwise operations

2018-05-17 Thread Steven D'Aprano
On Thu, May 17, 2018 at 03:49:02PM +0300, Serhiy Storchaka wrote: > 17.05.18 15:20, Steven D'Aprano пише: > >On Thu, May 17, 2018 at 02:14:10PM +0300, Serhiy Storchaka wrote: > >>17.05.18 13:53, Ken Hilton пише: > >>>But it might be useful in some cases to (let's say) xor a string (or > >>>bytestri

Re: [Python-ideas] String and bytes bitwise operations

2018-05-17 Thread Serhiy Storchaka
17.05.18 15:20, Steven D'Aprano пише: On Thu, May 17, 2018 at 02:14:10PM +0300, Serhiy Storchaka wrote: 17.05.18 13:53, Ken Hilton пише: But it might be useful in some cases to (let's say) xor a string (or bytestring): The question is how common a need of these operations? If it is not common

Re: [Python-ideas] String and bytes bitwise operations

2018-05-17 Thread Steven D'Aprano
On Thu, May 17, 2018 at 02:14:10PM +0300, Serhiy Storchaka wrote: > 17.05.18 13:53, Ken Hilton пише: > >We all know the bitwise operators: & (and), | (or), ^ (xor), and ~ > >(not). We know how they work with numbers: > > > >420 ^ 502 > > > >110100100 > >10110 > >== XOR == > >001010010 > >= 82

Re: [Python-ideas] String and bytes bitwise operations

2018-05-17 Thread Serhiy Storchaka
17.05.18 13:53, Ken Hilton пише: We all know the bitwise operators: & (and), | (or), ^ (xor), and ~ (not). We know how they work with numbers: 420 ^ 502 110100100 10110 == XOR == 001010010 = 82 But it might be useful in some cases to (let's say) xor a string (or bytestring): The questi

Re: [Python-ideas] String and bytes bitwise operations

2018-05-17 Thread Stephan Houben
Seems you want numpy: >>> import numpy >>> numpy.frombuffer(b"Hello", dtype=numpy.uint8) ^ numpy.frombuffer(b"World", dtype=numpy.uint8) array([31, 10, 30, 0, 11], dtype=uint8) Stephan 2018-05-17 12:53 GMT+02:00 Ken Hilton : > Hi all, > > We all know the bitwise operators: & (and), | (or), ^ (

[Python-ideas] String and bytes bitwise operations

2018-05-17 Thread Ken Hilton
Hi all, We all know the bitwise operators: & (and), | (or), ^ (xor), and ~ (not). We know how they work with numbers: 420 ^ 502 110100100 10110 == XOR == 001010010 = 82 But it might be useful in some cases to (let's say) xor a string (or bytestring): HELLO ^ world 01001000 01000101 010011