Closed dgw closed 4 years ago
If you want to be very fancy, you can use cycle
and a list-comprehension:
color_iterable = itertools.cycle(colors)
''.join(
char if unicodedata.category(char) == 'Zs'
else formatting.color(char, next(color_iterable))
for char in text
)
And to randomize it:
for _ in range(random.randint(len(colors))):
next(color_iterable)
Totally unnecessary so I had to do it.
A quick timeit
experiment shows that it's faster to set up deque
s, and faster to iterate over cycle
s (which makes sense).
But I'm too lazy to set up all the boilerplate that would let me actually test both whole implementations—and much too lazy to set up instrumentation that would let me profile the memory usage/bandwidth of each approach—so let's just let @Exirel make the code fancier by proxy. 😼
Oh yeah, it's configurable. I had to.