#๐ Help with doctest etc
29 messages ยท Page 1 of 1 (latest)
@long quest
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.
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
What are tripple quotes to do with it though?
I'm too stupid to read wikipedia
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)
:white_check_mark: Your 3.14 eval job has completed with return code 0.
001 | Trying:
002 | celsius_to_fahrenheit(0)
003 | Expecting:
004 | 32.0
005 | ok
006 | Trying:
007 | celsius_to_fahrenheit(100)
008 | Expecting:
009 | 212.0
010 | ok
... (truncated - too many lines)
Full output: https://paste.pythondiscord.com/SQ3FHQ6AXXW3I6WNXY6TMHRAGM
@long quest
Thank you so much
no problem
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.
I just do it manually but that also works
I find it handy if the output is at all complictaed.
complicated?
Supposing the function call returns a dict with a bunch of fields? To tedious to type, especially type and get correct.
Makes sense, thanks.
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.
shh
@long quest please use !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.