BitVector library, changing value in splice of BitVector
I have the following code:
from BitVector import BitVector
import struct
import ipaddress
def run_marker_function(marker):
bv = BitVector(size=16) # 16 0s
m(bv, "27.246.2.177", "255.255.255.0")
def m(bv, R, R1):
# converts ipaddr strings to binary
bitForm = bin(int(ipaddress.IPv4Address(R)))[2:]
bitForm1 = bin(int(ipaddress.IPv4Address(R1)))[2:]
# R2 helper fn is the interleaved output of the 32 bit IP addresses in binary form (R2 is 64 bits)
R2 = Interleaved(bitForm, bitForm1)
# splits R2 into list of 8 items, each 8 bits
frags = [R2[i:i+8] for i in range(0, len(R2), 8)]
o = 3
f = frags[o]
# bv[0:3] = 000 initially, I increment by o
offset = bv[0:3].int_val() + o
bv[0:3] = BitVector(intVal=offset, size=3)
# bv[8:16] = 00000000 initially, I want to replace with the 8 bits of f - DOESN'T WORK
frag = bv[8:16].int_val() + int(f,2)
bv[8:16] = BitVector(intVal=frag, size=len(bv[8:16]))
and I get the following error:
18 frag = bv[8:16].int_val() + int(f,2)
---> 19 bv[8:16] = BitVector(intVal=frag, size=len(bv[8:16]))
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/BitVector/BitVector.py in __init__(self, *args, **kwargs)
else:
if size < len(bitlist):
raise ValueError('''The value specified for size must be at least as large as for the smallest bit vector possible for intVal''')
ValueError: The value specified for size must be at least as large as for the smallest bit vector possible for intVal
But I don't understand why as I'm putting in the actual length of that splice of the bv. Also, I'm doing the same methodology in
offset = bv[0:3].int_val() + o
bv[0:3] = BitVector(intVal=offset, size=3)
which doesn't throw an error
EDIT
A similar instance of the same bug:
dist = bv[3:8].int_val() + 1
bv[3:8] = BitVector(intVal=dist, size=1)
Gives the error
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/BitVector/BitVector.py in __setitem__(self, pos, item)
if ( (pos.stop - pos.start) != len(item) ):
raise ValueError('incompatible lengths for slice assignment 7')
for i in range( pos.start, pos.stop ):
self[i] = item[ i - pos.start ]
ValueError: incompatible lengths for slice assignment 7
python python-3.x bitvector
|
show 1 more comment
I have the following code:
from BitVector import BitVector
import struct
import ipaddress
def run_marker_function(marker):
bv = BitVector(size=16) # 16 0s
m(bv, "27.246.2.177", "255.255.255.0")
def m(bv, R, R1):
# converts ipaddr strings to binary
bitForm = bin(int(ipaddress.IPv4Address(R)))[2:]
bitForm1 = bin(int(ipaddress.IPv4Address(R1)))[2:]
# R2 helper fn is the interleaved output of the 32 bit IP addresses in binary form (R2 is 64 bits)
R2 = Interleaved(bitForm, bitForm1)
# splits R2 into list of 8 items, each 8 bits
frags = [R2[i:i+8] for i in range(0, len(R2), 8)]
o = 3
f = frags[o]
# bv[0:3] = 000 initially, I increment by o
offset = bv[0:3].int_val() + o
bv[0:3] = BitVector(intVal=offset, size=3)
# bv[8:16] = 00000000 initially, I want to replace with the 8 bits of f - DOESN'T WORK
frag = bv[8:16].int_val() + int(f,2)
bv[8:16] = BitVector(intVal=frag, size=len(bv[8:16]))
and I get the following error:
18 frag = bv[8:16].int_val() + int(f,2)
---> 19 bv[8:16] = BitVector(intVal=frag, size=len(bv[8:16]))
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/BitVector/BitVector.py in __init__(self, *args, **kwargs)
else:
if size < len(bitlist):
raise ValueError('''The value specified for size must be at least as large as for the smallest bit vector possible for intVal''')
ValueError: The value specified for size must be at least as large as for the smallest bit vector possible for intVal
But I don't understand why as I'm putting in the actual length of that splice of the bv. Also, I'm doing the same methodology in
offset = bv[0:3].int_val() + o
bv[0:3] = BitVector(intVal=offset, size=3)
which doesn't throw an error
EDIT
A similar instance of the same bug:
dist = bv[3:8].int_val() + 1
bv[3:8] = BitVector(intVal=dist, size=1)
Gives the error
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/BitVector/BitVector.py in __setitem__(self, pos, item)
if ( (pos.stop - pos.start) != len(item) ):
raise ValueError('incompatible lengths for slice assignment 7')
for i in range( pos.start, pos.stop ):
self[i] = item[ i - pos.start ]
ValueError: incompatible lengths for slice assignment 7
python python-3.x bitvector
The issue may be the value offrag
, since it has to fit in the size, too. This is what the the error message is complaining about I think. Your example at the end withoffset
may work simply be its value is small enough to fit.
– martineau
Nov 11 at 7:26
@martineau thanks that fixed it! I thought solving that would solve another instance of the same issue, but it doesn't (I added in the edit). dist is 1 so how come size=1 throws an error?
– user1045890
Nov 11 at 7:37
@martineau sorry just added
– user1045890
Nov 11 at 7:39
A size of 1 means the valuedist
can only be0
or1
. That looks doubtful...
– martineau
Nov 11 at 7:43
@martineau hm I'm not quite sure what's going on anymore, sometimes when i run my code I get no errors but sometimes I get the wrong size errors :/
– user1045890
Nov 11 at 7:49
|
show 1 more comment
I have the following code:
from BitVector import BitVector
import struct
import ipaddress
def run_marker_function(marker):
bv = BitVector(size=16) # 16 0s
m(bv, "27.246.2.177", "255.255.255.0")
def m(bv, R, R1):
# converts ipaddr strings to binary
bitForm = bin(int(ipaddress.IPv4Address(R)))[2:]
bitForm1 = bin(int(ipaddress.IPv4Address(R1)))[2:]
# R2 helper fn is the interleaved output of the 32 bit IP addresses in binary form (R2 is 64 bits)
R2 = Interleaved(bitForm, bitForm1)
# splits R2 into list of 8 items, each 8 bits
frags = [R2[i:i+8] for i in range(0, len(R2), 8)]
o = 3
f = frags[o]
# bv[0:3] = 000 initially, I increment by o
offset = bv[0:3].int_val() + o
bv[0:3] = BitVector(intVal=offset, size=3)
# bv[8:16] = 00000000 initially, I want to replace with the 8 bits of f - DOESN'T WORK
frag = bv[8:16].int_val() + int(f,2)
bv[8:16] = BitVector(intVal=frag, size=len(bv[8:16]))
and I get the following error:
18 frag = bv[8:16].int_val() + int(f,2)
---> 19 bv[8:16] = BitVector(intVal=frag, size=len(bv[8:16]))
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/BitVector/BitVector.py in __init__(self, *args, **kwargs)
else:
if size < len(bitlist):
raise ValueError('''The value specified for size must be at least as large as for the smallest bit vector possible for intVal''')
ValueError: The value specified for size must be at least as large as for the smallest bit vector possible for intVal
But I don't understand why as I'm putting in the actual length of that splice of the bv. Also, I'm doing the same methodology in
offset = bv[0:3].int_val() + o
bv[0:3] = BitVector(intVal=offset, size=3)
which doesn't throw an error
EDIT
A similar instance of the same bug:
dist = bv[3:8].int_val() + 1
bv[3:8] = BitVector(intVal=dist, size=1)
Gives the error
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/BitVector/BitVector.py in __setitem__(self, pos, item)
if ( (pos.stop - pos.start) != len(item) ):
raise ValueError('incompatible lengths for slice assignment 7')
for i in range( pos.start, pos.stop ):
self[i] = item[ i - pos.start ]
ValueError: incompatible lengths for slice assignment 7
python python-3.x bitvector
I have the following code:
from BitVector import BitVector
import struct
import ipaddress
def run_marker_function(marker):
bv = BitVector(size=16) # 16 0s
m(bv, "27.246.2.177", "255.255.255.0")
def m(bv, R, R1):
# converts ipaddr strings to binary
bitForm = bin(int(ipaddress.IPv4Address(R)))[2:]
bitForm1 = bin(int(ipaddress.IPv4Address(R1)))[2:]
# R2 helper fn is the interleaved output of the 32 bit IP addresses in binary form (R2 is 64 bits)
R2 = Interleaved(bitForm, bitForm1)
# splits R2 into list of 8 items, each 8 bits
frags = [R2[i:i+8] for i in range(0, len(R2), 8)]
o = 3
f = frags[o]
# bv[0:3] = 000 initially, I increment by o
offset = bv[0:3].int_val() + o
bv[0:3] = BitVector(intVal=offset, size=3)
# bv[8:16] = 00000000 initially, I want to replace with the 8 bits of f - DOESN'T WORK
frag = bv[8:16].int_val() + int(f,2)
bv[8:16] = BitVector(intVal=frag, size=len(bv[8:16]))
and I get the following error:
18 frag = bv[8:16].int_val() + int(f,2)
---> 19 bv[8:16] = BitVector(intVal=frag, size=len(bv[8:16]))
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/BitVector/BitVector.py in __init__(self, *args, **kwargs)
else:
if size < len(bitlist):
raise ValueError('''The value specified for size must be at least as large as for the smallest bit vector possible for intVal''')
ValueError: The value specified for size must be at least as large as for the smallest bit vector possible for intVal
But I don't understand why as I'm putting in the actual length of that splice of the bv. Also, I'm doing the same methodology in
offset = bv[0:3].int_val() + o
bv[0:3] = BitVector(intVal=offset, size=3)
which doesn't throw an error
EDIT
A similar instance of the same bug:
dist = bv[3:8].int_val() + 1
bv[3:8] = BitVector(intVal=dist, size=1)
Gives the error
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/BitVector/BitVector.py in __setitem__(self, pos, item)
if ( (pos.stop - pos.start) != len(item) ):
raise ValueError('incompatible lengths for slice assignment 7')
for i in range( pos.start, pos.stop ):
self[i] = item[ i - pos.start ]
ValueError: incompatible lengths for slice assignment 7
python python-3.x bitvector
python python-3.x bitvector
edited Nov 11 at 7:46
martineau
65.5k988177
65.5k988177
asked Nov 11 at 5:27
user1045890
996
996
The issue may be the value offrag
, since it has to fit in the size, too. This is what the the error message is complaining about I think. Your example at the end withoffset
may work simply be its value is small enough to fit.
– martineau
Nov 11 at 7:26
@martineau thanks that fixed it! I thought solving that would solve another instance of the same issue, but it doesn't (I added in the edit). dist is 1 so how come size=1 throws an error?
– user1045890
Nov 11 at 7:37
@martineau sorry just added
– user1045890
Nov 11 at 7:39
A size of 1 means the valuedist
can only be0
or1
. That looks doubtful...
– martineau
Nov 11 at 7:43
@martineau hm I'm not quite sure what's going on anymore, sometimes when i run my code I get no errors but sometimes I get the wrong size errors :/
– user1045890
Nov 11 at 7:49
|
show 1 more comment
The issue may be the value offrag
, since it has to fit in the size, too. This is what the the error message is complaining about I think. Your example at the end withoffset
may work simply be its value is small enough to fit.
– martineau
Nov 11 at 7:26
@martineau thanks that fixed it! I thought solving that would solve another instance of the same issue, but it doesn't (I added in the edit). dist is 1 so how come size=1 throws an error?
– user1045890
Nov 11 at 7:37
@martineau sorry just added
– user1045890
Nov 11 at 7:39
A size of 1 means the valuedist
can only be0
or1
. That looks doubtful...
– martineau
Nov 11 at 7:43
@martineau hm I'm not quite sure what's going on anymore, sometimes when i run my code I get no errors but sometimes I get the wrong size errors :/
– user1045890
Nov 11 at 7:49
The issue may be the value of
frag
, since it has to fit in the size, too. This is what the the error message is complaining about I think. Your example at the end with offset
may work simply be its value is small enough to fit.– martineau
Nov 11 at 7:26
The issue may be the value of
frag
, since it has to fit in the size, too. This is what the the error message is complaining about I think. Your example at the end with offset
may work simply be its value is small enough to fit.– martineau
Nov 11 at 7:26
@martineau thanks that fixed it! I thought solving that would solve another instance of the same issue, but it doesn't (I added in the edit). dist is 1 so how come size=1 throws an error?
– user1045890
Nov 11 at 7:37
@martineau thanks that fixed it! I thought solving that would solve another instance of the same issue, but it doesn't (I added in the edit). dist is 1 so how come size=1 throws an error?
– user1045890
Nov 11 at 7:37
@martineau sorry just added
– user1045890
Nov 11 at 7:39
@martineau sorry just added
– user1045890
Nov 11 at 7:39
A size of 1 means the value
dist
can only be 0
or 1
. That looks doubtful...– martineau
Nov 11 at 7:43
A size of 1 means the value
dist
can only be 0
or 1
. That looks doubtful...– martineau
Nov 11 at 7:43
@martineau hm I'm not quite sure what's going on anymore, sometimes when i run my code I get no errors but sometimes I get the wrong size errors :/
– user1045890
Nov 11 at 7:49
@martineau hm I'm not quite sure what's going on anymore, sometimes when i run my code I get no errors but sometimes I get the wrong size errors :/
– user1045890
Nov 11 at 7:49
|
show 1 more comment
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246092%2fbitvector-library-changing-value-in-splice-of-bitvector%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246092%2fbitvector-library-changing-value-in-splice-of-bitvector%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The issue may be the value of
frag
, since it has to fit in the size, too. This is what the the error message is complaining about I think. Your example at the end withoffset
may work simply be its value is small enough to fit.– martineau
Nov 11 at 7:26
@martineau thanks that fixed it! I thought solving that would solve another instance of the same issue, but it doesn't (I added in the edit). dist is 1 so how come size=1 throws an error?
– user1045890
Nov 11 at 7:37
@martineau sorry just added
– user1045890
Nov 11 at 7:39
A size of 1 means the value
dist
can only be0
or1
. That looks doubtful...– martineau
Nov 11 at 7:43
@martineau hm I'm not quite sure what's going on anymore, sometimes when i run my code I get no errors but sometimes I get the wrong size errors :/
– user1045890
Nov 11 at 7:49