Software development practices in python
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Software development practices in python

on

  • 862 views

In this slides, the author demonstrates many software development practices in Python. Including: runtime environment setup, source code management, version control, unit test, coding convention, code ...

In this slides, the author demonstrates many software development practices in Python. Including: runtime environment setup, source code management, version control, unit test, coding convention, code duplication, documentation and automation.

Statistics

Views

Total Views
862
Views on SlideShare
862
Embed Views
0

Actions

Likes
0
Downloads
13
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Software development practices in python Presentation Transcript

  • 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. Outline1. Runtime Environment: virtualenv and pip2. Source Code Management: – Version control: Mercurial – Unit test: code coverage • doctest • nosetests – Coding convention: PEP8 – Code duplication: clonedigger3. Documentation: Sphinx4. Automation: fabric Software Development Practice in Python 2
  • 3. Demo Code$ hg clone https://bitbucket.org/noahsark/slideshareDemo 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: .hgignoressh://hg@bitbucket.org/noahs syntax: globark/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