Thursday, October 18, 2012

Python Web Frameworks PEP8 Consistency

The code is read much more often than it is written. The PEP8 guidelines are intended to improve the readability of code. Readability counts, no doubt, but readability consistency is important, it is equally important to know when to be inconsistent. The report below makes excuse for the following:
E501 line too long (> 79 characters)
E231 missing whitespace after ',:'
W291 trailing whitespace
W293 blank line contains whitespace
In this post we will take a look at web frameworks source code readability. The ratio between a web framework total python source lines to PEP8 errors found represents PEP8 error rate in respectful framework:
  1. bottle
  2. cherrypy
  3. circuits
  4. django
  5. flask
  6. pyramid
  7. pysi
  8. tornado
  9. turbogears
  10. web.py
  11. web2py
  12. wheezy.web
The source code is hosted on bitbucket, let clone it into some directory and setup virtual environment (this will download source code per framework listed above).
hg clone https://bitbucket.org/akorn/helloworld
cd helloworld/04-pep8 && make env up
The make file has a target for each metric, so in order to gather pep8 count issue make pep8, or make count to count total lines.
Since some web frameworks consist of several packages (developed by the same team), I have combined them this way:
flask += jinja2 + werkzeug
pyramid += chameleon + webob
Here are raw numbers (as of January 21, 2013):
bottle =>   6812 
chameleon =>  10183 
cherrypy =>   34117 
circuits =>  20947 
django =>  201506 
flask =>  11473 
jinja2 =>  16533 
pyramid =>   53205 
pysi =>  1177 
tornado =>   26675 
turbogears =>  10656 
web2py =>  109758 
webob =>  17336 
webpy =>  12685 
werkzeug =>  27915 
wheezy.caching =>  2978 
wheezy.core =>   3444 
wheezy.html =>   3469 
wheezy.http =>   5891 
wheezy.routing =>  1819 
wheezy.security =>   867 
wheezy.template =>  3309 
wheezy.validation =>  1957 
wheezy.web =>   6602

number of PEP8 errors or warnings:
bottle => 757
chameleon => 266
cherrypy => 1121
circuits => 740
django => 10168
flask => 429
jinja2 => 406
pyramid => 3544
pysi => 184
tornado => 301
turbogears => 655
web2py => 7975
webob => 1398
webpy => 855
werkzeug => 760
wheezy.caching => 0
wheezy.core => 0
wheezy.html => 0
wheezy.http => 0
wheezy.routing => 0
wheezy.security => 0
wheezy.template => 0
wheezy.validation => 0
wheezy.web => 0
Python has a number of web frameworks. A trivial PEP8 readability check gives you an idea where particular web framework stands in terms of internal quality. There is a wide field for improvement.

6 comments:

  1. "But most importantly: know when to be inconsistent -- sometimes the style guide just doesn't apply. When in doubt, use your best judgment. Look at other examples and decide what looks best."

    Not sure PEP8 is really a benchmark for quality, rather just a benchmark for conforming to a certain design.

    Although i do applaud your efforts to make wheezy conform, not really something that has to be boasted about given the above PEP8 paragraph.

    ReplyDelete
    Replies
    1. That should be an exception rather than rule, in my post I excluded 4 most controversial. If you believe there should be more `special` cases for exclusion, please let me know.

      Delete
    2. Would be interesting to see how Python itself conforms to PEP8...

      Delete
    3. Python 2.7.3 (maintenance only): 602678 lines, 81291 pep8 errors, 0.13 error ratio.
      Python 3.3.0 (future): 572,749 lines, 62,447 pep8 errors, 0.11 error ratio.

      Delete
    4. So Python's "internal quality" is bad?

      Delete
    5. I would say inconsistent, however the trend shows improvement in future versions.

      Delete