randfacts 0.3.0__py3-none-any.whl → 0.24.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
randfacts/__init__.py CHANGED
@@ -1,2 +1,48 @@
1
- from .__version__ import __title__, __description__, __url__, __version__, __author__, __author_email__, __license__, __copyright__
2
- from randfacts.main import getFact, safeFacts, unsafeFacts, allFacts
1
+ """Module to generate random facts.
2
+
3
+ randfacts provides an interface to a list of facts installed with the module.
4
+ You can retrieve facts via the `get_fact` method. randfacts also allows for
5
+ execution via the command line. See the examples section for more details.
6
+
7
+ # Examples:
8
+ To use randfacts in your Python code:
9
+
10
+ ```py
11
+ >>> import randfacts
12
+
13
+ # Generate a random SFW (safe for work) fact:
14
+ >>> randfacts.get_fact()
15
+
16
+ # Generate a random NSFW (not safe for work) fact.
17
+ >>> randfacts.get_fact(only_unsafe=True)
18
+
19
+ # Generate a random mixed fact (possibility of both SFW and NSFW facts)
20
+ >>> randfacts.get_fact(filter_enabled=False)
21
+ ```
22
+
23
+ # CLI Examples:
24
+ randfacts can be executed via the command line with the following options:
25
+
26
+ ```sh
27
+ $ python3 -m randfacts # normal execution; only safe facts
28
+ $ python3 -m randfacts --unsafe # only unsafe facts
29
+ $ python3 -m randfacts --mixed # possibility of both SFW and NSFW facts
30
+ $ python3 -m randfacts --help # show CLI help
31
+ ```
32
+ """
33
+
34
+ from randfacts.randfacts import (
35
+ __version__,
36
+ all_facts,
37
+ get_fact,
38
+ safe_facts,
39
+ unsafe_facts,
40
+ )
41
+
42
+ __all__ = [
43
+ "__version__",
44
+ "all_facts",
45
+ "get_fact",
46
+ "safe_facts",
47
+ "unsafe_facts",
48
+ ]
randfacts/__main__.py ADDED
@@ -0,0 +1,66 @@
1
+ """Main CLI entrypoint for randfacts."""
2
+
3
+ import argparse
4
+ from dataclasses import dataclass
5
+
6
+ from randfacts import __version__, get_fact
7
+
8
+
9
+ @dataclass
10
+ class RandfactsNamespace(argparse.Namespace):
11
+ """Dataclass representing randfacts CLI options.
12
+
13
+ Attributes:
14
+ version (bool): whether or not to display the version
15
+ mixed (bool): whether or not to include both safe and unsafe facts
16
+ unsafe (bool): whether or not to only show unsafe facts
17
+ """
18
+
19
+ version: bool
20
+ mixed: bool
21
+ unsafe: bool
22
+
23
+
24
+ def cli_entrypoint() -> None:
25
+ """Entrypoint for execution via command-line."""
26
+ parser = argparse.ArgumentParser(
27
+ description="Generate random facts from the command-line",
28
+ )
29
+
30
+ _ = parser.add_argument(
31
+ "-V",
32
+ "--version",
33
+ action="store_true",
34
+ help="Print the package version and exit",
35
+ )
36
+
37
+ group = parser.add_mutually_exclusive_group()
38
+ _ = group.add_argument(
39
+ "-m",
40
+ "--mixed",
41
+ action="store_true",
42
+ help="Include safe and unsafe facts",
43
+ )
44
+
45
+ _ = group.add_argument(
46
+ "-u",
47
+ "--unsafe",
48
+ action="store_true",
49
+ help="Only include unsafe facts",
50
+ )
51
+
52
+ args = parser.parse_args(namespace=RandfactsNamespace)
53
+
54
+ if args.version:
55
+ print(__version__)
56
+ return
57
+ if args.mixed:
58
+ print(get_fact(filter_enabled=False))
59
+ elif args.unsafe:
60
+ print(get_fact(only_unsafe=True))
61
+ else:
62
+ print(get_fact())
63
+
64
+
65
+ if __name__ == "__main__":
66
+ cli_entrypoint()
randfacts/randfacts.py ADDED
@@ -0,0 +1,42 @@
1
+ """Contains the core functionality of randfacts."""
2
+
3
+ import sys
4
+ from pathlib import Path
5
+ from random import choice
6
+
7
+ if sys.version_info >= (3, 8):
8
+ from importlib import metadata
9
+ else: # pragma: no cover
10
+ import importlib_metadata as metadata # pyright: ignore[reportUnreachable]
11
+
12
+ __version__: str = metadata.version("randfacts")
13
+
14
+ dir_path = Path(__file__).resolve().parent
15
+
16
+ with (dir_path / "safe.txt").open(encoding="utf-8") as f:
17
+ safe_facts = [fact.rstrip("\r\n ") for fact in f if fact.rstrip("\r\n ")]
18
+
19
+ with (dir_path / "unsafe.txt").open(encoding="utf-8") as f:
20
+ unsafe_facts = [fact.rstrip("\r\n ") for fact in f if fact.rstrip("\r\n ")]
21
+
22
+ all_facts = safe_facts + unsafe_facts
23
+
24
+
25
+ def get_fact(filter_enabled: bool = True, only_unsafe: bool = False) -> str:
26
+ """This function returns a random fact.
27
+
28
+ Args:
29
+ filter_enabled (bool): The `filter_enabled` parameter determines if the function
30
+ will filter out potentially inappropriate facts. Defaults to True.
31
+ only_unsafe (bool): The `only_unsafe` parameter determines if the function will
32
+ only give unsafe (NSFW) facts. Takes precedence over the
33
+ `filter_enabled` argument.
34
+
35
+ Returns:
36
+ str: A random fact.
37
+ """
38
+ if only_unsafe:
39
+ return choice(unsafe_facts)
40
+ if not filter_enabled:
41
+ return choice(all_facts)
42
+ return choice(safe_facts)