[issue31656] Bitwise operations for bytes-type

2018-05-17 Thread Nick Coghlan

Nick Coghlan  added the comment:

Issue 19251 is still under consideration, so marking this as a duplicate, 
rather than as rejected.

--
nosy: +ncoghlan
resolution: rejected -> duplicate
superseder:  -> bitwise ops for bytes of equal length

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue32974] Add bitwise operations and other missing comparison methods to Python's IP address module

2018-02-28 Thread Ned Deily

Change by Ned Deily :


--
nosy: +pmoody

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue32974] Add bitwise operations and other missing comparison methods to Python's IP address module

2018-02-28 Thread bbayles

Change by bbayles :


--
nosy: +bbayles

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue32974] Add bitwise operations and other missing comparison methods to Python's IP address module

2018-02-28 Thread Kyle Agronick

New submission from Kyle Agronick <agron...@gmail.com>:

I've recently had the experience of implementing a system for managing DNS 
records at a Fortune 10 company with ~10,000 stores. There were a number of 
things that I felt were missing from the IP address module and could be added 
without introducing any breaking changes.

The easiest changes I saw would be to implement bitwise operations without 
casting all the addresses to ints and then back to IP addresses.

Lets say you wanted to take the last octet from one address and put it on 
another:

Right now you need to do:
IPv6Address(int(v6('2001:db8:85a3:0:0:8a2e:370:7334')) & 
int(IPv6Address('0:0:0:0:0:0:0:')) | 
int(IPv6Address('1323:cf3:23df:0:0:32:44:0')))
# returns IPv6Address('1323:cf3:23df::32:44:7334')

You should be able to do:
(IPv6Address('2001:db8:85a3:0:0:8a2e:370:7334') & 
IPv6Address('0:0:0:0:0:0:0:')) | IPv6Address('1323:cf3:23df:0:0:32:44:0')
# returns TypeError: unsupported operand type(s) for &: 'IPv6Address' and 
'IPv6Address'

All that would be required is to do the casting to int automatically.

The other thing I saw that I would like would be to override the methods that 
return generators with iterables that provide more comparison operations.

Right now you can check if an IP is in a network with:
IPv4Address('192.168.1.12') in IPv4Network('192.168.1.0/24') 
# returns True

You should be able to do this with methods that return multiple networks.
ipaddress.summarize_address_range() is a method that returns a generator of 
IPv(4|6)Networks.

To see if an IP address is in one of multiple networks you need to do:
any(map(lambda i: IPv4Address('192.168.1.12') in i, 
ipaddress.summarize_address_range(IPv4Address('192.168.1.0'), 
IPv4Address('192.168.2.255'
# returns True

You should be able to do:
IPv4Address('192.168.1.12') in 
ipaddress.summarize_address_range(IPv4Address('192.168.1.0'), 
IPv4Address('192.168.2.255'))
# returns False

This should be the default for IPv(4|6)Addresses. IPv(4|6)Networks should check 
membership like they currently do.

You should be able to subtract ranges to make ranges that include some IPs but 
not others:
ipaddress.summarize_address_range(IPv4Address('192.168.1.0'), 
IPv4Address('192.168.2.255')) - 
ipaddress.summarize_address_range(IPv4Address('192.168.1.20'), 
IPv4Address('192.168.1.50'))
# returns TypeError: unsupported operand type(s) for -: 'generator' and 
'generator'

This should return a iterable that has networks that include 192.168.1.0 to 
192.168.1.20 and 192.168.1.50 to 192.168.2.255.

You should be able to do addition as well without casting to a list.

Methods like .hosts() should be able to be performed on the iterator instead of 
doing:
sum([list(i.hosts()) for i in 
ipaddress.summarize_address_range(IPv4Address('192.168.1.0'), 
IPv4Address('192.168.2.255'))], [])

do:
ipaddress.summarize_address_range(IPv4Address('192.168.1.0'), 
IPv4Address('192.168.2.255')).hosts()

Another great feature would be to allow division on networks and network 
generators giving you every xth host:
(i for i in IPv4Network('192.168.1.0/24').hosts() if int(i) % x == 0)

When x is 32 you get:
IPv4Address('192.168.1.32'), 
IPv4Address('192.168.1.64'), 
IPv4Address('192.168.1.96'), 
IPv4Address('192.168.1.128'), 
IPv4Address('192.168.1.160'), 
IPv4Address('192.168.1.192'), 
IPv4Address('192.168.1.224')

I would be happy to code this if the consensus was that this would be included.

--
components: Library (Lib)
messages: 313077
nosy: Kyle Agronick
priority: normal
severity: normal
status: open
title: Add bitwise operations and other missing comparison methods to Python's 
IP address module
type: enhancement
versions: Python 3.8

___
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue32974>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue31656] Bitwise operations for bytes-type

2017-10-01 Thread Martin Panter

Martin Panter  added the comment:

There’s already a bug open for this: Issue 19251. Only equal-length strings 
should be supported.

--
nosy: +martin.panter

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue31656] Bitwise operations for bytes-type

2017-10-01 Thread R. David Murray

R. David Murray  added the comment:

What happens when you apply a bitwise operation to two bytes objects of unequal 
length?

Since the answer to that question is not obvious, we simply don't support the 
operation.

If you want to make a proposal for this the python-ideas mailing list would be 
the appropriate forum.

--
nosy: +r.david.murray
resolution:  -> rejected
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue31656] Bitwise operations for bytes-type

2017-10-01 Thread Matthias Gilch

New submission from Matthias Gilch <matthias.gilch.mg...@gmail.com>:

I've seen that the bytes type does not support bitwise operations, but I think 
users would expect that those operations will work.

--
components: Interpreter Core
messages: 303464
nosy: MGilch
priority: normal
severity: normal
status: open
title: Bitwise operations for bytes-type
type: enhancement

___
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue31656>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue26548] Probably missing word in a sentence in the doc of bitwise operations on integer types

2016-03-12 Thread Raymond Hettinger

Changes by Raymond Hettinger :


--
resolution:  -> fixed
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue26548] Probably missing word in a sentence in the doc of bitwise operations on integer types

2016-03-12 Thread Roundup Robot

Roundup Robot added the comment:

New changeset b9256d5f1ab4 by Raymond Hettinger in branch '3.5':
Issue #26548:  Minor fix to awkward wording in docs
https://hg.python.org/cpython/rev/b9256d5f1ab4

--
nosy: +python-dev

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue26548] Probably missing word in a sentence in the doc of bitwise operations on integer types

2016-03-12 Thread Raymond Hettinger

Changes by Raymond Hettinger :


--
assignee: docs@python -> rhettinger
nosy: +rhettinger
priority: normal -> low

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue26548] Probably missing word in a sentence in the doc of bitwise operations on integer types

2016-03-12 Thread Julien

New submission from Julien:

In 
https://docs.python.org/3.5/library/stdtypes.html#bitwise-operations-on-integer-types
 the sentence "this assumes a sufficiently large number of bits that no 
overflow occurs during the operation" looks wrong to me, so I asked on #python 
and got from @benzrf:

"this assumes that there are sufficiently many bits for no overflow to occur 
during the operation"

Which looks better to me.

--
assignee: docs@python
components: Documentation
messages: 261667
nosy: docs@python, sizeof
priority: normal
severity: normal
status: open
title: Probably missing word in a sentence in the doc of bitwise operations on 
integer types
type: enhancement

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue26548>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: Bitwise Operations

2013-07-30 Thread Ulrich Eckhardt

Am 30.07.2013 01:34, schrieb Devyn Collier Johnson:

Typing 101  010 or x = (int(101, 2)  int(010, 2)) only gives errors.


What errors? Check out Eric Raymond's essay on asking smart questions, 
it's a real eye-opener! ;)


That said, use 0b as prefix for binary number literals (0b1000 is 
eight, for example).


Cheers!

Uli


--
http://mail.python.org/mailman/listinfo/python-list


Re: Bitwise Operations

2013-07-29 Thread Grant Edwards
On 2013-07-29, Devyn Collier Johnson devyncjohn...@gmail.com wrote:

 On Python3, how can I perform bitwise operations? For instance, I want 
 something that will 'and', 'or', and 'xor' a binary integer.

http://www.google.com/search?q=python+bitwise+operations

-- 
Grant Edwards   grant.b.edwardsYow! I have the power to
  at   HALT PRODUCTION on all
  gmail.comTEENAGE SEX COMEDIES!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Bitwise Operations

2013-07-29 Thread Devyn Collier Johnson
On Python3, how can I perform bitwise operations? For instance, I want 
something that will 'and', 'or', and 'xor' a binary integer.


Mahalo,

devyncjohn...@gmail.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Bitwise Operations

2013-07-29 Thread Devyn Collier Johnson


On 07/29/2013 05:53 PM, Grant Edwards wrote:

On 2013-07-29, Devyn Collier Johnson devyncjohn...@gmail.com wrote:


On Python3, how can I perform bitwise operations? For instance, I want
something that will 'and', 'or', and 'xor' a binary integer.

http://www.google.com/search?q=python+bitwise+operations

I understand the symbols. I want to know how to perform the task in a 
script or terminal. I have searched Google, but I never saw a command. 
Typing 101  010 or x = (int(101, 2)  int(010, 2)) only gives errors.


Mahalo,

DCJ
--
http://mail.python.org/mailman/listinfo/python-list


Re: Bitwise Operations

2013-07-29 Thread Ethan Furman

On 07/29/2013 04:34 PM, Devyn Collier Johnson wrote:


On 07/29/2013 05:53 PM, Grant Edwards wrote:

On 2013-07-29, Devyn Collier Johnson devyncjohn...@gmail.com wrote:


On Python3, how can I perform bitwise operations? For instance, I want
something that will 'and', 'or', and 'xor' a binary integer.

http://www.google.com/search?q=python+bitwise+operations


I understand the symbols. I want to know how to perform the task in a script or 
terminal. I have searched Google, but I
never saw a command. Typing 101  010 or x = (int(101, 2)  int(010, 2)) 
only gives errors.


x = (int('101', 2)  int('010', 2))

Notice the quotes.

In the future you'll better answers quicker if you tell us what you did (such 
as your example above) as well as the errors.

--
~Ethan~
--
http://mail.python.org/mailman/listinfo/python-list


Re: Bitwise Operations

2013-07-29 Thread Chris Angelico
On Tue, Jul 30, 2013 at 12:34 AM, Devyn Collier Johnson
devyncjohn...@gmail.com wrote:

 I understand the symbols. I want to know how to perform the task in a script
 or terminal. I have searched Google, but I never saw a command. Typing 101
  010 or x = (int(101, 2)  int(010, 2)) only gives errors.

Your problem here isn't in the bitwise operators, but in your binary
literals. Python deliberately and consciously rejects 010 as a
literal, because it might be interpreted either as decimal 10, or as
octal (decimal 8), the latter being C's interpretation. Fixing that
shows up a more helpful error:

 x = (int(101, 2)  int(10, 2))
Traceback (most recent call last):
  File pyshell#74, line 1, in module
x = (int(101, 2)  int(10, 2))
TypeError: int() can't convert non-string with explicit base

The int() call isn't doing what you think it is, because 101 is
already an int. The obvious solution now is to quote the values:

 x = (int(101, 2)  int(010, 2))
 x
0

But there's an easier way:

 x = 0b101  0b010
 x
0

I think that might do what you want. Also check out the bin()
function, which will turn an integer into a string of digits.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bitwise Operations

2013-07-29 Thread Devyn Collier Johnson


On 07/29/2013 07:41 PM, Ethan Furman wrote:

On 07/29/2013 04:34 PM, Devyn Collier Johnson wrote:


On 07/29/2013 05:53 PM, Grant Edwards wrote:

On 2013-07-29, Devyn Collier Johnson devyncjohn...@gmail.com wrote:


On Python3, how can I perform bitwise operations? For instance, I want
something that will 'and', 'or', and 'xor' a binary integer.

http://www.google.com/search?q=python+bitwise+operations

I understand the symbols. I want to know how to perform the task in a 
script or terminal. I have searched Google, but I
never saw a command. Typing 101  010 or x = (int(101, 2)  
int(010, 2)) only gives errors.


x = (int('101', 2)  int('010', 2))

Notice the quotes.

In the future you'll better answers quicker if you tell us what you 
did (such as your example above) as well as the errors.


--
~Ethan~
Thanks Ethan for the code help and the tip. I need to get out of that 
habit of not including errors. This code works well, thanks! I cannot 
believe I was that close to the solution!


Now here is something that confuses me, the binary numbers are numbers 
not strings, so why are they put in quotes as if they are strings?



Mahalo,

DCJ
--
http://mail.python.org/mailman/listinfo/python-list


Re: Bitwise Operations

2013-07-29 Thread Chris Angelico
On Tue, Jul 30, 2013 at 12:48 AM, Devyn Collier Johnson
devyncjohn...@gmail.com wrote:
 Now here is something that confuses me, the binary numbers are numbers not
 strings, so why are they put in quotes as if they are strings?

They aren't numbers at that point, they're strings of digits. A number
is represented in various forms:

 1234, 0x4d2, 0o2322, 0b10011010010
(1234, 1234, 1234, 1234)

The two-argument form of int() takes a string of digits and a base:

 int(ya,36)
1234

In base 36, y and a are digits. But in Python's base syntax, you can't
use them that way, so there's no way to render the number other than
as a string.

For what you're doing, I think the 0b notation is the best. It's an
int literal written in binary.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bitwise Operations

2013-07-29 Thread MRAB

On 30/07/2013 00:34, Devyn Collier Johnson wrote:


On 07/29/2013 05:53 PM, Grant Edwards wrote:

On 2013-07-29, Devyn Collier Johnson devyncjohn...@gmail.com wrote:


On Python3, how can I perform bitwise operations? For instance, I want
something that will 'and', 'or', and 'xor' a binary integer.

http://www.google.com/search?q=python+bitwise+operations


I understand the symbols. I want to know how to perform the task in a
script or terminal. I have searched Google, but I never saw a command.
Typing 101  010 or x = (int(101, 2)  int(010, 2)) only gives errors.


In Python 2, an integer with a leading 0, such as 0101, was octal (base
8). This was a feature borrowed from C but often confused newbies
because it looked like decimal (Why does 0101 == 101 return False?).

In Python 3, octal is indicated by a leading 0o, such as 0o101 (==
1*64+0*8+1==65) and the old style raises an exception so that those who
have switched from Python 2 will get a clear message that something has
changed.

For binary you need a leading 0b and for hexadecimal you need a leading
0x, so doing something similar for octal makes sense.

0b101 == 1*4+0*2+0 == 5
0o101 == 1*64+0*8+1 == 65
0x101 == 1*256+0*16+1 == 257

--
http://mail.python.org/mailman/listinfo/python-list


Re: Bitwise Operations

2013-07-29 Thread Peter Otten
Devyn Collier Johnson wrote:

 On Python3, how can I perform bitwise operations? For instance, I want
 something that will 'and', 'or', and 'xor' a binary integer.

 0b1010 | 0b1100
14
 bin(_)
'0b1110'
 0b1010  0b1100
8
 bin(_)
