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 :: pandas apply dont convert to timestamp 
Python :: google.api_core.exceptions.ServiceUnavailable: 503 The datastore operation timed out, or the data was temporarily unavailable when using stream 
Python :: python requests-session for websites wihout login 
Python :: _set.filter django 
Python :: As a general rule in python 
Python :: string to 2d array python 
Python :: hide model field form 
Python :: list data structure in python 
Python :: meaning of self keyword in user defined function 
Python :: threading pass keyword args example 
Python :: Display tail of the DataFrame 
Python :: formula for nth fibonnaci number 
Python :: nim game in python 
Python :: How to clear out a set in python 
Python :: python datetime toordinal 
Python :: python input() google suche 
Python :: It appears you are missing some prerequisite to build the package from source 
Python :: run c code in python 
Python :: django url wildcard 
Python :: enter three numbers and find smallest number in python 
Python :: pyPS4Controller usage 
Python :: find the index of nanmax 
Python :: python extract extension 
Python :: what is flash in flask 
Python :: _rocketcore pypi 
Python :: double char 
Python :: python scrape data from aspx page 
Python :: how to find left top width and height on an image using python 
Python :: multivariable traces f(x, y) = sin(x)cos(y) 
Python :: CNN Libraries 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =