aboutsummaryrefslogtreecommitdiff
path: root/challenge-286/ppentchev/python/scripts/ch-1.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-286/ppentchev/python/scripts/ch-1.py')
-rwxr-xr-xchallenge-286/ppentchev/python/scripts/ch-1.py39
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()