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?

#! /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.

#! /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..:

#! /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()
8719ecc8bacdbee0487d8682db115123

clonecd094

May 9, 2011, May 09, 2011 00:29, permalink

No rating. Login to rate!

That man is idle who can do something better.

F2960194a95e7834b45736d500667645

0116 cum swallowing amateur

May 16, 2011, May 16, 2011 09:58, permalink

No rating. Login to rate!

Three may keep a secret if two of them are dead.

E223501317f72e4e9ed692da75fa11b3

0307 hot girlfriend

May 18, 2011, May 18, 2011 06:09, permalink

No rating. Login to rate!

Prayer carries us half way to God, fasting brings us to the door of His Palace, and alms-giving procures us admission.

512c24b89e60efa8396208576f96eba0

PrivatePilot004

May 14, 2011, May 14, 2011 16:48, permalink

No rating. Login to rate!

How much have cost us evils that never happened!

Ee3a9dfd36253f3d09c3c1756070f557

expediacom534

May 14, 2011, May 14, 2011 18:32, permalink

No rating. Login to rate!

Joys divided are increased.

8ae9d2b465e927cfa983e2af5b0e91db

Watches071

May 14, 2011, May 14, 2011 21:19, permalink

No rating. Login to rate!

A man who gives his children habits of industry provides for them better than by giving them a fortune.

D72029f2c9e290217ec632affc28620f

FishOil466

May 15, 2011, May 15, 2011 00:05, permalink

No rating. Login to rate!

A bad workman quarrels with his tools.

9b77202ca140f9d66f95b1b8dd4c89c0

PrivatePilot755

May 15, 2011, May 15, 2011 09:19, permalink

No rating. Login to rate!

It is the first step that costs.

7cac3d30dd6f760772e29541e858ff5b

Watches612

May 15, 2011, May 15, 2011 13:35, permalink

No rating. Login to rate!

Better the devil you know than the devil you don't.

099bb8b2221eccf0881b0b6966af4239

FishOil012

May 15, 2011, May 15, 2011 16:22, permalink

No rating. Login to rate!

Не that respects not is not respected.

40ae3084824232d5559b2698e053cfa3

Lepnina667

May 15, 2011, May 15, 2011 21:55, permalink

No rating. Login to rate!

You cannot depend on your eyes when your imagination is out of focus.

51e2915effe01201c0beed0521b65b1b

Lepnina881

May 15, 2011, May 15, 2011 23:55, permalink

No rating. Login to rate!

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.

C415516a9fdde83eb0cab4ba58378ef7

0666 amateur real teens

May 17, 2011, May 17, 2011 08:57, permalink

No rating. Login to rate!

Wealth is nothing without health.

B1e576484fda09036b44fd5ad4071d4a

Strattera Angst

June 5, 2011, June 05, 2011 20:42, permalink

No rating. Login to rate!


Let’s cross that bridge when we come to it. (Deal with each problem as it arises).

044d8bf169b6c72925ec5ce44fb027a1

revia

June 12, 2011, June 12, 2011 17:26, permalink

No rating. Login to rate!

Bravo, que frase..., el pensamiento admirable

3c6e725c883e63e09b87f8aa341ee3c1

zennoposter twitter

June 19, 2011, June 19, 2011 13:57, permalink

No rating. Login to rate!

A mio parere, si sono errati. Io propongo di discuterne. Scrivere a me in PM, parlare.

E08eeeae1ed9f46238c889f4b60de9e2

alfuzosin tablets

September 3, 2011, September 03, 2011 17:03, permalink

No rating. Login to rate!

Ich meine, dass Sie sich irren. Geben Sie wir werden es besprechen. Schreiben Sie mir in PM, wir werden reden.

Your refactoring





Format Copy from initial code

or Cancel