Source code for xma.counters

# **************************************************
# Copyright (c) 2025, Mayank Mishra
# **************************************************

from collections import defaultdict
from contextlib import contextmanager
from typing import Any


_COUNTERS = defaultdict(int)
_IS_COUNTER_ENABLED = False


[docs] def is_counter_enabled() -> bool: global _IS_COUNTER_ENABLED return _IS_COUNTER_ENABLED
[docs] @contextmanager def enable_counters(): global _IS_COUNTER_ENABLED _IS_COUNTER_ENABLED = True yield _IS_COUNTER_ENABLED = False
[docs] def increment_counter(key: Any, value: int = 1) -> None: if not is_counter_enabled(): return global _COUNTERS _COUNTERS[key] += value
[docs] def reset_counters() -> None: global _COUNTERS _COUNTERS = defaultdict(int)
[docs] def get_counter_value(key: Any) -> int: global _COUNTERS return _COUNTERS[key]