ddp_utils.globals

import ddp_utils.globals

Thread-safe process-local value registry.

Examples

>>> from ddp_utils import globals as process_globals
>>> process_globals.set("DDP_LICENSE_API_KEY", "secret")
>>> process_globals.get_required("DDP_LICENSE_API_KEY")
'secret'
ddp_utils.globals.clear() → None

Remove every registered value.

Examples

>>> set("value", 1)
>>> clear()
>>> snapshot()
{}
ddp_utils.globals.get(key: str, default: Any = None) → Any

Return a value or default when the key is absent.

Parameters

Name

Type

Description

key

str

Non-empty string key.

default

Any

Fallback value.

Returns

Type

Description

Any

Stored value or default.

Examples

>>> get("missing", False)
False
ddp_utils.globals.get_required(key: str) → Any

Return a required value.

Parameters

Name

Type

Description

key

str

Non-empty string key.

Returns

Type

Description

Any

Stored value.

Raises

Exception

Description

KeyError

If the key is absent.

Examples

>>> set("request_id", "abc")
>>> get_required("request_id")
'abc'
ddp_utils.globals.has(key: str) → bool

Return whether a key exists, including a key storing None.

Parameters

Name

Type

Description

key

str

Non-empty string key.

Returns

Type

Description

bool

True when the key exists.

Examples

>>> set("optional", None)
>>> has("optional")
True
ddp_utils.globals.set(key: str, value: Any) → None

Store or replace one process-local value.

Parameters

Name

Type

Description

key

str

Non-empty string key.

value

Any

Value to store, including None.

Examples

>>> set("feature_enabled", True)
ddp_utils.globals.snapshot() → dict[str, Any]

Return an independent shallow copy of the registry.

Returns

Type

Description

dict[str, Any]

Dictionary containing current values.

Examples

>>> update({"one": 1})
>>> snapshot()["one"]
1
ddp_utils.globals.temporary(values: Mapping[str, Any]) → Iterator[None]

Temporarily override values and restore their exact prior state.

Parameters

Name

Type

Description

values

Mapping[str, Any]

Values applied for the lifetime of the context.

Yields

Control while temporary values are installed.

Raises

Exception

Description

TypeError

If values is not a mapping.

Examples

>>> set("mode", "production")
>>> with temporary({"mode": "test"}):
...     assert get("mode") == "test"
>>> get("mode")
'production'
ddp_utils.globals.unset(key: str, *, missing_ok: bool = True) → Any

Remove and return one value.

Parameters

Name

Type

Description

key

str

Non-empty string key.

missing_ok

bool

Return None when the key is absent.

Returns

Type

Description

Any

Removed value or None.

Raises

Exception

Description

KeyError

If absent and missing_ok is false.

Examples

>>> set("temporary", 1)
>>> unset("temporary")
1
ddp_utils.globals.update(values: Mapping[str, Any]) → None

Atomically store multiple values.

Parameters

Name

Type

Description

values

Mapping[str, Any]

Mapping of non-empty string keys to values.

Raises

Exception

Description

TypeError

If values is not a mapping.

Examples

>>> update({"api_key": "secret", "enabled": True})