Tools for A successful project development

 Domen Kožar@iElectric




EuroPython 2013

Hi!

  • Telecommunications student (5th year) in Ljubljana, Slovenia
  • Python / JavaScript freelancer
  • Web developer (Pyramid / Plone / Pylons / Flask / Django)
  • Active member of Kiberpipa hackerspace
  • Beer, skiing and kitesurfing (wannabe) addict

Opinionated best practices

by Kenneth Reitz

"common knowledge"

DEVELOPment ENVIRONMENT

FLAKE8

static analysis of your code to catch mistakes early 

pyflakes + pep8 == flake8


$ pip install flake8
$ flake8 src/


Ignoring warnings


$ cat setup.cfg
[flake8]
ignore = E123,E127,E121
# E123: closing bracket must match indent of starting bracket
# E127 continuation line over-indented for visual indent
# E121 continuation line indentation is not a multiple of four

Inline:

>>> print locals()  # noqa

Bonus: editor integration



Sphinx

The documentation tool for Python

$ pip install sphinx
$ sphinx-quickstart
$ make html
$ make pdf

outline is important!

example:
  • Why another tool
  • User Guide
  • Developer Guide
  • API
  • Changelog

Bonus: DRY versions

>>> import pkg_resources
>>> pkg_resources.get_distribution('foobar').version 1.0

readthedocs.org


Hosting your documentation
Automate (github hooks)!

Testing

unittest2 (python 2.6 and lower)
mock
nose
coverage
travis-ci (jenkins)
webtest

nose

a test runner with batteries included

$ pip install nose
$ nosetests


Nose (cont.)

nosetests --with-coverage 
                                                    --cover-package=mypackage 
                                                 --cover-min-percentage 100

nosetests --doctests-tests

nosetests --pdb --pdb-failures

$ cat setup.cfg
[nosetests]
with-coverage=1
cover-package=mypackage
cover-min-percentage=100
cover-erase=1

nose-selecttests


$ pip install nose-selecttests

$ nose -t foobar

Travis-CI


Automate everything!
Hosted (!) continuous integration testing

 $ cat .travis.yml
language: python
python:
  - 2.6
  - 2.7
  - 3.2
  - 3.3
  - pypy
install:
  - python setup.py dev
script:
  - make test -k

Bonus: coveralls

Collect code coverage statistics through time

travis-ci configuration:
after_success:
  - coveralls

Webtest


blackbox (functional) testing of WSGI apps

>>> from webtest import TestApp
>>> app = TestApp(wsgi_application)
   >>> response = app.get('/')
>>> response.mustcontain('')
>>> assert response.status_int == 200
   
>>> response2 = response.click('Submit')
>>> response2.html.find_all('a')
[]
django-webtest / zope.testbrowser

Bpython

enhanced python interpreter

  • In-line syntax highlighting.
  • Readline-like autocomplete with suggestions displayed as you type.
  • "Rewind" function to pop the last line of code from memory and re-evaluate.
  • Send the code you've entered off to a pastebin or a file
  • Python 3 support.
  • import bpdb

$ pip install bpython && bpython

Packaging tips

$ cat setup.py
extras_require={
    'test': [
        'nose',
        'nose-selecttests',
        'coverage',
        'unittest2',
        'flake8',
    ],
    'development': [
        'zest.releaser',
        'check-manifest',
    ],
},
$ cat setup.cfg
[aliases]
dev = develop easy_install mypackage [test,development]

Makefiles

don't be afraid of the Makefile!

.PHONY: nosetests test flake8
all: bin/python test test: nosetests flake8
bin/python: @virtualenv . @bin/python setup.py dev nosetests: @echo "==== Running nosetests ====" @nosetests flake8: @echo "==== Running Flake8 ====" @flake8 mypackage *.py

ZEST.RELEASER

Release  your code to pypi.python.org - the easy way

$ pip install zest.releaser
$ pip install check-manifest
$ bin/fullrelease
  • confirm release version
  • show diff of changes (also updates changelog)
  • see if files are not in Manifest.in
  • tag it in revision control
  • checkout the tag and upload to pypi
  • add new changelog entry
  • increment version and append ".dev0"

MR.BOB

$ pip install mr.bob

$ mr.bob -O project_name bobtemplates.ielectric:templates/pyramid

Welcome to mr.bob interactive mode. Before we generate directory structure, some questions need to be answered.

Answer with a question mark to display help.
Values in square brackets at the end of the questions show the default value if there is no answer.


--> Dotted name of the package?: projectname.foobar

...

Generated file structure at /home/ielectric/dev/foo

Why another tool?

  • Python 3 support
  • minimalistic code (~200 lines)
  • uses config file for questions schema
  • ability to use templates with HTTP and folders
  • Jinja2 renderer (can be replaced)

bobtemplates.ielectric

  • basic python package
  • pyramid layout

Python 3

So I know you have:

  • tests
  • automated tests



Any other excuse not to write Python 3 compatible code with six?

    Demo time!


    gittip


    Gittip is a way to give small weekly cash gifts to people you love and are inspired by.

    QUESTIONS?




    @iElectric / domen@dev.si




    (made with https://slid.es/)

    Tools for successful project development

    By ielectric

    Tools for successful project development

    • 3,558