[CentralOH] 2013-06-07 道場 Scribbles 落書/惡文?

jep200404 at columbus.rr.com jep200404 at columbus.rr.com
Mon Jun 10 19:16:16 CEST 2013


There were two particularly nifty things this week:

    nested generators
    tuple unpacking

Last week I wrote:

    def even_fibonacci(last):
        a = 0
        b = 1
        while True:
            c = a + b
            a = b
            b = c
            if b > last:
                break
            if b % 2 == 0:
                yield b

This week, a more elegant way of doing the above was described by XY,
where the one generator was split into three simple generators,
one that generates fibonacci numbers, one that yields only even numbers,
and lastly, one that limits how much is output.
Each generator does only one thing, and does it well.
The generators are general purpose and can be combined with other
generators. This is much like the Unix philosophy and using Unix pipes.

    def gen_fibonacci():
        a, b = 0, 1
        while True:
            a, b = b, a + b
            yield b

    def gen_even(gen):
        for i in gen:
            if i%2 == 0:
                yield i

    def gen_lte(gen, n):
        for i in gen:
            if i > n:
                break
            yield i

    print [i for i in gen_lte(gen_even(gen_fibonacci()), 1000)]

Now it's easy to combine generators in different ways.

    print [i for i in gen_lte(gen_fibonacci(), 1000)]

    def gen_n(gen, n):
        for i in gen:
            if n <= 0:
                break
            yield i
            n -= 1

    print [i for i in gen_n(gen_fibonacci(), 10)]

    print [i for i in gen_n(gen_even(gen_fibonacci()), 10)]

or as dabeaz might do:

    fibs = gen_fibonacci()
    evens = gen_even(fibs)
    lte = gen_lte(evens, 1000)
    print [i for i in lte]

dabeaz: Daved M. Beazley
Generator Tricks for Systems Programmers - Version 2.0
http://www.dabeaz.com/generators-uk/index.html
nested generators
use like pipes
redo my euler #2 to nest even generator with fib generator

Tuple unpacking simplified:

    c = a + b
    a = b
    b = c

to:

    a, b = b, a + b

Euler 44

decoding QR codes
http://qrcode.sourceforge.net/
code.google.com/p/zxing/

beautiful soup html parsing library
if you are using it you should stop and start using html5lib
https://github.com/html5lib
https://github.com/iynaix/zoopshop/ (private)

github.com
t shortcut filters (if javascript enabled)
rebasing

everytimezone.com

arch linux in a vm
genfstab
aur arch user repositories

od
xxd -g 1 -u

break out of two loops
def iter_foo():
    for x in a:
        for y in b:
            yield value

http://docs.python.org/2.7/library/itertools.html#itertools.product
andrewdavidson.com/gibberish/?companyname=Acme

millersville.edu/~bikenaga/math-proof/cardinality/cardinality.html

library books

unicorns
    chuck norris only eats unicorns
    oddee.com/item_98571.aspx
    robocop on a unicorn

Physics for Game Developers 2nd Ed $22.49 at Half Price Books on Bethel
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: dojo-20130607.ipynb
URL: <http://mail.python.org/pipermail/centraloh/attachments/20130610/dd659348/attachment.ksh>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: euler44-end-jep.ipynb
URL: <http://mail.python.org/pipermail/centraloh/attachments/20130610/dd659348/attachment-0001.ksh>


More information about the CentralOH mailing list