Download writing idiomatic python 2 7 3 or read online books in PDF, EPUB, Tuebl, and Mobi Format. Click Download or Read Online button to get writing idiomatic python 2 7 3 book now. This site is like a library, Use search box in the widget to get ebook that you want. R/Python: news about the dynamic, interpreted, interactive, object-oriented, extensible programming. Tips and tricks for writing idiomatic and Pythonic code.
title: The Writing Idiomatic Python Book is Finally Available!date: 2013-01-24 11:52categories: python idiomatic book software
It took far more effort and time than I ever anticipated, but the Writing Idiomatic Python eBookis finally available! It's in 'beta' mode right now, meaning I'm still planningon adding more content over the next month, but if you get it today you'llautomatically get all of the updates (and corrections) for free. I reallybelieve that the book will be of use to both those new to Python and thoselooking to increase their Python mastery.
Interestingly, the book has its own automated build and test process, and it'sthe most comprehensive I've ever used on a Python project. As the book isprimarily comprised of code samples, regression testing is an absolute must. I'musing pytest to implement the tests themselves. Ifound it a bit more flexible than nose in terms of deciding whichdirectories/files/functions should be searched for tests. I'm also usingthe
coverage
package to make sure all of the code samples are actually beingtested properly.Since there are two different versions of the book (one for Python 2.7.3+and one for Python 3.3+), tox is usedto test each version of Python against the non-version specific tests plusthose specific to version of Python being used. tox is incredibly flexible,which has been vital as my 'project' is much different than most other Pythonprojects.
Building the book
After the tests complete, a custom Python
generate
script traverses eachdirectory and process the .py files it finds. Section headings are stored in__init__.py
files, while individual idioms are normal Python files. For eachidiom, the file's docstring represents the idiom's description and analysis (written in Markdown). This is followed by two functions: test_harmful
andtest_idiomatic
, which contain the actual sample code.The
generate
script extracts the code from the two functions just mentionedas code objects and does a bit of post-processing (stripping out doctest relateddocstrings and pytest assertions). The sample code often uses non-existentfunctions and classes for illustrative purposes, but these need to exist inorder to test the samples. 'Helper' code implements the non-existent classes andfunctions in such a way that the sample code both runs and gives sensiblevalues. The helper code for each idiom resides in that idiom's file and isstripped out by the generate
script.An example idiom
To make the above a bit more clear, here's the full text of a sample file for asingle idiom (in this case named
use_else_to_determine_when_break_not_hit.py
):You may notice in the description that some terms are surrounded by a single ` and othersby two. This is used for the purposes of building the index at the end of thebook. Anything with two `'s is both formatted as inline code and marked as anoccurrence of that term for the index. A single ' is similarly formatted but notindexed (so useless phrases like
has_malformed_email_address
don't appear inthe index). The generate
script takes the appropriate action based on whichstyle of backquote is used.So after all of these steps are completed, the generate script produces a singleMarkdown file that represents the complete text of the book, properly formatted.This is then run through pandoc with acustom latex template to produce a '.latex' file. This gets run through
xelatex
so that the index may be generated, makeindex
is used toactually build the index, and xelatex
is run again to produce thefinal PDF document.In retrospect...
Of course, none of this infrastructure existed when I started. I had no idea howI was going to write the prose and test the code at the same time. I had no ideahow PDF files could be created. I had never used latex in any form. All of theabove just gradually grew out of necessity. Looking at it all now, I'm amazed Ihad the patience to set it all up since none of it is evident in the finalproduct (the book).
You can imagine, then, the pace at which the book has been written. I have afull time job, so work was done in hours stolen from evenings and weekends. Mywife has been more supportive of this than I deserve, but is glad that it'squite close to ending. I have a new found respect for those that write technicalbooks for a living. It is a mentally and emotionally draining process.
In the end, though, all that matters is the following: I set out to write a bookthat newcomers to Python would find helpful, I worked on it whenever I could,and I actually finished it. The last part is the key. I have started andabandoned scores of projects, as I'm sure many of you have. This time, though,I persevered. This time I finished. Even if no one actually buys the book, I still got through the process of writing it.
That's worth quite a lot to me.