Bitarray Library#29
Conversation
| except IndexError: | ||
| raise Exception("Set: Value entered is out of range of the array.") | ||
| except TypeError: | ||
| raise Exception("Set: Enter a value that is an integer") |
There was a problem hiding this comment.
Forgot to add generic exceptions here
There was a problem hiding this comment.
It's better to just re-raise the same type of exceptions here if you want to add this information. You should also specify which value was incorrect as it's not clear what exactly went wrong from the message alone.
| for i in range(len(self.arr)): | ||
| self.arr[i] = bit | ||
| else: | ||
| raise Exception("SetAll: Invalid bit entered, only 1 or 0 are valid inputs.") |
There was a problem hiding this comment.
I recommend raising specific exceptions when it makes sense. This seems to be a ValueError, you can reference the builtin exceptions here: https://docs.python.org/3/library/exceptions.html#ValueError
|
|
||
| if bit == 0 or bit == 1: | ||
| for i in range(len(self.arr)): | ||
| self.arr[i] = bit |
There was a problem hiding this comment.
Since everything is the same bit, it's probably faster to just do self.arr = [bit] * len(self.arr) again.
|
|
||
| def __init__(self, size): | ||
| self.arr = [0] * size | ||
|
|
There was a problem hiding this comment.
You should add a__str__ and __repr__ as a good practice. https://docs.python.org/3.8/reference/datamodel.html#object.__str__
| except IndexError: | ||
| raise Exception("Set: Value entered is out of range of the array.") | ||
| except TypeError: | ||
| raise Exception("Set: Enter a value that is an integer") |
There was a problem hiding this comment.
It's better to just re-raise the same type of exceptions here if you want to add this information. You should also specify which value was incorrect as it's not clear what exactly went wrong from the message alone.
| except IndexError: | ||
| raise Exception("Get: Value entered is out of range of the array.") | ||
| except TypeError: | ||
| raise Exception("Get: Value entered is not an integer.") |
There was a problem hiding this comment.
Same as above, re-raise the same error. Also I don't think it's necessary to mention which method in the exception message. We will already know that from the stacktrace.
|
|
||
|
|
||
| def setall(self, bit): | ||
|
|
There was a problem hiding this comment.
Knit pick. Remove these empty lines.
|
Good work, from an API standpoint this looks good, and could be a drop-in replacement for the current Space ComplexityIn this implementation, we represent a bit using a Python For example, let's say we want 1000 bits:
In memory, Now, if we run And, if we run We can see here that we have 64 bits in total, but we only ever use 8 bits to represent information; the rest is just padding. It would be nice if we could use all the bits in every byte, so in this case for a Expected BehaviourBuilding off the previous example, here's what we should expect to see: We should only need 1 byte to represent 8 bits of data (not including the size of the Now, if we run (Here setting the bit at index 3 is a bitwise operation: If we run (Here setting all the bits to 1 is a constant operation: Use
|
This is a library that creates a bitarray with a specified size (all set to the value of 0). This has 3 methods where it can setall() for every array index to the parameter (as a bit), set() which sets only one index of the array, and get() to retrieve a value from the array.
This can be typed as well from the research I gathered to make it more explicit, let me know if that should be done.