Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /trisongz_lazyops /src /lazyops /libs /lazyload /wrappers.py
| from __future__ import annotations | |
| """ | |
| Lazy Function Wrappers | |
| """ | |
| import functools | |
| from lazyops.libs.pooler import is_coro_func, ThreadPooler | |
| from typing import TypeVar, Callable, Any | |
| WrappedFnReturnT = TypeVar("WrappedFnReturnT") | |
| WrappedFn = TypeVar("WrappedFn", bound = Callable[..., Any]) | |
| ReturnT = TypeVar("ReturnT") | |
| def lazy_function_wrapper( | |
| function: Callable[..., ReturnT], | |
| *function_args, | |
| **function_kwargs, | |
| ) -> Callable[..., ReturnT]: | |
| """ | |
| Lazy Function Wrapper | |
| This function is used to handle lazily initializing a wrapper function (retry, etc) | |
| The execution of the function is deferred until the first call to the wrapper function | |
| If the returned function is None after initialization is called with arguments, then | |
| the initialized function will not be wrapped and the original function will be returned | |
| """ | |
| _initialized = False | |
| _initialized_function = None | |
| def lazywrapped(func: Callable[..., ReturnT]) -> Callable[..., ReturnT]: | |
| """ | |
| Wrapper Function | |
| """ | |
| if is_coro_func(func): | |
| async def _wrapper(*args, **kwargs) -> ReturnT: | |
| """ | |
| Wrapped Function | |
| """ | |
| nonlocal _initialized_function, _initialized | |
| if not _initialized: | |
| _initialized_function = await ThreadPooler.asyncish(function, *function_args, **function_kwargs) | |
| _initialized = True | |
| if _initialized_function is None: | |
| return await func(*args, **kwargs) | |
| return await _initialized_function(func)(*args, **kwargs) | |
| return _wrapper | |
| def _wrapper(*args, **kwargs) -> ReturnT: | |
| """ | |
| Wrapped Function | |
| """ | |
| nonlocal _initialized_function, _initialized | |
| if not _initialized: | |
| _initialized_function = function(*function_args, **function_kwargs) | |
| _initialized = True | |
| if _initialized_function is None: | |
| # If the function is None, return the original function | |
| return func(*args, **kwargs) | |
| return _initialized_function(func)(*args, **kwargs) | |
| return _wrapper | |
| return lazywrapped | |
Xet Storage Details
- Size:
- 2.37 kB
- Xet hash:
- 108b35f688f8b958fbc385ac453bd18fb1d6ae249e0234faf8158a3490c93844
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.