[CentralOH] 2016-06-27 會議 Scribbles 落書/惡文?: Generatorpalooza I: nested loop word counting nested generators verbose regexes 'C:\new\test' project euler #1 refactoring primes continue statement recursive permutations gcd in musical round 364 twelve days of christmas hypothesis

jep200404 at columbus.rr.com jep200404 at columbus.rr.com
Wed Jul 6 18:03:04 EDT 2016


Todo:

    register with PyOhio!

    July 7th is last day to get T-shirts.

Generatorpalooza!

    This month focused on generators.

    Code is posted on https://github.com/cohpy/challenge-201605-generators.
    Three Pythonistas submitted code.

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

James Prior presented:

    A technique for breaking out of nested loops
    by using a generator to yield tuples of values.

        http://nbviewer.jupyter.org/github/cohpy/challenge-201605-generators/blob/master/james-prior/b-break-out-of-nested-loops.ipynb

        Someone from Pillar mentioned another way of breaking out of nested
        loops by using a return statement.

    A word counting program rewritten to use generators
    instead of appending to lists.

        lists: https://github.com/cohpy/challenge-201605-generators/blob/master/james-prior/wordpop.py.0-lists.2e522fe748e20ee031749cbd504f0c46600b480d
        (copy of https://github.com/folkengine/whats-the-frequency-kenneth/blob/master/wordpop.py)

            A typical function pattern for lists was:

                def foo(lines):
                    ...
                    new_stuff = []
                    for line in lines:
                        new_stuff.append(bar(line))
                    ...
                    return new_stuff

        generators: https://github.com/cohpy/challenge-201605-generators/blob/master/james-prior/wordpop.py.1-gen.0d534ed5a17f41b36939f14a658736d015e085f9

            Oddly, most of the functions used "yield from" statements,
            not mere "yield" statements.

            A classic form was shown in the following function:

                def get_words_from_lines(lines):
                    for line in lines:
                        yield from line.lower().split()

            One function was particularly tricky:

                def strip_header(lines):
                    for line in lines:
                        if line.startswith(TEXT_BOUNDARY):
                            yield from lines

                The trickiness is that lines is referred to twice,
                first as the iterable in the for statement,
                then as the iterable in the yield from statement.
                The yield from lines statement yields lines that have
                not already been iterated through in the for statement.

                Is this _too_ tricky, or is an idiom that one should master?

    Using shell pipe syntax for nested generators.

        http://nbviewer.jupyter.org/github/cohpy/challenge-201605-generators/blob/master/james-prior/8-nested-generators-20160626-1920.ipynb

        First showed how one ugly, complicated, hard-to-read generator was
        split into three simple generators that each do one thing well.

        But nesting of generators can be hard to read. Reading of nested
        generators is generally backwards, i.e., from right to left, with
        matching of parameters needing ones eyes to search back and forth.

        "This is not a pipe."

        Showed alternative syntax similar to that of UNIX shell.
        Showed code to do it.

        Compare with http://coconut-lang.org/

            There is a whole 'nuther thing.

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

Travis Risner presented on regular expressions (regexes) in Python
as a dry run for presenting at PyOhio.

    He showed the verbose mode of coding regular expressions.

        https://docs.python.org/3/library/re.html#re.VERBOSE

        verbose mode is nifty

    Compare:

        filename = 'C:\new\test'
        filename = r'C:\new\test'

    Study:

        cohpy at forge:~$ python3
        Python 3.4.3 (default, Oct 14 2015, 20:33:09)
        [GCC 4.8.4] on linux
        Type "help", "copyright", "credits" or "license" for more information.
        >>> s = 'C:\new\test'
        >>> len(s)
        9
        >>> s = r'C:\new\test'
        >>> len(s)
        11
        >>>
        cohpy at forge:~$

    Study by searching for "raw"
    in https://docs.python.org/3/reference/lexical_analysis.html.

    For filenames in Microsoft Windows,
    avoid the backslash issues by not using them..
    Python groks the following just fine in Microsoft Windows:

        for line in open('C:/new/test')

    Raw strings are still handy for regular expressions.

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

Joe Friedrich presented his code which solves Problem #1 of Project Euler:

    https://github.com/cohpy/challenge-201605-generators/tree/master/joefriedrich

    Travis showed his solutions for the same problem,
    using a variety of techniques.

    Then Jim showed his solutions for the same problem,
    using even more techniques,
    including one that directly calculated the answer and
    and was smoking fast, even for extremely large numbers.
    Look at the output of the last cell in

        http://nbviewer.jupyter.org/url/colug.net/python/all-ipython-notebooks/euler-001-multiples-of-3-and-5-20150220.ipynb

    Search for euler-001 on http://colug.net/python/all-ipython-notebooks/.
    You will find other versions.

    Joe, Travis, and Jim all reinvented the wheel many times in solving Problem
    #1 of Project Euler. That's fine. It's good for learning.
    When you do not have an itch to scratch, the Project Euler problems are
    good to practice on. Many of the problems, especially the early ones,
    are well known to many. So they can be discussed and compared.

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

back to James Prior

    Refactoring of a function that calculated December meeting dates.
    The refactored code happened to have one generator function
    and two functions that returned generator expressions.

        original code:

            https://github.com/cohpy/challenge-201605-generators/blob/master/james-prior/cell01-original.py

        pretty refactored code:

            https://github.com/cohpy/challenge-201605-generators/blob/master/james-prior/cell53-pretty.py

        distilled essence of nastiness code:

            https://github.com/cohpy/challenge-201605-generators/blob/master/james-prior/cell65-nasty.py

        step by step refactoring:

            http://nbviewer.jupyter.org/github/cohpy/challenge-201605-generators/blob/master/james-prior/5-dojo-20160429-2016-Mar-COhPy_Challenge_Rough-20160625-1612.ipynb

            This refactors the code,
            changing one little thing at a time.
            This shows each little step in great detail.
            This is for novices to study.

    primes

        Reviewed how continue statement can be used to reduce need for
        indenting code.

        http://nbviewer.jupyter.org/github/cohpy/challenge-201605-generators/blob/master/james-prior/2-primes.ipynb

        Optimization with square root applies to sieve of eratosthenes also.
        That was missing from the above code.

    permutations

        The interesting part was that it was recursive.

        http://nbviewer.jupyter.org/github/cohpy/challenge-201605-generators/blob/master/james-prior/3-permutate.ipynb

    pythagorean triples

        The interesting part were the three versions of gcd()
            (greatest common divisor)
        The gcd() in cell #5 is interesting for not copying any data.
            wp:Three Blind Mice
            wp:Round (music)
        and use of reduce() to apply gcd to three numbers.

        The wheel was reinvented (again).
        https://docs.python.org/3.5/library/math.html#math.gcd
        Notebook ran Python 3.4.3, so it was not available.

        http://nbviewer.jupyter.org/github/cohpy/challenge-201605-generators/blob/master/james-prior/4-pythagorean-triples.ipynb

    simple "yield from" examples

        Can apply yield from with just about any iterable.
        Applying it to open('threelines.txt') was surprising.

        http://nbviewer.jupyter.org/github/cohpy/challenge-201605-generators/blob/master/james-prior/a-yield-from.ipynb

    Twelve Days of Christmas

        wp:On-Line Encyclopedia of Integer Sequences

            oooo aaahhhh!!!

        generates numerical sequences with

        repeat 1s
        count
        triangular numbers
        tetrahedral numbers
            364 total gifts for Twelve Days of Christmas
        what is the name for the accumulation of tetrahedral numbers?
            oeis.org/A000332

        wp:Epiphany season

    not presented:

        http://nbviewer.jupyter.org/github/cohpy/challenge-201605-generators/blob/master/james-prior/1-range.ipynb
        http://nbviewer.jupyter.org/github/cohpy/challenge-201605-generators/blob/master/james-prior/7-iter_date-revisited.ipynb

not done

    music generator
        wp:David A. Jaffe
        wp:Music Kit
        wp:NeXT

Staff Sergeant Christopher L. Brown Memorial Highway
http://codes.ohio.gov/orc/5533.052

BoxOfMusic channel on YouTube
eric stoddard??? stocker???

wp:French Republican Calendar

wp:Stardate

wp:Perfluorooctanoic acid
    Document Dump: Lawsuit Reveals Extent Of DuPont’s C8 Cover Up
    http://www.desmogblog.com/2016/06/19/document-dump-lawsuit-reveals-extent-dupont-s-c8-cover
    http://www.aboutlawsuits.com/dupont-c8/

quickcheck-ci.com shrinkage
    free for open source
    shrinkage:
        start with many randomly generated tests
        prune boring tests, concentrating on problematic ones

hypothesis for python
    how does it compare with pytest?
    https://pypi.python.org/pypi/hypothesis

'I urge everyone to fight back' – woman wins $10k from Microsoft over Windows 10 misery
http://www.theregister.co.uk/2016/06/27/woman_microsoft_windows_10_upgrades/

boots of spanish leather
positively fourth street

Sony Settles in Linux Battle
http://www.linuxjournal.com/content/sony-settles-linux-battle

piperize

    how to make work with:

        generators that expect iterable as first argument
        generators that expect iterable as second argument
        generators that use send/yield
            does that work with things where
            more is yielded than consumed or vice versa?

    perhaps use different decorators (that are compatible with each other)
        @piperize_first_arg_iterable
        def foo(iterable, *args): ...

        @piperize_second_arg_iterable
        def goo(x, iterable, *arg): ...

        @piperize_send
        def hoo(*args): ... (where hoo() uses send)

    perhaps use different wrapper classes?
        F(foo(iterable, *args))
        G(goo(x, iterable))
        H(hoo(*args)) (where hoo() uses send)

to maybe do:

    permutate:
        try:
            copy then delete

    chris-b
        redo nested generators to use shell syntax

    apply piperize to cell #65 of refactored joe

    pyflakes
    pylint
    tests
        pytest
        hypothesis?????

    cohpy website gmt issue


More information about the CentralOH mailing list