#! /usr/bin/python
""" A python primer. pydoc, doctest and classes.
Syntax:
To generate man pages:
> pydoc classtemplate
To try all the tests in doctest (and execute the script):
> 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()
Refactorings
No refactoring yet !
Will Ware
October 3, 2007, October 03, 2007 10:22, permalink
Here is a trivial example of OO programming. The base class Shape defines some behaviors that all shapes should have (compute my surface area, and draw myself using whatever graphics system is available) and individual shapes (Circle, Rectangle) implement those behaviors. Graphics programming is complicated and depends on what graphics system you're using, so the draw() method is unimplemented.
#! /usr/bin/python
""" A python primer. pydoc, doctest and classes.
Syntax:
To generate man pages:
> pydoc classtemplate
To try all the tests in doctest (and execute the script):
> 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
class Shape:
"""OO Example: base class for 2D shapes, defines area() and draw() methods
"""
def area(self):
raise Exception('Unimplemented for base class')
def draw(self):
# Doing this right depends on what graphics system you're using, what
# API calls are required, how the coordinate system works, etc.
raise Exception('Exercise in graphics programming left to the reader')
class Circle(Shape):
"""
>>> c = Circle(0, 0, 2.0)
>>> c.area()
12.566370614359172
"""
def __init__(self, x, y, radius):
self.x, self.y, self.radius = x, y, radius
def area(self):
import math
return math.pi * self.radius ** 2
class Rectangle(Shape):
"""
>>> r = Rectangle(1, 4, 2, 9)
>>> r.area()
21
"""
def __init__(self, left, right, top, bottom):
self.left, self.right, self.top, self.bottom = \
left, right, top, bottom
def area(self):
width = self.right - self.left
height = self.top - self.bottom
# sign may depend on coordinate system, so use abs()
return abs(width * height)
#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()
gioby
November 15, 2007, November 15, 2007 23:19, permalink
This template is a very nice idea! I have learned how to use pydoc and doctest by looking at it.
I have no real re-factoring tips to implement... but maybe you can play with some of the cool 'private' methods for python classes, like __repr__, __str__, etc..:
#! /usr/bin/python
""" A python primer. pydoc, doctest and classes.
Syntax:
To generate man pages:
> pydoc classtemplate
To try all the tests in doctest (and execute the script):
> 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
>>> print x # test __repr__ method
This is table 1
>>> print x + 'not' # test __add__ method
coucounot
>>> print x[0:2] # test __getitem__ method
cou
"""
self.id = id
self.textlabel = text
def __repr__(self):
return "This is table %s", % self.id
def __add__(self, i):
# This actually doesn't make sense. However, it is an example
return self.textlabel + i
def __getitem__(self, i):
return self.textlabel[i]
#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()
0116 cum swallowing amateur
May 16, 2011, May 16, 2011 09:58, permalink
Three may keep a secret if two of them are dead.
0307 hot girlfriend
May 18, 2011, May 18, 2011 06:09, permalink
Prayer carries us half way to God, fasting brings us to the door of His Palace, and alms-giving procures us admission.
PrivatePilot004
May 14, 2011, May 14, 2011 16:48, permalink
How much have cost us evils that never happened!
Watches071
May 14, 2011, May 14, 2011 21:19, permalink
A man who gives his children habits of industry provides for them better than by giving them a fortune.
Watches612
May 15, 2011, May 15, 2011 13:35, permalink
Better the devil you know than the devil you don't.
Lepnina667
May 15, 2011, May 15, 2011 21:55, permalink
You cannot depend on your eyes when your imagination is out of focus.
Lepnina881
May 15, 2011, May 15, 2011 23:55, permalink
One who is contented with what he has done will never become famous for what he will do, He has lain down to die, and the grass is already over him.
0666 amateur real teens
May 17, 2011, May 17, 2011 08:57, permalink
Wealth is nothing without health.
Strattera Angst
June 5, 2011, June 05, 2011 20:42, permalink
Let’s cross that bridge when we come to it. (Deal with each problem as it arises).
zennoposter twitter
June 19, 2011, June 19, 2011 13:57, permalink
A mio parere, si sono errati. Io propongo di discuterne. Scrivere a me in PM, parlare.
alfuzosin tablets
September 3, 2011, September 03, 2011 17:03, permalink
Ich meine, dass Sie sich irren. Geben Sie wir werden es besprechen. Schreiben Sie mir in PM, wir werden reden.
I'm new to python (and to OO programming in general). But I really enjoyed discovering the pydoc and the unittesting, doctest, concepts. Here's a first draft of a python primer ... Can you improve it?