Advertisement
here2share

# PyBuffer.py

Nov 29th, 2019
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. # PyBuffer.py
  2.  
  3. class PyBuffer:
  4.   def __init__(self, capacity):
  5.     self.capacity = capacity
  6.     self.current = 0
  7.     self.storage = [None]*capacity
  8.  
  9.   def append(self, item):
  10.     self.storage[self.current] = item
  11.     self.current += 1
  12.     # If we reach the capacity of the buffer
  13.     # set the current pointer back to the beginning
  14.     if self.current == self.capacity:
  15.       self.current = 0
  16.  
  17.   def all(self):
  18.     return self.storage
  19.  
  20.  
  21. # Some console.log tests
  22. buffer = PyBuffer(5)
  23.  
  24. buffer.append('a')
  25. buffer.append('b')
  26. buffer.append('c')
  27. buffer.append('d')
  28. print(buffer.all())  # should print ['a', 'b', 'c', 'd', None]
  29.  
  30. buffer.append('e');
  31. print(buffer.all())  # should print ['a', 'b', 'c', 'd', 'e']
  32.  
  33. buffer.append('f');
  34. print(buffer.all())  # should print ['f', 'b', 'c', 'd', 'e']
  35.  
  36. buffer.append('g');
  37. buffer.append('h');
  38. buffer.append('i');
  39. print(buffer.all())  # should print ['f', 'g', 'h', 'i', 'e']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement