-
Notifications
You must be signed in to change notification settings - Fork 1
Bitarray Library #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Bitarray Library #29
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| class bitarray: | ||
|
|
||
| def __init__(self, size): | ||
| self.arr = [0] * size | ||
|
|
||
|
|
||
| def setall(self, bit): | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Knit pick. Remove these empty lines. |
||
| if bit == 0 or bit == 1: | ||
| for i in range(len(self.arr)): | ||
| self.arr[i] = bit | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since everything is the same bit, it's probably faster to just do |
||
| else: | ||
| raise Exception("SetAll: Invalid bit entered, only 1 or 0 are valid inputs.") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recommend raising specific exceptions when it makes sense. This seems to be a |
||
|
|
||
|
|
||
| def set(self, n, bit): | ||
|
|
||
| if bit == 0 or bit == 1: | ||
|
|
||
| try: | ||
| self.arr[n] = bit | ||
| 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") | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Forgot to add generic exceptions here
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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. |
||
|
|
||
| else: | ||
| raise Exception("Set: Invalid bit entered, only 1 or 0 are valid inputs.") | ||
|
|
||
|
|
||
|
|
||
| def get(self, n): | ||
|
|
||
| try: | ||
| return self.arr[n] | ||
| 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.") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should add a
__str__and__repr__as a good practice. https://docs.python.org/3.8/reference/datamodel.html#object.__str__