[Chicago] islice on Nested Iterators

Adam Forsyth adam at adamforsyth.net
Mon Jan 14 16:30:30 CET 2013


This seems like a great candidate for a follow up SO question. If you post
it, send the link!

Adam


On Mon, Jan 14, 2013 at 6:37 AM, Oren Livne <livne at uchicago.edu> wrote:

> Dear All,
>
> I'm trying to reuse the solution at the end of http://stackoverflow.com/**
> questions/470690/how-to-**automatically-generate-n-**distinct-colors<http://stackoverflow.com/questions/470690/how-to-automatically-generate-n-distinct-colors>for Python 2.7. Unfortunately, the code below never returns, since even
> though islice() requests the first 100 iterates, it seems that the
> inner-map calls like
>
> gethsvs = lambda: flatten(itertools.imap(genhsv, getfracs()))
>
> force getting all iterates. Instead of passing the number of iterates to
> all lambda functions, is there a way to make an islice() call at the main
> code, as below, but making it get only the first 100 iterates in all inner
> maps as well?
>
> Thanks,
> Oren
>
> ---------------------
>
> import colorsys, itertools, numpy as np
> from fractions import Fraction
>
> def zenos_dichotomy():
>     '''
> http://en.wikipedia.org/wiki/**1/2_%2B_1/4_%2B_1/8_%2B_1/16_%**
> 2B_%C2%B7_%C2%B7_%C2%B7<http://en.wikipedia.org/wiki/1/2_%2B_1/4_%2B_1/8_%2B_1/16_%2B_%C2%B7_%C2%B7_%C2%B7>
>     '''
>     for k in itertools.count():
>         yield Fraction(1, 2 ** k)
>
> def getfracs():
>     '''
>     [Fraction(0, 1), Fraction(1, 2), Fraction(1, 4), Fraction(3, 4),
> Fraction(1, 8), Fraction(3, 8), Fraction(5, 8), Fraction(7, 8), Fraction(1,
> 16), Fraction(3, 16), ...]
>     [0.0, 0.5, 0.25, 0.75, 0.125, 0.375, 0.625, 0.875, 0.0625, 0.1875, ...]
>     '''
>     yield 0
>     for k in zenos_dichotomy():
>         i = k.denominator # [1,2,4,8,16,...]
>         for j in range(1, i, 2):
>             yield Fraction(j, i)
>
> '''Can be used for the v in hsv to map linear values 0..1 to something
> that looks equidistant.'''
> bias = lambda x: (np.sqrt(x / 3) / Fraction(2, 3) + Fraction(1, 3)) /
> Fraction(6, 5)
>
> def genhsv(h):
>     for s in [Fraction(6, 10)]: # optionally use range
>         for v in [Fraction(8, 10), Fraction(5, 10)]: # could use range too
>             yield (h, s, v) # use bias for v here if you use range
>
> genrgb = lambda x: colorsys.hsv_to_rgb(*x)
> flatten = itertools.chain.from_iterable
> gethsvs = lambda: flatten(itertools.imap(genhsv, getfracs()))
> getrgbs = lambda: itertools.imap(genrgb, gethsvs())
>
> def genhtml(x):
>     uint8tuple = itertools.imap(lambda y: int(y * 255), x)
>     return 'rgb({},{},{})'.format(***uint8tuple)
>
> gethtmlcolors = lambda: map(genhtml, getrgbs())
>
> if __name__ == '__main__':
>     print(list(itertools.islice(**gethtmlcolors(), 100)))
>
> --
> A person is just about as big as the things that make him angry.
>
> ______________________________**_________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/**mailman/listinfo/chicago<http://mail.python.org/mailman/listinfo/chicago>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20130114/0cfcac15/attachment.html>


More information about the Chicago mailing list