add @oneshot decorator
This commit is contained in:
parent
e8b5c7dec8
commit
355af43fe4
@ -30,3 +30,10 @@ class PortError(MasqueError):
|
|||||||
Exception raised by builder-related functions
|
Exception raised by builder-related functions
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class OneShotError(MasqueError):
|
||||||
|
"""
|
||||||
|
Exception raised when a function decorated with `@oneshot` is called more than once
|
||||||
|
"""
|
||||||
|
def __init__(self, func_name: str) -> None:
|
||||||
|
Exception.__init__(self, f'Function "{func_name}" with @oneshot was called more than once')
|
||||||
|
@ -5,6 +5,7 @@ from .types import layer_t, annotations_t, SupportsBool
|
|||||||
from .array import is_scalar
|
from .array import is_scalar
|
||||||
from .autoslots import AutoSlots
|
from .autoslots import AutoSlots
|
||||||
from .deferreddict import DeferredDict
|
from .deferreddict import DeferredDict
|
||||||
|
from .decorators import oneshot
|
||||||
|
|
||||||
from .bitwise import get_bit, set_bit
|
from .bitwise import get_bit, set_bit
|
||||||
from .vertices import (
|
from .vertices import (
|
||||||
|
21
masque/utils/decorators.py
Normal file
21
masque/utils/decorators.py
Normal 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
|
Loading…
Reference in New Issue
Block a user