bear-utils 0.7.11__tar.gz

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.
Files changed (83) hide show
  1. bear_utils-0.7.11/PKG-INFO +260 -0
  2. bear_utils-0.7.11/README.md +234 -0
  3. bear_utils-0.7.11/pyproject.toml +43 -0
  4. bear_utils-0.7.11/src/bear_utils/__init__.py +13 -0
  5. bear_utils-0.7.11/src/bear_utils/ai/__init__.py +30 -0
  6. bear_utils-0.7.11/src/bear_utils/ai/ai_helpers/__init__.py +130 -0
  7. bear_utils-0.7.11/src/bear_utils/ai/ai_helpers/_common.py +19 -0
  8. bear_utils-0.7.11/src/bear_utils/ai/ai_helpers/_config.py +24 -0
  9. bear_utils-0.7.11/src/bear_utils/ai/ai_helpers/_parsers.py +188 -0
  10. bear_utils-0.7.11/src/bear_utils/ai/ai_helpers/_types.py +20 -0
  11. bear_utils-0.7.11/src/bear_utils/cache/__init__.py +119 -0
  12. bear_utils-0.7.11/src/bear_utils/cli/__init__.py +4 -0
  13. bear_utils-0.7.11/src/bear_utils/cli/commands.py +59 -0
  14. bear_utils-0.7.11/src/bear_utils/cli/prompt_helpers.py +166 -0
  15. bear_utils-0.7.11/src/bear_utils/cli/shell/__init__.py +0 -0
  16. bear_utils-0.7.11/src/bear_utils/cli/shell/_base_command.py +74 -0
  17. bear_utils-0.7.11/src/bear_utils/cli/shell/_base_shell.py +390 -0
  18. bear_utils-0.7.11/src/bear_utils/cli/shell/_common.py +19 -0
  19. bear_utils-0.7.11/src/bear_utils/config/__init__.py +11 -0
  20. bear_utils-0.7.11/src/bear_utils/config/config_manager.py +92 -0
  21. bear_utils-0.7.11/src/bear_utils/config/dir_manager.py +64 -0
  22. bear_utils-0.7.11/src/bear_utils/config/settings_manager.py +232 -0
  23. bear_utils-0.7.11/src/bear_utils/constants/__init__.py +16 -0
  24. bear_utils-0.7.11/src/bear_utils/constants/_exceptions.py +3 -0
  25. bear_utils-0.7.11/src/bear_utils/constants/_lazy_typing.py +15 -0
  26. bear_utils-0.7.11/src/bear_utils/constants/date_related.py +36 -0
  27. bear_utils-0.7.11/src/bear_utils/constants/time_related.py +22 -0
  28. bear_utils-0.7.11/src/bear_utils/database/__init__.py +6 -0
  29. bear_utils-0.7.11/src/bear_utils/database/_db_manager.py +104 -0
  30. bear_utils-0.7.11/src/bear_utils/events/__init__.py +16 -0
  31. bear_utils-0.7.11/src/bear_utils/events/events_class.py +52 -0
  32. bear_utils-0.7.11/src/bear_utils/events/events_module.py +65 -0
  33. bear_utils-0.7.11/src/bear_utils/extras/__init__.py +17 -0
  34. bear_utils-0.7.11/src/bear_utils/extras/_async_helpers.py +15 -0
  35. bear_utils-0.7.11/src/bear_utils/extras/_tools.py +178 -0
  36. bear_utils-0.7.11/src/bear_utils/extras/platform_utils.py +53 -0
  37. bear_utils-0.7.11/src/bear_utils/extras/wrappers/__init__.py +0 -0
  38. bear_utils-0.7.11/src/bear_utils/extras/wrappers/add_methods.py +98 -0
  39. bear_utils-0.7.11/src/bear_utils/files/__init__.py +4 -0
  40. bear_utils-0.7.11/src/bear_utils/files/file_handlers/__init__.py +3 -0
  41. bear_utils-0.7.11/src/bear_utils/files/file_handlers/_base_file_handler.py +93 -0
  42. bear_utils-0.7.11/src/bear_utils/files/file_handlers/file_handler_factory.py +278 -0
  43. bear_utils-0.7.11/src/bear_utils/files/file_handlers/json_file_handler.py +44 -0
  44. bear_utils-0.7.11/src/bear_utils/files/file_handlers/log_file_handler.py +33 -0
  45. bear_utils-0.7.11/src/bear_utils/files/file_handlers/txt_file_handler.py +34 -0
  46. bear_utils-0.7.11/src/bear_utils/files/file_handlers/yaml_file_handler.py +57 -0
  47. bear_utils-0.7.11/src/bear_utils/files/ignore_parser.py +298 -0
  48. bear_utils-0.7.11/src/bear_utils/graphics/__init__.py +4 -0
  49. bear_utils-0.7.11/src/bear_utils/graphics/bear_gradient.py +140 -0
  50. bear_utils-0.7.11/src/bear_utils/graphics/image_helpers.py +39 -0
  51. bear_utils-0.7.11/src/bear_utils/gui/__init__.py +3 -0
  52. bear_utils-0.7.11/src/bear_utils/gui/gui_tools/__init__.py +5 -0
  53. bear_utils-0.7.11/src/bear_utils/gui/gui_tools/_settings.py +37 -0
  54. bear_utils-0.7.11/src/bear_utils/gui/gui_tools/_types.py +12 -0
  55. bear_utils-0.7.11/src/bear_utils/gui/gui_tools/qt_app.py +145 -0
  56. bear_utils-0.7.11/src/bear_utils/gui/gui_tools/qt_color_picker.py +119 -0
  57. bear_utils-0.7.11/src/bear_utils/gui/gui_tools/qt_file_handler.py +138 -0
  58. bear_utils-0.7.11/src/bear_utils/gui/gui_tools/qt_input_dialog.py +306 -0
  59. bear_utils-0.7.11/src/bear_utils/logging/__init__.py +25 -0
  60. bear_utils-0.7.11/src/bear_utils/logging/logger_manager/__init__.py +0 -0
  61. bear_utils-0.7.11/src/bear_utils/logging/logger_manager/_common.py +47 -0
  62. bear_utils-0.7.11/src/bear_utils/logging/logger_manager/_console_junk.py +131 -0
  63. bear_utils-0.7.11/src/bear_utils/logging/logger_manager/_styles.py +91 -0
  64. bear_utils-0.7.11/src/bear_utils/logging/logger_manager/loggers/__init__.py +0 -0
  65. bear_utils-0.7.11/src/bear_utils/logging/logger_manager/loggers/_base_logger.py +238 -0
  66. bear_utils-0.7.11/src/bear_utils/logging/logger_manager/loggers/_base_logger.pyi +50 -0
  67. bear_utils-0.7.11/src/bear_utils/logging/logger_manager/loggers/_buffer_logger.py +55 -0
  68. bear_utils-0.7.11/src/bear_utils/logging/logger_manager/loggers/_console_logger.py +249 -0
  69. bear_utils-0.7.11/src/bear_utils/logging/logger_manager/loggers/_console_logger.pyi +64 -0
  70. bear_utils-0.7.11/src/bear_utils/logging/logger_manager/loggers/_file_logger.py +141 -0
  71. bear_utils-0.7.11/src/bear_utils/logging/logger_manager/loggers/_level_sin.py +58 -0
  72. bear_utils-0.7.11/src/bear_utils/logging/logger_manager/loggers/_logger.py +18 -0
  73. bear_utils-0.7.11/src/bear_utils/logging/logger_manager/loggers/_sub_logger.py +110 -0
  74. bear_utils-0.7.11/src/bear_utils/logging/logger_manager/loggers/_sub_logger.pyi +38 -0
  75. bear_utils-0.7.11/src/bear_utils/logging/loggers.py +76 -0
  76. bear_utils-0.7.11/src/bear_utils/monitoring/__init__.py +10 -0
  77. bear_utils-0.7.11/src/bear_utils/monitoring/host_monitor.py +350 -0
  78. bear_utils-0.7.11/src/bear_utils/time/__init__.py +16 -0
  79. bear_utils-0.7.11/src/bear_utils/time/_helpers.py +91 -0
  80. bear_utils-0.7.11/src/bear_utils/time/_time_class.py +316 -0
  81. bear_utils-0.7.11/src/bear_utils/time/_timer.py +80 -0
  82. bear_utils-0.7.11/src/bear_utils/time/_tools.py +17 -0
  83. bear_utils-0.7.11/src/bear_utils/time/time_manager.py +218 -0
