Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

running doctests python

python -m doctest -v filename.py 
Comment

doctest example in python

"""
This is the "example" module.

The example module supplies one function, factorial().  For example,

>>> factorial(5)
120
"""

def factorial(n):
    """Return the factorial of n, an exact integer >= 0.

    >>> [factorial(n) for n in range(6)]
    [1, 1, 2, 6, 24, 120]
    >>> factorial(30)
    265252859812191058636308480000000
    >>> factorial(-1)
    Traceback (most recent call last):
        ...
    ValueError: n must be >= 0

    Factorials of floats are OK, but the float must be an exact integer:
    >>> factorial(30.1)
    Traceback (most recent call last):
        ...
    ValueError: n must be exact integer
    >>> factorial(30.0)
    265252859812191058636308480000000

    It must also not be ridiculously large:
    >>> factorial(1e100)
    Traceback (most recent call last):
        ...
    OverflowError: n too large
    """

    import math
    if not n >= 0:
        raise ValueError("n must be >= 0")
    if math.floor(n) != n:
        raise ValueError("n must be exact integer")
    if n+1 == n:  # catch a value like 1e300
        raise OverflowError("n too large")
    result = 1
    factor = 2
    while factor <= n:
        result *= factor
        factor += 1
    return result


if __name__ == "__main__":
    import doctest
    doctest.testmod()
Comment

doctest python

"""
This is the "example" module.

The example module supplies one function, factorial().  For example,

>>> factorial(5)
120
"""

def factorial(n):
    """Return the factorial of n, an exact integer >= 0.

    >>> [factorial(n) for n in range(6)]
    [1, 1, 2, 6, 24, 120]
    >>> factorial(30)
    265252859812191058636308480000000
    >>> factorial(-1)
    Traceback (most recent call last):
        ...
    ValueError: n must be >= 0

    Factorials of floats are OK, but the float must be an exact integer:
    >>> factorial(30.1)
    Traceback (most recent call last):
        ...
    ValueError: n must be exact integer
    >>> factorial(30.0)
    265252859812191058636308480000000

    It must also not be ridiculously large:
    >>> factorial(1e100)
    Traceback (most recent call last):
        ...
    OverflowError: n too large
    """

    import math
    if not n >= 0:
        raise ValueError("n must be >= 0")
    if math.floor(n) != n:
        raise ValueError("n must be exact integer")
    if n+1 == n:  # catch a value like 1e300
        raise OverflowError("n too large")
    result = 1
    factor = 2
    while factor <= n:
        result *= factor
        factor += 1
    return result


if __name__ == "__main__":
    import doctest
    doctest.testmod()
Comment

how to write a python doctest

def a_function(*args):
  '''
  >>>an input
  expected output
  '''
  if this:
    return that
# A doctest may also include a docstring
Comment

The simplest way to start using doctest in python

if __name__ == "__main__":
    import doctest
    doctest.testmod()
Comment

PREVIOUS NEXT
Code Example
Python :: how to generate python code 
Python :: dependency injection python 
Python :: how to avoid inserting duplicate records in orm django 
Python :: parse email python 
Python :: python qr scanner 
Python :: tuple and for loop 
Python :: how to get parent model object based on child model filter in django 
Python :: python TypeError: function takes positional arguments but were given 
Python :: python destructuring 
Python :: how to iterate row wise using 2d integer array in python 
Python :: get_queryset django rest framework 
Python :: re.sub 
Python :: add legend to colorbar 
Python :: traversal tree in python 
Python :: google.protobuf.Struct example python 
Python :: python call function that need args with decorator 
Python :: tables in jinja template 
Python :: how to split a string by colon in python 
Python :: start index from 1 in python 
Python :: title() in python 
Python :: when to use finally python 
Python :: full form of api 
Python :: |safe django 
Python :: python remove character from string 
Python :: pairs with specific difference 
Python :: python multidimensional dictionary 
Python :: TRY 
Python :: how to convert time from one timezone to another in python 
Python :: print all objects in list python 
Python :: standard error of mean 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =