Skip to content

Commit

Permalink
Negative index processing moved to a method
Browse files Browse the repository at this point in the history
  • Loading branch information
Milan Falešník committed May 26, 2017
1 parent 7b4d099 commit 93aa830
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/widgetastic/widget.py
Expand Up @@ -1322,17 +1322,20 @@ def assoc_column_position(self):
raise TypeError(
'Wrong type passed for assoc_column= : {}'.format(type(self.assoc_column).__name__))

def _process_negative_index(self, nindex):
"""The semantics is pretty much the same like for ordinary list."""
rc = self.row_count
if (- nindex) > rc:
raise ValueError(
'Negative index {} wanted but we only have {} rows'.format(nindex, rc))
return rc + nindex

def __getitem__(self, at_index):
if not isinstance(at_index, int):
raise TypeError('table indexing only accepts integers')
if at_index < 0:
# Row class cannot handle negative indices, so we need to convert it ourselves.
# The semantics is pretty much the same like for ordinary list.
rc = self.row_count
if (- at_index) > rc:
raise ValueError(
'Negative index {} wanted but we only have {} rows'.format(at_index, rc))
at_index = rc + at_index
at_index = self._process_negative_index(at_index)
return self.Row(self, at_index, logger=self.logger)

def row(self, *extra_filters, **filters):
Expand Down

0 comments on commit 93aa830

Please sign in to comment.