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

optional data for callback? #17

Closed
dmischel opened this issue Jan 25, 2016 · 4 comments
Closed

optional data for callback? #17

dmischel opened this issue Jan 25, 2016 · 4 comments

Comments

@dmischel
Copy link

Hi. I want to use pylru in an application and use the callback option. However, my callback would need additional information in order to execute. I would like to hand that information into the init() function as an optional dictionary. The callback would know what to do with it.

Is this feasible or is there already a way to do this that I have missed?

If this is the wrong place to request a feature (or propose a code mod) please let me know. Thanks,

< david

@jlhutch
Copy link
Owner

jlhutch commented Jan 25, 2016

If I understand what you want, you can do it without changing the Pylru API. Use a nested function to create a closure, thus capturing any variables the callback function needs. Pass the resulting callback function to Pylru at initialization. If you need an example, look at how I implemented the callback function in WriteBackCacheManager.

@dmischel
Copy link
Author

I see your callback is nested the object's init() function. It is able to use attributes of the object. My callback function is nested in a procedure which is not the method of an object, it is just a function. Other than global data (ick) I don't see how to give it the data it needs to perform its function.

Something like this occurred to me. Perhaps I didn't understand how to do this with existing code.

class lrucache(object):
     def __init__(self, size, callback=None, cbk_data=None):
        self.callback = callback
        self.cbk_data = cbk_data
    ...

    def __setitem__(self, key, value):
    ...
        if not node.empty:
            if self.callback is not None:
                self.callback(node.key, node.value, self.cbk_data)

@jlhutch
Copy link
Owner

jlhutch commented Jan 26, 2016

What you want can be done with the current API. I suggest you learn more about nested functions and closures so that you can create the callback function you require. I am not going to change the API of pylru, when it is not necessary. But, you are always free to fork the source and modify it as you see fit.

Best of luck.

@jlhutch jlhutch closed this as completed Jan 26, 2016
@dmischel
Copy link
Author

Thanks. I didn't fully grok closures but now I do. All is well.

< david

On 01/26/2016 10:42 AM, jlhutch wrote:

What you want can be done with the current API. I suggest you learn
more about nested functions and closures so that you can create the
callback function you require. I am not going to change the API of
pylru, when it is not necessary. But, you are always free to fork the
source and modify it as you see fit.

Best of luck.


Reply to this email directly or view it on GitHub
#17 (comment).

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

No branches or pull requests

2 participants