randfacts 0.23.0__py3-none-any.whl → 0.24.1__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,57 +1,48 @@
1
1
  """Module to generate random facts.
2
2
 
3
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
4
+ You can retrieve facts via the `get_fact` method. randfacts also allows for
5
5
  execution via the command line. See the examples section for more details.
6
6
 
7
- Code Examples:
8
- Example usage of randfacts in code.
7
+ # Examples:
8
+ To use randfacts in your Python code:
9
9
 
10
- Generate a random SFW (safe for work) fact.
10
+ ```py
11
+ >>> import randfacts
11
12
 
12
- >>> randfacts.get_fact()
13
+ # Generate a random SFW (safe for work) fact:
14
+ >>> randfacts.get_fact()
13
15
 
14
- Generate a random NSFW (not safe for work) fact.
16
+ # Generate a random NSFW (not safe for work) fact.
17
+ >>> randfacts.get_fact(only_unsafe=True)
15
18
 
16
- >>> randfacts.get_fact(only_unsafe=True)
19
+ # Generate a random mixed fact (possibility of both SFW and NSFW facts)
20
+ >>> randfacts.get_fact(filter_enabled=False)
21
+ ```
17
22
 
18
- Generate a random mixed fact (possibility of both SFW and NSFW facts)
19
-
20
- >>> randfacts.get_fact(filter_enabled=False)
21
-
22
- CLI Examples:
23
- randfacts can be executed via the command line with the following commands:
24
-
25
- Normal execution; only safe facts
26
-
27
- $ python3 -m randfacts
28
-
29
- The unsafe argument can be supplied to provide only unsafe facts
30
-
31
- $ python3 -m randfacts --unsafe
32
-
33
- The mixed argument can be provided to provide both SFW and NSFW facts.
34
-
35
- $ python3 -m randfacts --mixed
36
-
37
- More help.
38
-
39
- $ python3 -m randfacts --help
23
+ # CLI Examples:
24
+ randfacts can be executed via the command line with the following options:
40
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
+ ```
41
32
  """
42
33
 
43
34
  from randfacts.randfacts import (
44
- __version__,
45
- all_facts,
46
- get_fact,
47
- safe_facts,
48
- unsafe_facts,
35
+ __version__,
36
+ all_facts,
37
+ get_fact,
38
+ safe_facts,
39
+ unsafe_facts,
49
40
  )
50
41
 
51
42
  __all__ = [
52
- "__version__",
53
- "all_facts",
54
- "get_fact",
55
- "safe_facts",
56
- "unsafe_facts",
43
+ "__version__",
44
+ "all_facts",
45
+ "get_fact",
46
+ "safe_facts",
47
+ "unsafe_facts",
57
48
  ]
randfacts/__main__.py CHANGED
@@ -1,3 +1,66 @@
1
- from .randfacts import _cli_entrypoint # pyright: ignore[reportPrivateUsage]
1
+ """Main CLI entrypoint for randfacts."""
2
2
 
3
- _cli_entrypoint()
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 CHANGED
@@ -1,94 +1,42 @@
1
1
  """Contains the core functionality of randfacts."""
2
2
 
3
- import argparse
4
3
  import sys
5
4
  from pathlib import Path
6
5
  from random import choice
7
6
 
8
- if sys.version_info >= (3, 8): # noqa: UP036
9
- from importlib import metadata
10
- else:
11
- import importlib_metadata as metadata
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]
12
11
 
13
12
  __version__: str = metadata.version("randfacts")
14
13
 
15
14
  dir_path = Path(__file__).resolve().parent
16
15
 
17
16
  with (dir_path / "safe.txt").open(encoding="utf-8") as f:
18
- safe_facts = [fact.rstrip("\r\n ") for fact in f if fact.rstrip("\r\n ")]
17
+ safe_facts = [fact.rstrip("\r\n ") for fact in f if fact.rstrip("\r\n ")]
19
18
 
20
19
  with (dir_path / "unsafe.txt").open(encoding="utf-8") as f:
21
- unsafe_facts = [fact.rstrip("\r\n ") for fact in f if fact.rstrip("\r\n ")]
20
+ unsafe_facts = [fact.rstrip("\r\n ") for fact in f if fact.rstrip("\r\n ")]
22
21
 
23
22
  all_facts = safe_facts + unsafe_facts
24
23
 
25
24
 
26
25
  def get_fact(filter_enabled: bool = True, only_unsafe: bool = False) -> str:
27
- """This function returns a random fact.
28
-
29
- Parameters
30
- ----------
31
- filter_enabled : bool
32
- The `filter_enabled` parameter determines if the function will filter
33
- out potentially inappropriate facts. Defaults to True.
34
-
35
- only_unsafe : bool
36
- The `only_unsafe` parameter determines if the function will only give
37
- unsafe (NSFW) facts. Takes precedence over the `filter_enabled` argument.
38
-
39
- Returns:
40
- ------
41
- str
42
- A random fact.
43
-
44
- """
45
- if only_unsafe:
46
- return choice(unsafe_facts)
47
- if not filter_enabled:
48
- return choice(all_facts)
49
- return choice(safe_facts)
50
-
51
-
52
- def _cli_entrypoint() -> None:
53
- """Entrypoint for execution via command-line."""
54
- parser = argparse.ArgumentParser(
55
- description="Generate random facts from the command-line",
56
- )
57
-
58
- parser.add_argument(
59
- "-V",
60
- "--version",
61
- action="store_true",
62
- help="Print the package version and exit",
63
- )
64
-
65
- group = parser.add_mutually_exclusive_group()
66
- group.add_argument(
67
- "-m",
68
- "--mixed",
69
- action="store_true",
70
- help="Include safe and unsafe facts",
71
- )
72
-
73
- group.add_argument(
74
- "-u",
75
- "--unsafe",
76
- action="store_true",
77
- help="Only include unsafe facts",
78
- )
79
-
80
- args = parser.parse_args()
81
-
82
- if args.version: # pyright: ignore[reportAny]
83
- print(__version__)
84
- return
85
- if args.mixed: # pyright: ignore[reportAny]
86
- print(get_fact(filter_enabled=False))
87
- elif args.unsafe: # pyright: ignore[reportAny]
88
- print(get_fact(only_unsafe=True))
89
- else:
90
- print(get_fact())
91
-
92
-
93
- if __name__ == "__main__":
94
- _cli_entrypoint()
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)