Next / Previous / Contents / TCC Help System / NM Tech homepage

46. Test driver for singleton

singtest
#!/usr/bin/env python
#================================================================
# Test driver for the Singleton class.
#
# Do not edit this file.  It is extracted automatically from the
# documentation:
#   http://www.nmt.edu/tcc/help/lang/python/examples/logscan/
#----------------------------------------------------------------

from singleton import *

# - - -   m a i n

def main():
    '''Test the Singleton class.
    '''
    d1 = D('one')
    print d1
    d2 = D('two')
    print d2

# - - - - -   c l a s s   D

class D(Singleton):
    '''Singleton test object.

      Exports:
        D(s):
          [ s is some string ->
              if D has been instantiated before ->
                  return the previous instance with its
                  .last attribute set to s
              else ->
                  return a new instance of D with its .first
                  and .last attributes set to s ]
        .first:  [ argument to D() on first call ]
        .last:   [ argument to D() on latest call ]
        .__str__(self):  [ display self as a string ]
    '''
    everCalled = False

    def __init__ ( self, s  ):
        if D.everCalled:
            self.last = s
        else:
            D.everCalled = True
            self.first = self.last = s

    def __str__ ( self ):
        return "D.first='%s' .last='%s'" % (self.first, self.last)



#================================================================
# Epilogue
#----------------------------------------------------------------
if __name__ == '__main__':
    main()