|
We try to define here coding conventions for Python coding The guidelinesClassesWhen creating a new: - class: always use a capital letter;
- data attribute: always use a noun;
- method attribute: always use a verb;
Pydoc - Enable use of man pagesEvery python script or module should be "pydoc" acceptant. > pydoc module Doctest - Enable unit-testingWhenever a new class is created, make sure you get a UnitTest programmed so you can test the classe. The philosophy is Debugging sucks, Testing Rocks. - 50% coding program,
- 50% coding unittests.
> python module.py -v iPython - Quickly inspect the modulesUsing iPython as a command-line environment instead of the default python is really interesting because it allows quick-inspection of modules by tabbing away (using tab will auto-complete the methods in the classes and other objects). The TemplateThe code (python)#! /usr/bin/python
""" A python primer. pydoc, doctest and classes.
Syntax:
To generate man pages:
> pydoc classtemplate
To try all the tests in doctest.
> python classtemplate.py -v
"""
#Class definition
class Table:
"""Class Table.
Doctest: here we insert python command lines inputs and outputs.
>>> print Table.database
http://access.com/db
"""
#Data attributes here
database='http://access.com/db'
#Method attributes here
def __init__(self,id,text):
"""Initializes the id and textlabel data attributes.
Doctest: Testing more data attributes defined at construction time
>>> x = Table(1,'coucou')
>>> print x.id
1
>>> print x.textlabel
coucou
"""
self.id=id
self.textlabel=text
#doctest -- "Debugging sucks :( Testing rocks :)"
def _test():
"""Inline Doctest activated. Cool! :D
This means that whenever the module is called in python
> python thismodule.py -v
the doctest function will try all the tests implemented in doctest.
"""
import doctest
doctest.testmod()
if __name__ == "__main__":
_test()The man page (pydoc) > pydoc classtemplate Help on module classtemplate:
NAME
classtemplate - A python primer. pydoc, doctest and classes.
FILE
/users/left/guillaume/python/classtemplate.py
DESCRIPTION
Syntax:
To generate man pages:
> pydoc classtemplate
To try all the tests in doctest.
> python classtemplate.py -v
CLASSES
Table
class Table
| Class Table.
|
| Doctest: here we insert python command lines inputs and outputs.
| >>> print Table.database
| http://access.com/db
|
| Methods defined here:
|
| __init__(self, id, text)
| Initializes the id and textlabel data attributes.
|
| Doctest: Testing more data attributes defined at construction time
| >>> x = Table(1,'coucou')
| >>> print x.id
| 1
| >>> print x.textlabel
| coucou
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| database = 'http://access.com/db'
The tests results (doctest) > python classtemplate.py -v Trying:
print Table.database
Expecting:
http://access.com/db
ok
Trying:
x = Table(1,'coucou')
Expecting nothing
ok
Trying:
print x.id
Expecting:
1
ok
Trying:
print x.textlabel
Expecting:
coucou
ok
2 items had no tests:
__main__
__main__._test
2 items passed all tests:
1 tests in __main__.Table
3 tests in __main__.Table.__init__
4 tests in 4 items.
4 passed and 0 failed.
Test passed.
|
Hey guys, I wrote a wiki with a prototypical python template using pydoc, doctest and classes.