I'm working on a class that reads bytes from a mmap and I'd like to add the ability to "rewind" a given read. I feel like a decorator could help here, but I'm not sure how to implement it.
Here's how it looks:
class ByteReader:
  def __init__(self, data: bytes):
    self.data = data
  def read_bytes(self, n_bytes: int, rewind: bool=False):
    if rewind:
      start_position = self.data.tell()
    read_data = self.data.read(n_bytes)
    if rewind:
      self.data.seek(start_position)
    
    return read_data
I know I could create a rewind function that takes a function as an argument, like this ...
  def rewind(self, func):
    start_position = self.data.tell()
    data = func()
    self.data.seek(start_position)
    return data
... but I feel like there's a better way to implement this.
(btw I'm not really worried if this isn't the best way to accomplish this particular task, I'm more interested in learning how to use decorators!)
