#๐Ÿ”’ Help with doctest etc

29 messages ยท Page 1 of 1 (latest)

long quest
#

Hi I've been given a task for my coding class and I can't seem to understand what it wants me to do? What is a Doctest?

tiny rockBOT
#

@long quest

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

remote zephyr
#

doctest is a module included in the Python programming language's standard library that allows the easy generation of tests based on output from the standard Python interpreter shell, cut and pasted into docstrings.

#

google man

long quest
#

What are tripple quotes to do with it though?

long quest
remote zephyr
#

its so you know the code outputs

#
def add(a, b):
    """
    Returns the sum of two numbers.

    >>> add(2, 3)
    5
    >>> add(-1, 1)
    0
    >>> add(0, 0)
    0
    """
    return a + b
#

then you can do

    import doctest
    doctest.testmod(verbose=True)
#

no tab

#

!e

def celsius_to_fahrenheit(c):
    """
    Convert Celsius to Fahrenheit.

    >>> celsius_to_fahrenheit(0)
    32.0
    >>> celsius_to_fahrenheit(100)
    212.0
    >>> celsius_to_fahrenheit(-40)
    -40.0
    """
    return c * 9 / 5 + 32

if __name__ == "__main__":
    import doctest
    doctest.testmod(verbose=True)
tiny rockBOT
remote zephyr
#

@long quest

long quest
#

Thank you so much

remote zephyr
#

no problem

glacial snow
# long quest Thank you so much

I gather you're on board with doctests being testable python runs in the docstrings.

You can make doctests by doing example code in the Python REPL (the >>> prompt) and pasting it into the docstring afterwards.

remote zephyr
glacial snow
remote zephyr
glacial snow
#

Supposing the function call returns a dict with a bunch of fields? To tedious to type, especially type and get correct.

glacial snow
#

Here's a big docstest:

    Here's a simple example with some `Tag`s and a `TagSet`.

        >>> tags = TagSet()
        >>> # add a "bare" Tag named 'blue' with no value
        >>> tags.add('blue')
        >>> # add a "topic=tagging" Tag
        >>> tags.set('topic', 'tagging')
        >>> # make a "subtopic" Tag and add it
        >>> subtopic = Tag('subtopic', 'ontologies')
        >>> tags.add(subtopic)
        >>> # Tags have nice repr() and str()
        >>> subtopic
        Tag(name='subtopic',value='ontologies')
        >>> print(subtopic)
        subtopic=ontologies
        >>> # a TagSet also has a nice repr() and str()
        >>> tags
        TagSet:{'blue': None, 'topic': 'tagging', 'subtopic': 'ontologies'}
        >>> print(tags)
        blue subtopic=ontologies topic=tagging
        >>> tags2 = TagSet({'a': 1}, b=3, c=[1,2,3], d='dee')
        >>> tags2
        TagSet:{'a': 1, 'b': 3, 'c': [1, 2, 3], 'd': 'dee'}
        >>> print(tags2)
        a=1 b=3 c=[1,2,3] d=dee
        >>> # since you can print a TagSet to a file as a line of text
        >>> # you can get it back from a line of text
        >>> TagSet.from_str('a=1 b=3 c=[1,2,3] d=dee')
        TagSet:{'a': 1, 'b': 3, 'c': [1, 2, 3], 'd': 'dee'}
        >>> # because TagSets are dicts you can format strings with them
        >>> print('topic:{topic} subtopic:{subtopic}'.format_map(tags))
        topic:tagging subtopic:ontologies
        >>> # TagSets have convenient membership tests
        >>> # test for blueness
        >>> 'blue' in tags
        True
        >>> # test for redness
        >>> 'red' in tags
        False
        >>> # test for any "subtopic" tag
        >>> 'subtopic' in tags
        True
        >>> # test for subtopic=ontologies
        >>> print(subtopic)
        subtopic=ontologies
        >>> subtopic in tags
        True
        >>> # test for subtopic=libraries
        >>> subtopic2 = Tag('subtopic', 'libraries')
        >>> subtopic2 in tags
        False
#

Anyway, that was taken from an interactive run, and edited to clean out the mistakes.

astral tide
#

shh

remote zephyr
#

@long quest please use !close

long quest
#

Oh yes of course

#

!close

tiny rockBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.