Skip to content

Commit

Permalink
Inflated inline if statement.
Browse files Browse the repository at this point in the history
  • Loading branch information
umarbutler committed May 6, 2024
1 parent 63b8b8b commit 681e34e
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/persist_cache/persist_cache.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import inspect
import os
from datetime import timedelta
import inspect

from functools import wraps
from datetime import timedelta
from typing import Any, Callable, Union

from . import caching
from .caching import NOT_IN_CACHE
from .helpers import inflate_arguments, is_async, signaturize, is_generator
from .helpers import inflate_arguments, is_async, signaturize


def cache(
Expand Down Expand Up @@ -89,6 +90,7 @@ async def async_wrapper(*args: tuple[Any], **kwargs: dict[str, Any]) -> Any:

return value

# Initialise a wrapper for generator functions.
def generator_wrapper(*args: tuple[Any], **kwargs: dict[str, Any]) -> Any:
nonlocal dir, expiry, is_method

Expand All @@ -106,13 +108,21 @@ def generator_wrapper(*args: tuple[Any], **kwargs: dict[str, Any]) -> Any:
yield item

caching.set(key, value, dir)

return

for item in value:
yield item

# Identify the appropriate wrapper for the function by checking whether it is asynchronous or not.
wrapper = generator_wrapper if is_generator(func) else async_wrapper if is_async(func) else sync_wrapper
# Identify the appropriate wrapper for the function,
if is_async(func):
wrapper = async_wrapper

elif inspect.isgeneratorfunction(func):
wrapper = generator_wrapper

else:
wrapper = sync_wrapper

# Attach convenience functions to the wrapper for modifying the cache.
def delete_cache() -> None:
Expand Down

0 comments on commit 681e34e

Please sign in to comment.