Software development practices in python
-
Upload
jimmy-lai -
Category
Technology
-
view
610 -
download
3
description
Transcript of Software development practices in python
![Page 1: Software development practices in python](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bb772b4c90530298b48ae/html5/thumbnails/1.jpg)
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
![Page 2: Software development practices in python](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bb772b4c90530298b48ae/html5/thumbnails/2.jpg)
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
![Page 3: Software development practices in python](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bb772b4c90530298b48ae/html5/thumbnails/3.jpg)
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
![Page 4: Software development practices in python](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bb772b4c90530298b48ae/html5/thumbnails/4.jpg)
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
![Page 5: Software development practices in python](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bb772b4c90530298b48ae/html5/thumbnails/5.jpg)
Source Code Management Version Control - Mercurial
• Distributed repositories
• Common commands:
$ hg clone ssh://[email protected]/noahsark/slideshare
$ hg commit
$ hg push
$ hg fetch
• Useful hooks: http://pypi.python.org/pypi/hghooks/
Ignore some files shown in `hg status`
File: .hgignore
syntax: glob
*.pyc
*.swp
docs/build
Software Development Practice in Python 5
![Page 6: Software development practices in python](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bb772b4c90530298b48ae/html5/thumbnails/6.jpg)
Source Code Management Unit Test - nose
• nose
– Simpler and easier than unittest
– Nose tests in XXX_test.py and functions named test_XXX
$ pip install nose
# install
$ nosetests
# nose tests and execute
Software Development Practice in Python 6
def setup(): global test_case_1 test_case_1 = (1, 2) def teardown(): pass def adder(a, b): return a + b @with_setup(setup, teardown) def test_adder(): expected = 3 actual = adder(test_case_1[0], test_case_1[1]) nt.assert_equal(actual, expected)
![Page 7: Software development practices in python](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bb772b4c90530298b48ae/html5/thumbnails/7.jpg)
Source Code Management Unit Test - doctest
• Doctest
– Write test in the docstring of function
– Easy and simple
– The test case is a usage example and appears in documents.
• http://docs.python.org/2/library/doctest.html
def adder(a, b):
'''
>>> adder(2, 3)
5
'''
return a + b
Software Development Practice in Python 7
![Page 8: Software development practices in python](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bb772b4c90530298b48ae/html5/thumbnails/8.jpg)
Source Code Management Unit Test – Test Coverage
• Test is the shield of code. Test coverage is the percentage of code be covered by test cases.
• Coverage rate over 80% is good.
$ pip install coverage
$ nosetests --with-coverage
# run coverage by nose
Name Stmts Miss Cover Missing
-------------------------------------------------------
python_demo 0 0 100%
python_demo.fabfile 23 15 35% 13-14
python_demo.my_operator 6 0 100%
-------------------------------------------------------
TOTAL 29 15 48%
Software Development Practice in Python 8
![Page 9: Software development practices in python](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bb772b4c90530298b48ae/html5/thumbnails/9.jpg)
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
![Page 10: Software development practices in python](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bb772b4c90530298b48ae/html5/thumbnails/10.jpg)
Source Code Management Code Duplication - clonedigger
• Duplicated codes should be refactored as reusable module
$ pip install clonedigger
$ clonedigger .
$ firefox output.html
Software Development Practice in Python 10
Output example
![Page 11: Software development practices in python](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bb772b4c90530298b48ae/html5/thumbnails/11.jpg)
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
![Page 12: Software development practices in python](https://reader033.fdocuments.us/reader033/viewer/2022052822/554bb772b4c90530298b48ae/html5/thumbnails/12.jpg)
Automation - fabric
• Automation reduces repeating efforts.
• Things you do for the second time is worth to automate.
$ pip install fabric
• Common tasks suitable for automation:
– Build executable
– Build document (fab doc)
– Run unit tests (fab test)
– Setup Runtime Environment (fab setup)
– Deploy
• See fabfile.py for more examples.
Software Development Practice in Python 12