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 access a dictionary within a dictionary in python 
Python :: change date format to yyyy mm dd in django template datepicker 
Python :: TfidfVectorizer use 
Python :: how to get all the keys of a dictionary in python 
Python :: Django serializer, 
Python :: firebase functions python 
Python :: class attributes in python 
Python :: maximum count of replacements in python 
Python :: minio python check if bucket exists 
Python :: python is scripting language or programming language 
Python :: _getexif 
Python :: how to check if object is of file type in python3 
Python :: hex string to hex number 
Python :: numpy combine two arrays selecting min 
Python :: python open aspx file 
Python :: add output to setting scrapy 
Python :: python power 
Python :: python vs java 
Python :: pairwise combinations groupby 
Python :: element not interactable headless chrome 
Python :: py list 3d 
Python :: use model from checkpoint tensorflow 
Python :: how to change the main diagonal in pandas 
Python :: minio python remove a bucket 
Python :: python get colorscale 
Python :: django error displaying images page not found 
Python :: reverse string python 
Python :: run django server on any network address of the system 
Python :: check even or odd in single line 
Python :: how to decode recv data in python 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =