Aae34a7973a8d98e53764a1c89090c55

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#! /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 !

0d168f46860cd85e8fde51ce611bf5cb

Will Ware

October 3, 2007, October 03, 2007 10:22, permalink

No rating. Login to rate!

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#! /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()
B4e2590d3e9900d359fdfcc96cbb040b

gioby

November 15, 2007, November 15, 2007 23:19, permalink

No rating. Login to rate!

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..:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#! /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()

Your refactoring





Format Copy from initial code

or Cancel