Coding style.

- Slightly optimize add_entry by having sorted_collection.insert
  return where the insert was.
This commit is contained in:
Andrew Hamilton 2021-04-13 19:48:06 +10:00
parent 65e136941b
commit 7cfcc56250
2 changed files with 7 additions and 3 deletions

View file

@ -275,8 +275,7 @@ class Summary:
Entry.MAX_WIDTH = max(len(entry), Entry.MAX_WIDTH)
self._max_path_length = max(len(entry.path) - len("./"),
self._max_path_length)
self._entries.insert(entry)
entry_index = self._entries.index(entry)
entry_index = self._entries.insert(entry)
x, y = self._cursor_position
if entry_index <= y:
self.scroll(0, -1)

View file

@ -64,7 +64,10 @@ class SortedCollection(object):
... ('bill', 'smith', 22),
... ('david', 'thomas', 32)]:
... s.insert(record)
0
0
0
3
>>> pprint(list(s)) # show records sorted by age
[('bill', 'smith', 22),
('angela', 'jones', 28),
@ -167,6 +170,7 @@ class SortedCollection(object):
i = bisect_left(self._keys, k)
self._keys.insert(i, k)
self._items.insert(i, item)
return i
def insert_right(self, item):
'Insert a new item. If equal keys are found, add to the right'
@ -174,6 +178,7 @@ class SortedCollection(object):
i = bisect_right(self._keys, k)
self._keys.insert(i, k)
self._items.insert(i, item)
return i
def remove(self, item):
'Remove first occurence of item. Raise ValueError if not found'