Skip to content

Instantly share code, notes, and snippets.

@kwinkunks
Last active March 8, 2023 15:52
Show Gist options
  • Save kwinkunks/3e1c5b8c6e993c3b1e791eed3aaa7a39 to your computer and use it in GitHub Desktop.
Save kwinkunks/3e1c5b8c6e993c3b1e791eed3aaa7a39 to your computer and use it in GitHub Desktop.
*Docstrings* and *doctests* are nice ways to get started with the topics of documentation and testing. The idea is to write the string that shows up when you do `help(some_function)` and in particular to include examples of how to call your function. The beautiful thing is that we can check that those examples work as advertised, a very useful …
def has_illegal_chars(string: str, illegal: str = ',;"!+=') -> bool:
"""
Detect the presence of illegal characters in a string.
By default, illegal characters are: `,;"!+=`
Args:
string: A string of text of any length.
illegal: A sequence of characters that are not allowed.
Returns:
True if any of the illegals appear at least once, and
False otherwise.
Examples:
>>> has_illegal_chars('This string is okay.')
False
>>> has_illegal_chars('This string is not okay!')
True
>>> has_illegal_chars('Is this okay?', illegal='?') # This test fails, can you fix it?
False
"""
return any(c in string for c in illegal)
# Check that the examples work as we promised:
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment