apfel.container.once
Primitives for containers that can be written only once. Inspired by OnceCell and LazyCell.
The Once and Lazy classes, are exposed in the package namespace.
Implementation
Once takes reference from OnceCell and the current status is as follows:
Reference OnceCell |
Counterpart |
|---|---|
get |
get |
get_mut |
/ |
get_mut_or_init |
/ |
get_mut_or_try_init |
/ |
get_or_init |
get_or_init |
get_or_try_init |
- |
into_inner |
- |
new |
- |
set |
set |
take |
- |
try_insert |
- |
API#
Lazyclass#
class Lazy(func: Callable[[], T], /)A container that can be lazily initialized only once.
The stored function will be actually called only on the first retrieval, and the result will be cached for consequent calls.
See LazyCell for more information.
This class is exposed in the package namespace.
from apfel.container.once import Lazy
@Lazydef func(): print("called") return object()
obj = func.value()# print "called"# here, the function `func` will be called and the result will be cached.
func.value() is obj # the function `func` will not be called again.__slots__attributeclass attributeinstance attribute#
__slots__ = ('_value', '_has_value', '_init')__bool__method#
def __bool__() -> bool__call__method#
def __call__() -> TAn alias for Lazy.value.
Notice that this operator overloading might be slower than calling value directly.
@Lazydef func(): return object()
obj = func()assert func() is obj__class_getitem__method#
def __class_getitem__(item: U) -> Lazy[U]__init__method#
def __init__(func: Callable[[], T], /) -> NoneCreate a lazily initialized Lazy container.
Parameters
| Name | Type | Description |
|---|---|---|
func | Callable[[], T] | The function to be lazily initialized. |
unwrapmethod#
def unwrap() -> TGet the inner value of the Lazy container.
If no value has been set, this method raises a ValueError.
Returns
T- The lazily initialized value of the
Lazycontainer.
Raises
ValueError- If no value has been set.
valuemethod#
def value() -> TGet the inner value of the Lazy container.
If no value has been set, this method initializes the value with the stored function.
Returns
T- The lazily initialized value of the
Lazycontainer.
LazyLockclass#
class LazyLock(func: Callable[[], T], /)__slots__attributeclass attributeinstance attribute#
__slots__ = ('_value', '_has_value', '_init', '_lock')__bool__method#
def __bool__() -> bool__call__method#
def __call__() -> TAn alias for LazyLock.value.
Notice that this operator overloading might be slower than calling value directly.
from apfel.container.once import LazyLock
lazy_lock = LazyLock(lambda: object())obj = lazy_lock()assert lazy_lock() is obj__class_getitem__method#
def __class_getitem__(item: U) -> LazyLock[U]__init__method#
def __init__(func: Callable[[], T], /) -> NoneCreate a thread-safe lazily initialized LazyLock container.
Parameters
| Name | Type | Description |
|---|---|---|
func | Callable[[], T] | The function to be lazily initialized. |
unwrapmethod#
def unwrap() -> TGet the inner value of the LazyLock container.
If no value has been set, this method raises a ValueError.
Returns
T- The inner value of the
LazyLockcontainer.
Raises
ValueError- If no value has been set.
valuemethod#
def value() -> TGet the inner value of the LazyLock container.
If no value has been set, this method initializes the value with the stored function.
Returns
T- The lazily initialized value of the
LazyLockcontainer.
Onceclass#
class Once()A container that can be written only once.
See OnceCell for more information.
This class is exposed in the package namespace.
__slots__attributeclass attributeinstance attribute#
__slots__ = ('_value', '_has_value')__bool__method#
def __bool__() -> boolCheck if the Once container has been set.
Returns
boolTrueif theOncecontainer has been set,Falseotherwise.
__class_getitem__method#
def __class_getitem__(item: U) -> Once[U]__init__method#
def __init__()Create an unpopulated Once container.
Returns
Once- An unpopulated
Oncecontainer.
getmethod#
def get() -> _maybe.Maybe[T]Get the inner value of the Once container if it has been set.
Returns
_maybe.Maybe[T]- Maybe[T]: A
Just-wrapped inner value of theOncecontainer if it has been set, aNothingotherwise.
get_or_initmethod#
def get_or_init(func: Callable[[], T], /) -> TGet the inner value of the Once container, or initialize it with the given function if no value has been set.
Parameters
| Name | Type | Description |
|---|---|---|
func | Callable[[], T] | The function to initialize the Once container with if no value has been set. |
Returns
T- The inner value (maybe newly set) of the
Oncecontainer.
of_hintmethodclassmethod#
def of_hint(type: type[U], /) -> Once[U]Create a Once container hinting the given type.
Parameters
| Name | Type | Description |
|---|---|---|
type | Type[T] | The type of the value to be stored in the Once container. |
Returns
Once[U]- Once[T]: A
Oncecontainer hinting the given type.
setmethod#
def set(value: T) -> _result.Result[None, T]Set the inner value of the Once container.
If a value has already been set, this method returns an Err containing the existing value.
Parameters
| Name | Type | Description |
|---|---|---|
value | T | The value to set the Once container to. |
Returns
_result.Result[None, T]- Result[None, T]:
Ok(None)if the value was set successfully.Err(value)if the value has already been set, containing the existing value.
unwrapmethod#
def unwrap() -> TGet the inner value of the Once container.
If no value has been set, this method raises a ValueError.
Returns
T- The inner value of the
Oncecontainer.
Raises
ValueError- If no value has been set.
OnceLockclass#
class OnceLock()__slots__attributeclass attributeinstance attribute#
__slots__ = ('_value', '_has_value', '_lock')__bool__method#
def __bool__() -> boolCheck if the OnceLock container has been set.
Returns
boolTrueif theOnceLockcontainer has been set,Falseotherwise.
__class_getitem__method#
def __class_getitem__(item: U) -> OnceLock[U]__init__method#
def __init__() -> NoneCreate an unpopulated OnceLock container.
Returns
OnceLockNone- An unpopulated
OnceLockcontainer.
getmethod#
def get() -> _maybe.Maybe[T]Get the inner value of the OnceLock container if it has been set.
Returns
_maybe.Maybe[T]- Maybe[T]: A
Just-wrapped inner value of theOnceLockcontainer if it has been set, aNothingotherwise.
get_or_initmethod#
def get_or_init(func: Callable[[], T], /) -> TGet the inner value of the OnceLock container, or initialize it with the given function if no value has been set.
Parameters
| Name | Type | Description |
|---|---|---|
func | Callable[[], T] | The function to initialize the OnceLock container with if no value has been set. |
Returns
T- The inner value (maybe newly set) of the
OnceLock
setmethod#
def set(value: T) -> _result.Result[None, T]Set the inner value of the OnceLock container.
If a value has already been set, this method returns an Err containing the existing value.
Parameters
| Name | Type | Description |
|---|---|---|
value | T | The value to set the OnceLock container to. |
Returns
_result.Result[None, T]- Result[None, T]:
Ok(None)if the value was set successfully.Err(value)if the value has already been set, containing the existing value.
unwrapmethod#
def unwrap() -> TGet the inner value of the OnceLock container.
If no value has been set, this method raises a ValueError.
Returns
T- The inner value of the
OnceLockcontainer.
Raises
ValueError- If no value has been set.
