Software Development Practices          in Python                       Jimmy Lai               r97922028 [at] ntu.edu.tw ...
Outline1. Runtime Environment: virtualenv and pip2. Source Code Management:  – Version control: Mercurial  – Unit test: co...
Demo Code$ hg clone https://bitbucket.org/noahsark/slideshareDemo Codes are in python_demo dir.Files:  1.   my_operator.py...
Runtime Environment - virtualenv• Isolates runtime environment dependency of  your python project.$ virtualenv --no-site-p...
Source Code Management            Version Control - Mercurial• Distributed repositories                      Ignore some f...
Source Code Management              Unit Test - nose• nose                                  def setup():                  ...
Source Code Management             Unit Test - doctest• Doctest                                  def adder(a, b):         ...
Source Code Management          Unit Test – Test Coverage• Test is the shield of                Name                     S...
Source Code Management       Coding Convention – PEP8• http://www.python.org/dev/peps/pep-0008/• Code layout consistency: ...
Source Code Management     Code Duplication - clonedigger• Duplicated codes should be refactored as  reusable module$ pip ...
Documentation - Sphinx• Document is important for reuse• Document type:  – Tutorial, system architecture  – Functions, mod...
Automation - fabric• Automation reduces                    • Common tasks suitable  repeating efforts.                    ...
Upcoming SlideShare
Loading in...5
×

Software development practices in python

713

Published on

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.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
713
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Software development practices in python

  1. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×