Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # PyBuffer.py
- class PyBuffer:
- def __init__(self, capacity):
- self.capacity = capacity
- self.current = 0
- self.storage = [None]*capacity
- def append(self, item):
- self.storage[self.current] = item
- self.current += 1
- # If we reach the capacity of the buffer
- # set the current pointer back to the beginning
- if self.current == self.capacity:
- self.current = 0
- def all(self):
- return self.storage
- # Some console.log tests
- buffer = PyBuffer(5)
- buffer.append('a')
- buffer.append('b')
- buffer.append('c')
- buffer.append('d')
- print(buffer.all()) # should print ['a', 'b', 'c', 'd', None]
- buffer.append('e');
- print(buffer.all()) # should print ['a', 'b', 'c', 'd', 'e']
- buffer.append('f');
- print(buffer.all()) # should print ['f', 'b', 'c', 'd', 'e']
- buffer.append('g');
- buffer.append('h');
- buffer.append('i');
- print(buffer.all()) # should print ['f', 'g', 'h', 'i', 'e']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement