KEMBAR78
Software development practices in python | PDF
Software Development Practices
          in Python
                       Jimmy Lai
               r97922028 [at] ntu.edu.tw
   http://tw.linkedin.com/pub/jimmy-lai/27/4a/536
                      2013/02/14
Outline
1. Runtime Environment: virtualenv and pip
2. Source Code Management:
  – Version control: Mercurial
  – Unit test: code coverage
     • doctest
     • nosetests
  – Coding convention: PEP8
  – Code duplication: clonedigger
3. Documentation: Sphinx
4. Automation: fabric
                   Software Development Practice in Python   2
Demo Code
$ hg clone https://bitbucket.org/noahsark/slideshare
Demo Codes are in python_demo dir.
Files:
  1.   my_operator.py, my_operator_test.py
  2.   fabfile.py




                   Software Development Practice in Python   3
Runtime Environment - virtualenv
• Isolates runtime environment dependency of
  your python project.
$ virtualenv --no-site-packages env_name
$ . env_name/bin/activate
      # enter the virtual environment
$ pip install package_name
      # install python packages

               Software Development Practice in Python   4
Source Code Management
            Version Control - Mercurial
• Distributed repositories                      Ignore some files shown in
• Common commands:                              `hg status`
$ hg clone                                      File: .hgignore
ssh://hg@bitbucket.org/noahs
                                                syntax: glob
ark/slideshare
$ hg commit                                     *.pyc
                                                *.swp
$ hg push                                       docs/build
$ hg fetch
• Useful hooks:
http://pypi.python.org/pypi/hghooks/


                          Software Development Practice in Python        5
Source Code Management
              Unit Test - nose
• nose                                  def setup():
                                          global test_case_1
  – Simpler and easier than               test_case_1 = (1, 2)
    unittest
                                        def teardown():
  – Nose tests in XXX_test.py             pass
    and functions named
    test_XXX                            def adder(a, b):
                                          return a + b
  $ pip install nose
                                        @with_setup(setup, teardown)
      # install                         def test_adder():
  $ nosetests                             expected = 3
                                          actual = adder(test_case_1[0],
      # nose tests and execute          test_case_1[1])
                                          nt.assert_equal(actual, expected)

                    Software Development Practice in Python                   6
Source Code Management
             Unit Test - doctest
• Doctest                                  def adder(a, b):
                                             '''
   – Write test in the                       >>> adder(2, 3)
     docstring of function                   5
   – Easy and simple                         '''
                                             return a + b
   – The test case is a usage
     example and appears in
     documents.
• http://docs.python.org/
  2/library/doctest.html


                     Software Development Practice in Python   7
Source Code Management
          Unit Test – Test Coverage
• Test is the shield of                Name                     Stmts Miss Cover Missing
                                       -------------------------------------------------------
   code. Test coverage is              python_demo                      0 0 100%
   the percentage of code              python_demo.fabfile                 23 15 35% 13-14
   be covered by test cases.           python_demo.my_operator                    6 0 100%
                                       -------------------------------------------------------
• Coverage rate over 80%               TOTAL                     29 15 48%
   is good.
$ pip install coverage
$ nosetests --with-coverage
      # run coverage by nose


                    Software Development Practice in Python                           8
Source Code Management
       Coding Convention – PEP8
• http://www.python.org/dev/peps/pep-0008/
• Code layout consistency:
  – Usage of indentation, space, blank line
  – Naming convention
  – Programming recommendations
• Setup PEP8 checking as mercurial hook to
  ensure the consistency.
  pretxncommit.pep8 = python:hghooks.code.pep8hook

                 Software Development Practice in Python   9
Source Code Management
     Code Duplication - clonedigger
• Duplicated codes should be refactored as
  reusable module
$ pip install clonedigger
$ clonedigger .           Output example

$ firefox output.html




                Software Development Practice in Python   10
Documentation - Sphinx
• Document is important for reuse
• Document type:
  – Tutorial, system architecture
  – Functions, modules explanation
• Sphinx: write docs in reStructuredText
  $ pip install sphinx
  Refer to http://www.slideshare.net/jimmy_lai/documentation-with-sphinx
  for more detail.

                       Software Development Practice in Python             11
Automation - fabric
• Automation reduces                    • Common tasks suitable
  repeating efforts.                      for automation:
• Things you do for the                       – Build executable
  second time is worth to                     – Build document (fab doc)
  automate.                                   – Run unit tests (fab test)
$ pip install fabric                          – Setup Runtime
                                                Environment (fab setup)
                                              – Deploy
                                        • See fabfile.py for more
                                          examples.
                  Software Development Practice in Python              12

Software development practices in python

  • 1.
    Software Development Practices in Python Jimmy Lai r97922028 [at] ntu.edu.tw http://tw.linkedin.com/pub/jimmy-lai/27/4a/536 2013/02/14
  • 2.
    Outline 1. Runtime Environment:virtualenv and pip 2. Source Code Management: – Version control: Mercurial – Unit test: code coverage • doctest • nosetests – Coding convention: PEP8 – Code duplication: clonedigger 3. Documentation: Sphinx 4. Automation: fabric Software Development Practice in Python 2
  • 3.
    Demo Code $ hgclone https://bitbucket.org/noahsark/slideshare Demo Codes are in python_demo dir. Files: 1. my_operator.py, my_operator_test.py 2. fabfile.py Software Development Practice in Python 3
  • 4.
    Runtime Environment -virtualenv • Isolates runtime environment dependency of your python project. $ virtualenv --no-site-packages env_name $ . env_name/bin/activate # enter the virtual environment $ pip install package_name # install python packages Software Development Practice in Python 4
  • 5.
    Source Code Management Version Control - Mercurial • Distributed repositories Ignore some files shown in • Common commands: `hg status` $ hg clone File: .hgignore ssh://hg@bitbucket.org/noahs syntax: glob ark/slideshare $ hg commit *.pyc *.swp $ hg push docs/build $ hg fetch • Useful hooks: http://pypi.python.org/pypi/hghooks/ Software Development Practice in Python 5
  • 6.
    Source Code Management Unit Test - nose • nose def setup(): global test_case_1 – Simpler and easier than test_case_1 = (1, 2) unittest def teardown(): – Nose tests in XXX_test.py pass and functions named test_XXX def adder(a, b): return a + b $ pip install nose @with_setup(setup, teardown) # install def test_adder(): $ nosetests expected = 3 actual = adder(test_case_1[0], # nose tests and execute test_case_1[1]) nt.assert_equal(actual, expected) Software Development Practice in Python 6
  • 7.
    Source Code Management Unit Test - doctest • Doctest def adder(a, b): ''' – Write test in the >>> adder(2, 3) docstring of function 5 – Easy and simple ''' return a + b – The test case is a usage example and appears in documents. • http://docs.python.org/ 2/library/doctest.html Software Development Practice in Python 7
  • 8.
    Source Code Management Unit Test – Test Coverage • Test is the shield of Name Stmts Miss Cover Missing ------------------------------------------------------- code. Test coverage is python_demo 0 0 100% the percentage of code python_demo.fabfile 23 15 35% 13-14 be covered by test cases. python_demo.my_operator 6 0 100% ------------------------------------------------------- • Coverage rate over 80% TOTAL 29 15 48% is good. $ pip install coverage $ nosetests --with-coverage # run coverage by nose Software Development Practice in Python 8
  • 9.
    Source Code Management Coding Convention – PEP8 • http://www.python.org/dev/peps/pep-0008/ • Code layout consistency: – Usage of indentation, space, blank line – Naming convention – Programming recommendations • Setup PEP8 checking as mercurial hook to ensure the consistency. pretxncommit.pep8 = python:hghooks.code.pep8hook Software Development Practice in Python 9
  • 10.
    Source Code Management Code Duplication - clonedigger • Duplicated codes should be refactored as reusable module $ pip install clonedigger $ clonedigger . Output example $ firefox output.html Software Development Practice in Python 10
  • 11.
    Documentation - Sphinx •Document is important for reuse • Document type: – Tutorial, system architecture – Functions, modules explanation • Sphinx: write docs in reStructuredText $ pip install sphinx Refer to http://www.slideshare.net/jimmy_lai/documentation-with-sphinx for more detail. Software Development Practice in Python 11
  • 12.
    Automation - fabric •Automation reduces • Common tasks suitable repeating efforts. for automation: • Things you do for the – Build executable second time is worth to – Build document (fab doc) automate. – Run unit tests (fab test) $ pip install fabric – Setup Runtime Environment (fab setup) – Deploy • See fabfile.py for more examples. Software Development Practice in Python 12