Thanks to Jeff (and others) for reminding me about doctest.  I did 3 hours
leading a Python training today, for GIS people (ESRI is moving to Python
for its scripting language).  

One of my modules contained this doctest example, which I tweaked to ensure
at least one test failed for the cameras:

def okfloat(anystring):
    """
    Accept a string and say if it's in float format

    >>> okfloat(1.2)
    False
    >>> okfloat('5.692')
    True
    >>> okfloat('abc')
    False
    >>> okfloat('1')
    True
    >>> okfloat([1])  # make True or invent test to force error report
    False
    """
    try:
        # preferred:  assert isinstance(anystring, basestring)
        assert type(anystring)==type('') 
        f = float(anystring)
        return True
    except:
        return False

if __name__ == '__main__':
    import doctest
    doctest.testmod()

All in all, the training went well, but I need to remember that 3 hrs is NOT
enough to cover all basic and advanced features of the language.  We got to
generators, and even decorators, but not, perversely, classes per se.

Kirby


_______________________________________________
Edu-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/edu-sig

Reply via email to