Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable runtime cashing #167

Closed
Borda opened this issue Feb 13, 2024 · 2 comments · Fixed by #168
Closed

enable runtime cashing #167

Borda opened this issue Feb 13, 2024 · 2 comments · Fixed by #168

Comments

@Borda
Copy link
Contributor

Borda commented Feb 13, 2024

I have a use case where I pass to a function function func1 another function func2 as input, and then inside the function func1, new arguments are prepared and calling func2 with these new arguments... as this function func2 could be one of many it is not very practical to add decorator for each option but instead I would like to apply wrapper inside func1...
A demonstration is:

from functools import wraps, partial

def my_decorator(f):
    @wraps(f)
    def wrapper(*args, **kwds):
        print('Calling decorated function')
        return f(*args, **kwds)
    return wrapper

# @my_decorator
def example(a, b=2):
    """Docstring"""
    print('Called example function with arguments:', a, b)

my_decorator(example)(1)
# example.__name__
# example.__doc__

example_ = partial(example, b=4)
my_decorator(example_)(1)

It is related to my work on Lightning-AI/torchmetrics#2335 where some functions can have implementation inside test, some would be coming straight from sklearn and some would be with functools.partial

@shaypal5
Copy link
Collaborator

Ok. Nice. That's a valid use case.
Definitely would be cool to support this, as long as nothing else breaks, slows down or becomes more complex to maintain.

You got any implementation ideas?

@Borda
Copy link
Contributor Author

Borda commented Feb 13, 2024

You got any implementation ideas?

I tried to use cachier in the same fashion

from functools import partial

from cachier import cachier

def example(a, b=2):
    """Docstring"""
    print('Called example function with arguments:', a, b)

cachier()(example)(1)
# example.__name__
# example.__doc__

example_ = partial(example, b=4)
cachier()(example_)(1)

and now trying to debug why it fails...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants