Veritas
Veritas is a minimal Python microframework that enables safe usage of shared mutable default arguments in functions. Designed with both async and sync contexts in mind, Veritas flips the conventional wisdom around "mutable defaults are bad" into a powerful feature for building lightweight stateful systems, in-memory caches, and inter-task communication channels.
Core Philosophy
Mutable defaults, when used intentionally and safely, can act as implicit state carriers. Veritas enforces safety by requiring that shared mutable defaults be explicitly defined using thread-safe or async-safe structures. It validates their usage at function definition time and ensures consistent behavior across sync and async contexts.
Features
- ThreadSafeDict / AsyncSafeDict: Built-in safe dictionaries for concurrent access.
- Safe by Default: Disallows unsafe usage unless explicitly opted-in.
- Support for asyncio and threading: Works seamlessly with both paradigms.
- Introspective Design: Automatically extracts and validates the shared object.
- Direct Access to Internal State: Use
.stateon wrapped functions to inspect or manipulate shared memory.
Installation
pip install veritas-mem
Usage
from veritas import veritas
from veritas.datastructs import ThreadSafeDict
shared = ThreadSafeDict()
shared.set("count", 0)
@veritas
def increment(shared=shared):
val = shared.get("count")
shared.set("count", val + 1)
print(f"Count is now {val + 1}")
for _ in range(10):
increment()
# Access shared state directly
print("Final count:", increment.state.get("count"))
Caching Features
Veritas also includes a powerful @cache decorator that provides in-memory caching with the following features:
maxsize: Limits the cache to a maximum number of entries.- LRU Eviction: Uses a Least Recently Used (LRU) policy to evict entries when the cache is full.
ttl: Sets a Time-to-Live (TTL) for cached items, causing them to expire after a specified duration.- Probabilistic Eviction: When both
maxsizeandttlare set, the cache uses a probabilistic eviction strategy to efficiently remove expired items.
Caching Example
from veritas.cache import cache
import time
@cache(maxsize=10, ttl=5)
def my_cached_function(x):
print(f"Computing for {x}...")
return x * x
# First call, this will be a miss
my_cached_function(5)
# Second call, this will be a hit
my_cached_function(5)
# Wait for the TTL to expire
time.sleep(6)
# Third call, this will be a miss again
my_cached_function(5)
Example
See the examples directory for a multi-threaded shared status writer-reader.
Why Not ContextVars?
Veritas is not a replacement for contextvars. Instead, it offers a different kind of shared memory: persistent, introspectable, and defined at function scope rather than dynamic execution context.
Contributing
Feature requests, bug reports, and PRs are welcome. See the TODO list in the issues for roadmap.
Veritas — because shared memory doesn’t have to be dangerous.
Metadata
Release files for veritas-mem 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| veritas_mem-0.1.1.tar.gz | 5.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| veritas_mem-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.1 kB
Release files / veritas_mem-0.1.1.tar.gz
| Download URL | veritas_mem-0.1.1.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5648faa7ddab0652ee5339c61e62d732fa145ff1fb86ed7038cb39441542dda8
|
|
BLAKE2b-256 checksum How to use checksums |
f9a80f734836944219691eb05b0aa1cc3aa463518367336dc3dbb49c6b312100
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.11.9 Windows/10
|
Release files / veritas_mem-0.1.1-py3-none-any.whl
| Download URL | veritas_mem-0.1.1-py3-none-any.whl |
|---|---|
| Size | 6.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
78f21aec4f8a2077fb6ce07747a0bf2d58a2a38d891f54785d5f6f65cbc40680
|
|
BLAKE2b-256 checksum How to use checksums |
968ba42028e7d99a16546340a8af1e6cd9e29ba05d578c55734ca032a702ea8e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.11.9 Windows/10
|