Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ''' circular_sequence_generator3.py
- can be replaced with itertools.cycle()
- with Python25 use f.next()
- with Python26 and higher use next(f)
- '''
- # allows Python27 to use Python3 print() options
- from __future__ import print_function
- import itertools
- def circular_sequence_gen(seq=""):
- '''
- generic circular sequence generator
- '''
- i = 0
- while True:
- yield seq[i]
- i = (i + 1)%len(seq)
- # testing ...
- mystr = 'abc'
- for count, c in enumerate(circular_sequence_gen(mystr)):
- print(c, end="")
- if count >= 20:
- break
- print('')
- for count, c in enumerate(itertools.cycle(mystr)):
- print(c, end="")
- if count >= 20:
- break
- print('')
- colors = ['red', 'blue', 'green', 'orange']
- color_gen = circular_sequence_gen(colors)
- for color in range(12):
- # Python26 and up
- print(next(color_gen))
- print('-'*20)
- color_gen = itertools.cycle(colors)
- for color in range(12):
- # Python26 and up
- print(next(color_gen))
- ''' result -->
- abcabcabcabcabcabcabc
- abcabcabcabcabcabcabc
- red
- blue
- green
- orange
- red
- blue
- green
- orange
- red
- blue
- green
- orange
- --------------------
- red
- blue
- green
- orange
- red
- blue
- green
- orange
- red
- blue
- green
- orange
- '''
Advertisement
Add Comment
Please, Sign In to add comment