-
Notifications
You must be signed in to change notification settings - Fork 96
Add __getitem__
method to allow easy access to version parts
#138
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
Changes from all commits
f1d7cde
ba5899e
e88e0ba
c30d38f
26a7e03
80da20c
a610c22
95e79d8
ab35e38
27632b7
976a4ee
cf65b3d
dabebd9
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 |
---|---|---|
|
@@ -174,6 +174,39 @@ def __iter__(self): | |
for v in self._astuple(): | ||
yield v | ||
|
||
def __getitem__(self, index): | ||
"""Implement getitem. If the part requested is undefined, or a part of the | ||
range requested is undefined, it will throw an index error. | ||
Negative indices are not supported | ||
|
||
:param Union[int, slice] index: a positive integer indicating the | ||
offset or a slice | ||
:raises: IndexError, if index is beyond the range or a part is None | ||
""" | ||
undefined_error = IndexError("Version part undefined") | ||
negative_error = IndexError("Version index cannot be negative") | ||
if isinstance(index, slice): | ||
if (False if index.start is None else index.start < 0) or \ | ||
(False if index.stop is None else index.stop < 0): | ||
raise negative_error | ||
else: | ||
slice_is_full = True | ||
part = self._astuple()[index] | ||
for i in part: | ||
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. What is 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. One idea could be to replace 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 guess I could use 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. See also the more recent comments below. 😉 |
||
if i is None: | ||
slice_is_full = False | ||
elif not slice_is_full: | ||
raise undefined_error | ||
part = tuple(filter(None, part)) | ||
else: | ||
if index < 0: | ||
raise negative_error | ||
else: | ||
part = self._astuple()[index] | ||
if part is None: | ||
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. if not part: ... is probably more pythonic. Or is there a reason for an explicit 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. This is in several places in the code, by the way, and could be made pythonic throughout the code. |
||
raise undefined_error | ||
return part | ||
|
||
def bump_major(self): | ||
"""Raise the major part of the version, return a new object | ||
but leave self untouched | ||
|
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.
This docstring has an unusual indentation. According to PEP 257 indentation may look like:
(what I like less but seems to be favored by Guido & friends), or:
Same below, in the method following next.
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.
Good point, but then the other docstrings needs to be adapted as well. Maybe we could do that in a combined issue later?