Created
June 30, 2020 23:02
-
-
Save guionardo/c078616b85b106aefe8d23279f640ff5 to your computer and use it in GitHub Desktop.
Bus List in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections.abc import Iterable | |
| class BusList(list): | |
| """ Bus (FIFO) list | |
| """ | |
| def __init__(self, *args): | |
| self._max_length = 0 | |
| for arg in args: | |
| if isinstance(arg, int): | |
| self._max_length = arg | |
| elif isinstance(arg, Iterable): | |
| super().__init__(arg) | |
| def _bus_out(self, index=0): | |
| if self._max_length > 0 and len(self) == self._max_length: | |
| self.pop(min(index, len(self)-1)) | |
| def append(self, value): | |
| self._bus_out() | |
| super().append(value) | |
| def insert(self, index, value): | |
| self._bus_out() | |
| super().insert(index, value) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from bus_list import BusList | |
| bl = BusList(4, [1, 2, 3, 4]) | |
| print(bl) | |
| for n in range(5, 10): | |
| bl.append(n) | |
| print(bl) | |
| """ | |
| RESULT: | |
| [1, 2, 3, 4] | |
| [2, 3, 4, 5] | |
| [3, 4, 5, 6] | |
| [4, 5, 6, 7] | |
| [5, 6, 7, 8] | |
| [6, 7, 8, 9] | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment