Skip to main content

Veritas is a minimal Python microframework that enables safe usage of shared mutable default arguments in functions.

Project description

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 .state on 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 maxsize and ttl are 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

veritas_mem-0.1.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

veritas_mem-0.1.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file veritas_mem-0.1.1.tar.gz.

File metadata

  • Download URL: veritas_mem-0.1.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.9 Windows/10

File hashes

Hashes for veritas_mem-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5648faa7ddab0652ee5339c61e62d732fa145ff1fb86ed7038cb39441542dda8
MD5 3fbbd0e8c98d0f3c590610bbe744a037
BLAKE2b-256 f9a80f734836944219691eb05b0aa1cc3aa463518367336dc3dbb49c6b312100

See more details on using hashes here.

File details

Details for the file veritas_mem-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: veritas_mem-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.9 Windows/10

File hashes

Hashes for veritas_mem-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 78f21aec4f8a2077fb6ce07747a0bf2d58a2a38d891f54785d5f6f65cbc40680
MD5 6e882b87f547ccceca0817c33bde993f
BLAKE2b-256 968ba42028e7d99a16546340a8af1e6cd9e29ba05d578c55734ca032a702ea8e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page