fork(1) download
  1. from itertools import cycle, islice
  2.  
  3. class CycleView:
  4. def __init__(self, seq):
  5. self.seq = seq
  6.  
  7. def __getitem__(self, index):
  8. if isinstance(index, slice):
  9. start = (index.start or 0) % len(self.seq)
  10. if (stop := index.stop) is not None:
  11. stop = start + stop - (index.start or 0)
  12. return islice(cycle(self.seq), start, stop)
  13. return self.seq[index % len(self.seq)]
  14.  
  15. DEADVUL = 'x', 'y', 'z'
  16. print(*CycleView(DEADVUL)[:10])
  17. print(*CycleView(DEADVUL)[2000000000:2000000010])
Success #stdin #stdout 0.04s 9656KB
stdin
Standard input is empty
stdout
x y z x y z x y z x
z x y z x y z x y z