'0b1000'
 0b1010 ^ 0b1100
6
 bin(_)
'0b110'


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bitwise Operations

2013-07-29 Thread Terry Reedy

On 7/29/2013 7:44 PM, Chris Angelico wrote:


But there's an easier way:


x = 0b101  0b010
x

0

I think that might do what you want. Also check out the bin()
function, which will turn an integer into a string of digits.


 bin(0b101 | 0b010)
'0b111'

Now you are set to go. Have fun.
--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


Bitwise operations in Python?

2005-08-18 Thread Carl
Dear friends,

I am currently porting a fortran program to Python but am stuck on the
intrinsic IBITS function. 

Does anyone know about a replacement function for IBITS in Python?

Yours, Carl





IBITS(I, POS, LEN)

Extracts a sequence of bits.

I
must be of type integer.

POS
must be of type integer. It must be nonnegative and POS + LEN must be
less than or equal to BIT_SIZE (I).

LEN
must be of type integer and nonnegative. 

Class

Elemental function

Result Type and Attributes

Same as I.

Result Value

The result has the value of the sequence of LEN bits in I beginning at bit
POS, right-adjusted and with all other bits zero.

The bits are numbered from 0 to BIT_SIZE(I)-1, from right to left.

Examples

IBITS (14, 1, 3) has the value 7. 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bitwise operations in Python?

2005-08-18 Thread Paul Rubin
Carl [EMAIL PROTECTED] writes:
 IBITS(I, POS, LEN)
 Extracts a sequence of bits.
 The result has the value of the sequence of LEN bits in I beginning at bit
 POS, right-adjusted and with all other bits zero.
 
 The bits are numbered from 0 to BIT_SIZE(I)-1, from right to left.
 
 Examples
 
 IBITS (14, 1, 3) has the value 7. 

 def ibits(i,pos,len):
return (i  pos)  ~(-1  len)

 ibits(14,1,3)
7
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bitwise operations in Python?

2005-08-18 Thread Carl
Incredible, Paul! Thanks a thousand times! /Carl
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bitwise operations in Python?

2005-08-18 Thread John Machin
Carl wrote:
 Dear friends,
 
 I am currently porting a fortran program to Python but am stuck on the
 intrinsic IBITS function. 
 
 Does anyone know about a replacement function for IBITS in Python?
 
 Yours, Carl
 
 IBITS(I, POS, LEN)
 
 Extracts a sequence of bits.
 
 I
 must be of type integer.
 
 POS
 must be of type integer. It must be nonnegative and POS + LEN must be
 less than or equal to BIT_SIZE (I).
 
 LEN
 must be of type integer and nonnegative. 
 
 Class
 
 Elemental function
 
 Result Type and Attributes
 
 Same as I.
 
 Result Value
 
 The result has the value of the sequence of LEN bits in I beginning at bit
 POS, right-adjusted and with all other bits zero.
 
 The bits are numbered from 0 to BIT_SIZE(I)-1, from right to left.
 
 Examples
 
 IBITS (14, 1, 3) has the value 7. 
 

No, don't know, but you could write one yourself PDQ. Something like this:

def ibits(arg, pos, len):
 return (arg  pos)  ((1  len) - 1)

if __name__ == __main__:

 def testit(arg, pos, len, reqd):
 result = ibits(arg, pos, len)
 print arg, pos, len, reqd, result, * [result == reqd]

 testit(14, 1, 3, 7)
 for pos in range(6):
 testit(4095, pos, 3, 7)
 for pos in range(6):
 testit(-1, pos, 3, 7)
 for pos in range(6):
 testit(0, pos, 3, 0)
 for pos in range(0, 32, 4):
 testit(0x12345678, pos, 4, 8-pos/4)
 for pos in range(0, 32, 4):
 testit(0x87654321, pos, 4, 1+pos/4)
-- 
http://mail.python.org/mailman/listinfo/python-list