diff options
| author | Peter Pentchev <roam@ringlet.net> | 2024-09-10 23:49:05 +0300 |
|---|---|---|
| committer | Peter Pentchev <roam@ringlet.net> | 2024-09-11 21:32:24 +0300 |
| commit | 7b0cce752eca91696664c2bb0de3c5e0395f3fda (patch) | |
| tree | 9f9c62c53aecdc669cec6ff05d19a478b0895bf7 /challenge-286/ppentchev/python/scripts/ch-1.py | |
| parent | cbf28e6e924b7cd24d1614a5ad28d801f0e85dea (diff) | |
| download | perlweeklychallenge-club-7b0cce752eca91696664c2bb0de3c5e0395f3fda.tar.gz perlweeklychallenge-club-7b0cce752eca91696664c2bb0de3c5e0395f3fda.tar.bz2 perlweeklychallenge-club-7b0cce752eca91696664c2bb0de3c5e0395f3fda.zip | |
Add Peter Pentchev's Python solutions to 286
Also fix a couple of things in the other files:
- make the TAP test functions accept an arrayref for the command to run
instead of a single string, since we want to run the Python solution using
`python3 -B -u /path/to/the/program`
- fix the use of Test::More::skip() in the Self Spammer test function
- fix a shadowed variable in the Order Game test function
- correct the indentation in the documentation's "Implementation details" section
- make the README file a copy of docs/index.md instead of a wrong copy of
a completely different project's README file
- point blog.txt to the Ringlet copy of this challenge's documentation
Diffstat (limited to 'challenge-286/ppentchev/python/scripts/ch-1.py')
| -rwxr-xr-x | challenge-286/ppentchev/python/scripts/ch-1.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/challenge-286/ppentchev/python/scripts/ch-1.py b/challenge-286/ppentchev/python/scripts/ch-1.py new file mode 100755 index 0000000000..a3470d1bd4 --- /dev/null +++ b/challenge-286/ppentchev/python/scripts/ch-1.py @@ -0,0 +1,39 @@ +#!/usr/bin/python3 +# SPDX-FileCopyrightText: Peter Pentchev <roam@ringlet.net> +# SPDX-License-Identifier: BSD-2-Clause +"""Pick a random word from this script's source file and output it.""" + +from __future__ import annotations + +import pathlib +import random +import typing + + +if typing.TYPE_CHECKING: + from typing import Final + + +class EmptyWordError(RuntimeError): + """An empty word was chosen, `str.split()` is not suppoed to do that.""" + + words: list[str] + """The words among which the empty word was found.""" + + def __str__(self) -> str: + """Provide a human-readable error message.""" + return f"Internal error: empty word in {self.words!r}" + + +def main() -> None: + """Find the source file, select a word, output it.""" + words: Final = pathlib.Path(__file__).read_text(encoding="UTF-8").split() + chosen_one: Final = random.choice(words) # noqa: S311 # no cryptography here + if not chosen_one: + raise EmptyWordError(words) + + print(chosen_one) # noqa: T201 # this is the whole point of this program + + +if __name__ == "__main__": + main() |
