73 lines
2.1 KiB
Markdown
73 lines
2.1 KiB
Markdown
# Boilerplate
|
|
Because I need a boilerplate for all my python stuff, obviously
|
|
|
|
## Settings
|
|
Settings are loaded from app/config/settings.yaml. You can define any dict here you want, then update `utils/settings.py` with the class to match your new definition. You can set defaults for any key and they will be used.
|
|
|
|
### Example Usage:
|
|
```python
|
|
from utils.settings import get_settings()
|
|
|
|
# you can pass a specific config file or use the default (config/settings.yaml)
|
|
config_file = Path("config/settings.yaml")
|
|
settings = get_settings(config_file)
|
|
|
|
# this uses config/settings.yaml
|
|
settings = get_settings()
|
|
|
|
header = f"{settings.app.name} - version:{settings.app.version_major}.{settings.app.version_minor}"
|
|
print(header)
|
|
```
|
|
|
|
## Logs setup and config
|
|
|
|
I have migrated to **structlog** because it's awesome and has "extra" support.
|
|
Example of how to use logging_setup.py
|
|
|
|
```python
|
|
from utils.logging_setup.py import get_logger
|
|
|
|
logger = get_logger(__file__)
|
|
logger.info("I logged something",foo=bar,extra_dict={"foo":"bar"})
|
|
```
|
|
|
|
## Cache DB Example Usage
|
|
|
|
```python
|
|
from utils.cache_db import get_cache
|
|
import time
|
|
|
|
# Singleton cache with default expiration 30 minutes
|
|
cache = get_cache(default_expiration_minutes=30)
|
|
|
|
# Create a cache entry (uses default expiration 30 min)
|
|
cache.create("user:1", {"name": "Alice"})
|
|
|
|
# Create a cache entry that expires in 2 minutes
|
|
cache.create("temp:key", {"value": 42}, expires_in_minutes=2)
|
|
|
|
# Read entries
|
|
print(cache.read("user:1"))
|
|
print(cache.read("temp:key"))
|
|
|
|
time.sleep(120)
|
|
print(cache.read("temp:key")) # None (expired)
|
|
|
|
```
|
|
|
|
## Tasks master example usage
|
|
```python
|
|
from utils.tasks_master import get_tasksmaster
|
|
|
|
# Get the singleton TasksMaster instance
|
|
tasks_master = get_tasksmaster() # auto-creates BackgroundScheduler
|
|
|
|
# At this point, scheduler is already running
|
|
# You can also manually add tasks if needed:
|
|
tasks_master.run_scheduled_tasks() # optional if you want to refresh tasks
|
|
|
|
# List scheduled jobs
|
|
jobs = tasks_master.list_jobs()
|
|
for job in jobs:
|
|
print(f"Job {job['id']} named '{job['name']}' next runs at {job['next_run']}")
|
|
``` |