add @oneshot decorator

This commit is contained in:
jan 2023-04-07 16:33:23 -07:00
commit 46a7f60460
2 changed files with 29 additions and 0 deletions

View file

@ -5,6 +5,7 @@ from .types import layer_t, annotations_t, SupportsBool
from .array import is_scalar
from .autoslots import AutoSlots
from .deferreddict import DeferredDict
from .decorators import oneshot
from .bitwise import get_bit, set_bit
from .vertices import (

View file

@ -0,0 +1,21 @@
from typing import Callable
from functools import wraps
from ..error import OneShotError
def oneshot(func: Callable) -> Callable:
"""
Raises a OneShotError if the decorated function is called more than once
"""
expired = False
@wraps(func)
def wrapper(*args, **kwargs):
nonlocal expired
if expired:
raise OneShotError(func.__name__)
expired = True
return func(*args, **kwargs)
return wrapper