Hello,
can anyone help me regarding how this can be done?

I need to declare an 'array' which has 95 indices, and each entry holds 8 bits each of data.

am doing this with:

PORT_MAP_LEN = 764/8

bit_map_len = [0]*PORT_MAP_LEN


but it is giving me list index out of range error, while accessing bit_map_len[] sometimes after 1 run, sometimes after 10, 150, 460, 760 runs and so on(i get this error randomly).
anyone pls help me out

If each entry contains 8 bits of data, you may consider using the array module instead of a list. Otherwise the code that you wrote above doesn't produce list index out of range error. This error does not occur randomly, so your program must have changed the size of the list somewhere, or you are accessing an index outside of the interval [0, 95[ . Also please use code tags for python code :)

Ok :)

I used the following:

import array
bit_map_len = array.array('B',[0]*PORT_MAP_LEN)

still it is giving the error, now after 513 runs...

no I am not changing the size anywhere, and not accessing index out of interval :)

Edited 5 Years Ago by novice20: n/a

Ok :)

I used the following:

import array
bit_map_len = array.array('B',[0]*764)

still it is giving the error, now after 513 runs...

no I am not changing the size anywhere, and not accessing index out of interval :)

You could avoid creating a list each time by storing an array of zeroes

import array
from copy import copy
zeroes = array.array('B',[0]*764)
...
bit_map_len = copy(zeroes)

Otherwise the error is somewhere in the code that you did NOT post, so it's difficult to guess. What you could do is add assert statements here and then in your program, like

assert len(bit_map_len) == 764
assert 0 <= index < 764

Edited 5 Years Ago by Gribouillis: n/a

i think it is the problem with data types. am not sure :( how can i convert a result of pysnmp get into an unsigned char??

unsigned char, signed, unsigned int, unsigned char array... do all these things differ and cause the code to go astray....

there is no way to predefine data type of any variable right?

i think it is the problem with data types. am not sure :( how can i convert a result of pysnmp get into an unsigned char??

unsigned char, signed, unsigned int, unsigned char array... do all these things differ and cause the code to go astray....

there is no way to predefine data type of any variable right?

You don't really need to worry about this: python is not C. In an array.array() defined with type 'B', you can only store integers in the range [0, 256[. Python will complain if you try to give another value to an array element

>>> import array
>>> a = array.array('B', [0]*10)
>>> a[3] = 300
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: unsigned byte integer is greater than maximum
>>> a[3] = -23
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: unsigned byte integer is less than minimum
>>> a[3] = 45 # ok
>>>

All you have to do is to convert your pysnmp results into ordinary python integers.

In any case, these datatype issues cannot lead to 'List index out of range'.

Edited 5 Years Ago by Gribouillis: n/a

Comments
agree

Thanks tons Gribouillis :) i converted the return value of pysnmp into python int. and the error, i suppose was caused due to system level issue. I had set some different value for a parameter of the system card. But, it didn't strike my mind... sorry for having posted this query, but thanks for guiding me in the right direction :)

This question has already been answered. Start a new discussion instead.