@@ -0,0 +1,260 @@
1
+ Metadata-Version: 2.3
2
+ Name: bear-utils
3
+ Version: 0.7.11
4
+ Summary: Various utilities for Bear programmers, including a rich logging utility, a disk cache, and a SQLite database wrapper amongst other things.
5
+ Author: chaz
6
+ Author-email: bright.lid5647@fastmail.com
7
+ Requires-Python: >=3.12
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.12
10
+ Classifier: Programming Language :: Python :: 3.13
11
+ Requires-Dist: diskcache (>=5.6.3,<6.0.0)
12
+ Requires-Dist: httpx (>=0.28.1)
13
+ Requires-Dist: pathspec (>=0.12.1)
14
+ Requires-Dist: pillow (>=11.2.1,<12.0.0)
15
+ Requires-Dist: prompt-toolkit (>=3.0.51,<4.0.0)
16
+ Requires-Dist: pydantic (>=2.11.5)
17
+ Requires-Dist: pyglm (>=2.8.2,<3.0.0)
18
+ Requires-Dist: pyqt6 (>=6.9.0)
19
+ Requires-Dist: pytz (>=2025.2)
20
+ Requires-Dist: pyyaml (>=6.0.2)
21
+ Requires-Dist: rich (>=14.0.0,<15.0.0)
22
+ Requires-Dist: singleton-base (>=1.0.5)
23
+ Requires-Dist: sqlalchemy (>=2.0.40,<3.0.0)
24
+ Description-Content-Type: text/markdown
25
+
26
+ # Bear Utils v# Bear Utils v0.7.11
27
+
28
+ Personal set of tools and utilities for Python projects, focusing on modularity and ease of use. This library includes components for caching, database management, logging, time handling, file operations, CLI prompts, image processing, clipboard interaction, gradient utilities, event systems, and async helpers.
29
+
30
+ ## Overview
31
+
32
+ Bear Utils is a collection of utility modules I've created for common tasks across my Python projects. The library is designed to be modular and easy to use, with each component focusing on a specific functionality.
33
+
34
+ ## Installation
35
+
36
+ ```bash
37
+ # Using pip
38
+ pip install bear-utils
39
+
40
+ # Using poetry
41
+ poetry add bear-utils
42
+ ```
43
+
44
+ ## Key Components
45
+
46
+ ### Cache Management
47
+
48
+ The `cache` package provides disk cache functionality via `diskcache`:
49
+
50
+ ```python
51
+ from bear_utils.cache import CacheWrapper, cache_factory
52
+
53
+ # Create a cache instance
54
+ cache = Cache(directory="~/.cache/my_app")
55
+
56
+ # Use the cache factory to create a decorated function
57
+ @cache_factory(directory="~/.cache/my_app")
58
+ def expensive_function(arg1, arg2):
59
+ # Function will be cached based on arguments
60
+ pass
61
+ ```
62
+
63
+ ### Database Management
64
+
65
+ The `database` package provides SQLAlchemy integration with helpful patterns:
66
+
67
+ ```python
68
+ from bear_utils.database import DatabaseManager
69
+ from sqlalchemy import Column, Integer, String
70
+
71
+ # Get declarative base
72
+ Base = DatabaseManager.get_base()
73
+
74
+ # Define models
75
+ class User(Base):
76
+ __tablename__ = "users"
77
+ id = Column(Integer, primary_key=True)
78
+ name = Column(String)
79
+
80
+ # Create and use database
81
+ db = DatabaseManager("sqlite:///app.db")
82
+ with db.session() as session:
83
+ session.add(User(name="test"))
84
+ session.commit()
85
+ ```
86
+
87
+ ### Logging
88
+
89
+ The `logging` package provides an enhanced console logger using rich:
90
+
91
+ ```python
92
+ from bear_utils.logging import ConsoleLogger
93
+
94
+ logger = ConsoleLogger()
95
+ logger.info("Information message")
96
+ logger.error("Error message")
97
+ logger.warning("Warning message")
98
+ ```
99
+
100
+ ### Time Management
101
+
102
+ The `time` package contains tools for time handling and measurement:
103
+
104
+ ```python
105
+ from bear_utils.time import TimeTools, EpochTimestamp
106
+
107
+ # Get current timestamp
108
+ now = EpochTimestamp()
109
+
110
+ # Time function execution
111
+ from bear_utils.time.time_manager import timer
112
+
113
+ with timer(name="my_operation"):
114
+ # Code to measure
115
+ pass
116
+ ```
117
+
118
+ ### File Handling
119
+
120
+ The `files` package provides abstractions for working with different file types:
121
+
122
+ ```python
123
+ from pathlib import Path
124
+ from bear_utils.files.file_handlers import FileHandlerFactory
125
+
126
+ factory = FileHandlerFactory()
127
+ handler = factory.get_handler(Path("config.json"))
128
+ data = handler.read_file()
129
+ ```
130
+
131
+ ### Prompt Helpers
132
+
133
+ Utilities for creating interactive command-line prompts:
134
+
135
+ ```python
136
+ from bear_utils.cli.prompt_helpers import restricted_prompt
137
+
138
+ choice = restricted_prompt(
139
+ message="Select an option:",
140
+ valid_options=["option1", "option2"]
141
+ )
142
+ ```
143
+
144
+ ### Image Helpers
145
+
146
+ Utilities for working with images are located in the `graphics` package:
147
+
148
+ ```python
149
+ from pathlib import Path
150
+ from bear_utils.graphics import encode_image_to_jpeg, encode_image_to_png
151
+
152
+ # Encode image to base64 string
153
+ jpeg_data = encode_image_to_jpeg(Path("image.jpg"), max_size=800)
154
+ png_data = encode_image_to_png(Path("image.png"), max_size=800)
155
+ ```
156
+
157
+ ### Clipboard Helpers
158
+
159
+ `bear_utils.extras._tools` includes simple helpers for interacting with the system clipboard:
160
+
161
+ ```python
162
+ from bear_utils.extras._tools import copy_to_clipboard, paste_from_clipboard
163
+
164
+ copy_to_clipboard("hello world")
165
+ text = paste_from_clipboard()
166
+ ```
167
+
168
+ Supported platforms include macOS (`pbcopy/pbpaste`), Windows (`clip`/`powershell Get-Clipboard`),
169
+ and Linux with either Wayland (`wl-copy`/`wl-paste`) or X11 (`xclip`).
170
+
171
+ ### Gradient Utilities
172
+
173
+ The `graphics.bear_gradient` module provides color gradient functionality for the purposes of visualizing data or creating color transitions:
174
+
175
+ ```python
176
+ from bear_utils.graphics import ColorGradient, RichColor
177
+ from rich.console import Console
178
+ from rich.color_triplet import ColorTriplet
179
+
180
+ console = Console()
181
+
182
+ # Health meter goes from red (low health) to green (full health)
183
+ health_gradient = ColorGradient()
184
+
185
+ console.print("🏥 [bold]Health Meter Demonstration[/bold] 🏥\n")
186
+
187
+ # Normal health: Red (low) -> Green (high)
188
+ console.print("[bold green]Normal Health Levels (0% = Critical, 100% = Perfect):[/bold green]")
189
+ for health in range(0, 101, 10):
190
+ color: ColorTriplet = health_gradient.map_to_color(0, 100, health)
191
+ health_bar = "█" * (health // 5)
192
+ console.print(f"HP: {health:3d}/100 {health_bar:<20}", style=color.rgb)
193
+
194
+ console.print("\n" + "="*50 + "\n")
195
+
196
+ # Reversed: Infection/Damage meter (Green = good, Red = bad)
197
+ console.print("[bold red]Infection Level (0% = Healthy, 100% = Critical):[/bold red]")
198
+ health_gradient.reverse = True
199
+ for infection in range(0, 101, 10):
200
+ color: ColorTriplet = health_gradient.map_to_color(0, 100, infection)
201
+ infection_bar = "█" * (infection // 5)
202
+ status = "🦠" if infection > 70 else "⚠️" if infection > 30 else "✅"
203
+ console.print(f"Infection: {infection:3d}% {infection_bar:<20} {status}", style=color.rgb)
204
+
205
+ health_scenarios = [
206
+ (5, "💀 Nearly Dead"),
207
+ (25, "🩸 Critical Condition"),
208
+ (50, "⚠️ Wounded"),
209
+ (75, "😐 Recovering"),
210
+ (95, "💪 Almost Full Health"),
211
+ (100, "✨ Perfect Health")
212
+ ]
213
+
214
+ console.print("[bold green]Health Status Examples:[/bold green]")
215
+ for hp, status in health_scenarios:
216
+ color: ColorTriplet = health_gradient.map_to_color(0, 100, hp)
217
+ console.print(f"{status}: {hp}/100 HP", style=color.rgb)
218
+ ```
219
+
220
+ ### Event System
221
+
222
+ The `events` module provides a pub/sub event system that supports both synchronous and asynchronous handlers:
223
+
224
+ ```python
225
+ from bear_utils.events.events_module import subscribe, publish
226
+
227
+ # Subscribe to events
228
+ @subscribe("user_logged_in")
229
+ def handle_login(user_id):
230
+ print(f"User {user_id} logged in")
231
+
232
+ # Publish events
233
+ publish("user_logged_in", 12345)
234
+
235
+ # Async support
236
+ @subscribe("data_processed")
237
+ async def handle_process(data):
238
+ await process_async(data)
239
+
240
+ # Clear handlers
241
+ from bear_utils.events.events_module import clear_handlers_for_event
242
+ clear_handlers_for_event("user_logged_in")
243
+ ```
244
+
245
+ ### Async Helpers
246
+
247
+ The `extras/_async_helpers.py` module provides utility functions for working with async code:
248
+
249
+ ```python
250
+ from bear_utils.extras._async_helpers import is_async_function
251
+
252
+ def handle_function(func):
253
+ if is_async_function(func):
254
+ # Handle async function
255
+ pass
256
+ else:
257
+ # Handle sync function
258
+ pass
259
+ ```
260
+
@@ -0,0 +1,234 @@
1
+ # Bear Utils v# Bear Utils v0.7.11
2
+
3
+ Personal set of tools and utilities for Python projects, focusing on modularity and ease of use. This library includes components for caching, database management, logging, time handling, file operations, CLI prompts, image processing, clipboard interaction, gradient utilities, event systems, and async helpers.
4
+
5
+ ## Overview
6
+
7
+ Bear Utils is a collection of utility modules I've created for common tasks across my Python projects. The library is designed to be modular and easy to use, with each component focusing on a specific functionality.
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ # Using pip
13
+ pip install bear-utils
14
+
15
+ # Using poetry
16
+ poetry add bear-utils
17
+ ```
18
+
19
+ ## Key Components
20
+
21
+ ### Cache Management
22
+
23
+ The `cache` package provides disk cache functionality via `diskcache`:
24
+
25
+ ```python
26
+ from bear_utils.cache import CacheWrapper, cache_factory
27
+
28
+ # Create a cache instance
29
+ cache = Cache(directory="~/.cache/my_app")
30
+
31
+ # Use the cache factory to create a decorated function
32
+ @cache_factory(directory="~/.cache/my_app")
33
+ def expensive_function(arg1, arg2):
34
+ # Function will be cached based on arguments
35
+ pass
36
+ ```
37
+
38
+ ### Database Management
39
+
40
+ The `database` package provides SQLAlchemy integration with helpful patterns:
41
+
42
+ ```python
43
+ from bear_utils.database import DatabaseManager
44
+ from sqlalchemy import Column, Integer, String
45
+
46
+ # Get declarative base
47
+ Base = DatabaseManager.get_base()
48
+
49
+ # Define models
50
+ class User(Base):
51
+ __tablename__ = "users"
52
+ id = Column(Integer, primary_key=True)
53
+ name = Column(String)
54
+
55
+ # Create and use database
56
+ db = DatabaseManager("sqlite:///app.db")
57
+ with db.session() as session:
58
+ session.add(User(name="test"))
59
+ session.commit()
60
+ ```
61
+
62
+ ### Logging
63
+
64
+ The `logging` package provides an enhanced console logger using rich:
65
+
66
+ ```python
67
+ from bear_utils.logging import ConsoleLogger
68
+
69
+ logger = ConsoleLogger()
70
+ logger.info("Information message")
71
+ logger.error("Error message")
72
+ logger.warning("Warning message")
73
+ ```
74
+
75
+ ### Time Management
76
+
77
+ The `time` package contains tools for time handling and measurement:
78
+
79
+ ```python
80
+ from bear_utils.time import TimeTools, EpochTimestamp
81
+
82
+ # Get current timestamp
83
+ now = EpochTimestamp()
84
+
85
+ # Time function execution
86
+ from bear_utils.time.time_manager import timer
87
+
88
+ with timer(name="my_operation"):
89
+ # Code to measure
90
+ pass
91
+ ```
92
+
93
+ ### File Handling
94
+
95
+ The `files` package provides abstractions for working with different file types:
96
+
97
+ ```python
98
+ from pathlib import Path
99
+ from bear_utils.files.file_handlers import FileHandlerFactory
100
+
101
+ factory = FileHandlerFactory()
102
+ handler = factory.get_handler(Path("config.json"))
103
+ data = handler.read_file()
104
+ ```
105
+
106
+ ### Prompt Helpers
107
+
108
+ Utilities for creating interactive command-line prompts:
109
+
110
+ ```python
111
+ from bear_utils.cli.prompt_helpers import restricted_prompt
112
+
113
+ choice = restricted_prompt(
114
+ message="Select an option:",
115
+ valid_options=["option1", "option2"]
116
+ )
117
+ ```
118
+
119
+ ### Image Helpers
120
+
121
+ Utilities for working with images are located in the `graphics` package:
122
+
123
+ ```python
124
+ from pathlib import Path
125
+ from bear_utils.graphics import encode_image_to_jpeg, encode_image_to_png
126
+
127
+ # Encode image to base64 string
128
+ jpeg_data = encode_image_to_jpeg(Path("image.jpg"), max_size=800)
129
+ png_data = encode_image_to_png(Path("image.png"), max_size=800)
130
+ ```
131
+
132
+ ### Clipboard Helpers
133
+
134
+ `bear_utils.extras._tools` includes simple helpers for interacting with the system clipboard:
135
+
136
+ ```python
137
+ from bear_utils.extras._tools import copy_to_clipboard, paste_from_clipboard
138
+
139
+ copy_to_clipboard("hello world")
140
+ text = paste_from_clipboard()
141
+ ```
142
+
143
+ Supported platforms include macOS (`pbcopy/pbpaste`), Windows (`clip`/`powershell Get-Clipboard`),
144
+ and Linux with either Wayland (`wl-copy`/`wl-paste`) or X11 (`xclip`).
145
+
146
+ ### Gradient Utilities
147
+
148
+ The `graphics.bear_gradient` module provides color gradient functionality for the purposes of visualizing data or creating color transitions:
149
+
150
+ ```python
151
+ from bear_utils.graphics import ColorGradient, RichColor
152
+ from rich.console import Console
153
+ from rich.color_triplet import ColorTriplet
154
+
155
+ console = Console()
156
+
157
+ # Health meter goes from red (low health) to green (full health)
158
+ health_gradient = ColorGradient()
159
+
160
+ console.print("🏥 [bold]Health Meter Demonstration[/bold] 🏥\n")
161
+
162
+ # Normal health: Red (low) -> Green (high)
163
+ console.print("[bold green]Normal Health Levels (0% = Critical, 100% = Perfect):[/bold green]")
164
+ for health in range(0, 101, 10):
165
+ color: ColorTriplet = health_gradient.map_to_color(0, 100, health)
166
+ health_bar = "█" * (health // 5)
167
+ console.print(f"HP: {health:3d}/100 {health_bar:<20}", style=color.rgb)
168
+
169
+ console.print("\n" + "="*50 + "\n")
170
+
171
+ # Reversed: Infection/Damage meter (Green = good, Red = bad)
172
+ console.print("[bold red]Infection Level (0% = Healthy, 100% = Critical):[/bold red]")
173
+ health_gradient.reverse = True
174
+ for infection in range(0, 101, 10):
175
+ color: ColorTriplet = health_gradient.map_to_color(0, 100, infection)
176
+ infection_bar = "█" * (infection // 5)
177
+ status = "🦠" if infection > 70 else "⚠️" if infection > 30 else "✅"
178
+ console.print(f"Infection: {infection:3d}% {infection_bar:<20} {status}", style=color.rgb)
179
+
180
+ health_scenarios = [
181
+ (5, "💀 Nearly Dead"),
182
+ (25, "🩸 Critical Condition"),
183
+ (50, "⚠️ Wounded"),
184
+ (75, "😐 Recovering"),
185
+ (95, "💪 Almost Full Health"),
186
+ (100, "✨ Perfect Health")
187
+ ]
188
+
189
+ console.print("[bold green]Health Status Examples:[/bold green]")
190
+ for hp, status in health_scenarios:
191
+ color: ColorTriplet = health_gradient.map_to_color(0, 100, hp)
192
+ console.print(f"{status}: {hp}/100 HP", style=color.rgb)
193
+ ```
194
+
195
+ ### Event System
196
+
197
+ The `events` module provides a pub/sub event system that supports both synchronous and asynchronous handlers:
198
+
199
+ ```python
200
+ from bear_utils.events.events_module import subscribe, publish
201
+
202
+ # Subscribe to events
203
+ @subscribe("user_logged_in")
204
+ def handle_login(user_id):
205
+ print(f"User {user_id} logged in")
206
+
207
+ # Publish events
208
+ publish("user_logged_in", 12345)
209
+
210
+ # Async support
211
+ @subscribe("data_processed")
212
+ async def handle_process(data):
213
+ await process_async(data)
214
+
215
+ # Clear handlers
216
+ from bear_utils.events.events_module import clear_handlers_for_event
217
+ clear_handlers_for_event("user_logged_in")
218
+ ```
219
+
220
+ ### Async Helpers
221
+
222
+ The `extras/_async_helpers.py` module provides utility functions for working with async code:
223
+
224
+ ```python
225
+ from bear_utils.extras._async_helpers import is_async_function
226
+
227
+ def handle_function(func):
228
+ if is_async_function(func):
229
+ # Handle async function
230
+ pass
231
+ else:
232
+ # Handle sync function
233
+ pass
234
+ ```
@@ -0,0 +1,43 @@
1
+ [project]
2
+ name = "bear-utils"
3
+ version = "0.7.11"
4
+ description = "Various utilities for Bear programmers, including a rich logging utility, a disk cache, and a SQLite database wrapper amongst other things."
5
+ authors = [
6
+ { name = "chaz", email = "bright.lid5647@fastmail.com" }
7
+ ]
8
+ readme = "README.md"
9
+ requires-python = ">=3.12"
10
+ dependencies = [
11
+ "diskcache (>=5.6.3,<6.0.0)",
12
+ "pyglm (>=2.8.2,<3.0.0)",
13
+ "sqlalchemy (>=2.0.40,<3.0.0)",
14
+ "pillow (>=11.2.1,<12.0.0)",
15
+ "rich (>=14.0.0,<15.0.0)",
16
+ "prompt-toolkit (>=3.0.51,<4.0.0)",
17
+ "pyqt6>=6.9.0",
18
+ "pydantic>=2.11.5",
19
+ "pathspec>=0.12.1",
20
+ "pyyaml>=6.0.2",
21
+ "pytz>=2025.2",
22
+ "singleton-base>=1.0.5",
23
+ "httpx>=0.28.1",
24
+ ]
25
+
26
+
27
+ [build-system]
28
+ requires = ["poetry-core>=2.0.0,<3.0.0"]
29
+ build-backend = "poetry.core.masonry.api"
30
+
31
+ [dependency-groups]
32
+ dev = [
33
+ "bump2version>=1.0.1",
34
+ "isort>=6.0.1",
35
+ "pytest>=8.3.5",
36
+ "twine>=6.1.0",
37
+ ]
38
+
39
+
40
+ [tool.pytest.ini_options]
41
+ markers = [
42
+ "visual: marks tests as visual verification tests (deselect with '-m \"not visual\"')"
43
+ ]
@@ -0,0 +1,13 @@
1
+ from .cache import CacheWrapper, cache, cache_factory
2
+ from .config.settings_manager import SettingsManager, get_settings_manager
3
+ from .constants.date_related import DATE_FORMAT, DATE_TIME_FORMAT
4
+ from .database import DatabaseManager
5
+ from .events import Events
6
+ from .files.file_handlers.file_handler_factory import FileHandlerFactory
7
+ from .logging.logger_manager._common import VERBOSE_CONSOLE_FORMAT
8
+ from .logging.logger_manager._styles import VERBOSE
9
+ from .logging.loggers import BaseLogger, BufferLogger, ConsoleLogger, FileLogger
10
+ from .time._time_class import EpochTimestamp
11
+ from .time.time_manager import TimeTools
12
+
13
+ __version__ = "__version__ = "0.7.11""
@@ -0,0 +1,30 @@
1
+ from .ai_helpers._common import (
2
+ GPT_4_1,
3
+ GPT_4_1_MINI,
4
+ GPT_4_1_NANO,
5
+ PRODUCTION_MODE,
6
+ TESTING_MODE,
7
+ AIModel,
8
+ EnvironmentMode,
9
+ )
10
+ from .ai_helpers._config import AIEndpointConfig
11
+ from .ai_helpers._parsers import CommandResponseParser, PassthroughResponseParser, ResponseParser, TypedResponseParser
12
+ from .ai_helpers._types import ResponseParser as BaseResponseParser
13
+ from .ai_helpers._types import T_Response
14
+
15
+ __all__ = [
16
+ "GPT_4_1",
17
+ "GPT_4_1_MINI",
18
+ "GPT_4_1_NANO",
19
+ "PRODUCTION_MODE",
20
+ "TESTING_MODE",
21
+ "AIModel",
22
+ "EnvironmentMode",
23
+ "AIEndpointConfig",
24
+ "CommandResponseParser",
25
+ "PassthroughResponseParser",
26
+ "ResponseParser",
27
+ "TypedResponseParser",
28
+ "T_Response",
29
+ "BaseResponseParser",
30
+ ]
@@ -0,0 +1,130 @@
1
+ from collections.abc import Callable
2
+ from typing import Any
3
+
4
+ from ...logging import BaseLogger
5
+ from ._common import GPT_4_1, GPT_4_1_MINI, GPT_4_1_NANO, PRODUCTION_MODE, TESTING_MODE, AIModel, EnvironmentMode
6
+ from ._config import AIEndpointConfig
7
+ from ._parsers import JSONResponseParser, ModularAIEndpoint, PassthroughResponseParser, TypedResponseParser
8
+
9
+
10
+ def create_typed_endpoint[T_Response](
11
+ response_type: type[T_Response],
12
+ project_name: str,
13
+ prompt: str,
14
+ testing_url: str,
15
+ production_url: str,
16
+ logger: BaseLogger,
17
+ transformers: dict[str, Callable] | None = None,
18
+ environment: EnvironmentMode = PRODUCTION_MODE,
19
+ chat_model: AIModel = GPT_4_1_NANO,
20
+ **kwargs,
21
+ ) -> ModularAIEndpoint[T_Response]:
22
+ """Create an endpoint with strict TypedDict response typing."""
23
+ config = AIEndpointConfig(
24
+ project_name=project_name,
25
+ prompt=prompt,
26
+ testing_url=testing_url,
27
+ production_url=production_url,
28
+ environment=environment,
29
+ chat_model=chat_model,
30
+ **kwargs,
31
+ )
32
+ parser = TypedResponseParser(default_response=response_type, response_transformers=transformers)
33
+ return ModularAIEndpoint(config, logger, parser) # type: ignore[return-value, arg-type]
34
+
35
+
36
+ def create_command_endpoint[T_Response](
37
+ default_response: T_Response,
38
+ project_name: str,
39
+ prompt: str,
40
+ testing_url: str,
41
+ production_url: str,
42
+ logger: BaseLogger,
43
+ environment: EnvironmentMode = EnvironmentMode.PRODUCTION,
44
+ chat_model: AIModel = GPT_4_1_NANO,
45
+ **kwargs,
46
+ ) -> ModularAIEndpoint[T_Response]:
47
+ from rich.markdown import Markdown
48
+
49
+ config = AIEndpointConfig(
50
+ project_name=project_name,
51
+ prompt=prompt,
52
+ testing_url=testing_url,
53
+ production_url=production_url,
54
+ environment=environment,
55
+ chat_model=chat_model,
56
+ **kwargs,
57
+ )
58
+
59
+ parser = TypedResponseParser(
60
+ default_response=default_response,
61
+ response_transformers={"response": lambda x: Markdown(str(x))},
62
+ )
63
+
64
+ return ModularAIEndpoint(config, logger, parser)
65
+
66
+
67
+ def create_flexible_endpoint(
68
+ project_name: str,
69
+ prompt: str,
70
+ testing_url: str,
71
+ production_url: str,
72
+ logger: BaseLogger,
73
+ required_fields: list | None = None,
74
+ transformers: dict[str, Callable] | None = None,
75
+ append_json: bool = True,
76
+ environment: EnvironmentMode = PRODUCTION_MODE,
77
+ chat_model: AIModel = GPT_4_1_NANO,
78
+ **kwargs,
79
+ ) -> ModularAIEndpoint[dict[str, Any]]:
80
+ """Create an endpoint with flexible JSON parsing."""
81
+ config = AIEndpointConfig(
82
+ project_name=project_name,
83
+ prompt=prompt,
84
+ testing_url=testing_url,
85
+ production_url=production_url,
86
+ append_json_suffix=append_json,
87
+ environment=environment,
88
+ chat_model=chat_model,
89
+ **kwargs,
90
+ )
91
+ parser = JSONResponseParser(required_fields, transformers)
92
+ return ModularAIEndpoint(config, logger, parser)
93
+
94
+
95
+ def create_simple_endpoint(
96
+ project_name: str,
97
+ prompt: str,
98
+ testing_url: str,
99
+ production_url: str,
100
+ logger: BaseLogger,
101
+ environment: EnvironmentMode = PRODUCTION_MODE,
102
+ chat_model: AIModel = GPT_4_1_NANO,
103
+ **kwargs,
104
+ ) -> ModularAIEndpoint[dict[str, Any]]:
105
+ """Create an endpoint that returns raw output without JSON parsing."""
106
+ config = AIEndpointConfig(
107
+ project_name=project_name,
108
+ prompt=prompt,
109
+ testing_url=testing_url,
110
+ production_url=production_url,
111
+ append_json_suffix=False,
112
+ environment=environment,
113
+ chat_model=chat_model,
114
+ **kwargs,
115
+ )
116
+ parser = PassthroughResponseParser()
117
+ return ModularAIEndpoint(config, logger, parser)
118
+
119
+
120
+ __all__: list[str] = [
121
+ "create_typed_endpoint",
122
+ "create_command_endpoint",
123
+ "create_flexible_endpoint",
124
+ "create_simple_endpoint",
125
+ "AIEndpointConfig",
126
+ "EnvironmentMode",
127
+ "PRODUCTION_MODE",
128
+ "TESTING_MODE",
129
+ "ModularAIEndpoint",
130
+ ]