[issue41220] add optional make_key argument to lru_cache

2020-07-17 Thread Raymond Hettinger
Raymond Hettinger added the comment: I've left this open for a week to collect comments. I concur with Felipe that this should be the responsibility of the caller. And I concur with Jim that the use cases are likely too rare to warrant shoehorning in the extra functionality. For my part, I

[issue41220] add optional make_key argument to lru_cache

2020-07-11 Thread Jim Jewett
Jim Jewett added the comment: Going back to Raymond's analysis, this is useful when at least some of the parameters either do not change the result, or are not hashable. At a minimum, you need to figure out which parameters those are, and whether to drop them or transform them. Is this alre

[issue41220] add optional make_key argument to lru_cache

2020-07-11 Thread Itay azolay
Itay azolay added the comment: Hey Felipe! Thanks for stepping in! I do get your argument. However, in my opinion, I try to argue the same thing for max or sorted. "if one wants to use `sorted`, they should make sure their arguments are comparable". However, it is not the case, since we do ha

[issue41220] add optional make_key argument to lru_cache

2020-07-10 Thread Felipe Rodrigues
Felipe Rodrigues added the comment: Correction: (...) on the _caller_ to solve the hashing* issue (...) -- ___ Python tracker ___ _

[issue41220] add optional make_key argument to lru_cache

2020-07-10 Thread Felipe Rodrigues
Felipe Rodrigues added the comment: Hello all, I love discussions about caching! While I do see the point of Itayazolay's proposal, I think that it should be on the _caller_ to solve the caching issue, and not on the _callee_ to provide a way to make it happen. That is, I think that if one

[issue41220] add optional make_key argument to lru_cache

2020-07-08 Thread Itay azolay
Itay azolay added the comment: Thanks, you have some very good points. Let my try to address them * cache functions really are expected to be cheap, but what they really need to be is *cheaper*. If my computation is expensive enough, I might be okay with making a less, still somewhat expensi

[issue41220] add optional make_key argument to lru_cache

2020-07-07 Thread Raymond Hettinger
Raymond Hettinger added the comment: Thanks, I see what you're trying to do now: 1) Given a slow function 2) that takes a complex argument 2a) that includes a hashable unique identifier 2b) and some unhashable data 3) Cache the function result using only the unique identifier The l

[issue41220] add optional make_key argument to lru_cache

2020-07-07 Thread Itay azolay
Itay azolay added the comment: Yes, you're right. It's a bad example, I tried to simplify it, and I ended up oversimplifying it. Real-life cases are of course more complicated. What I wanted to accomplish, is very similar to the `key` argument in sorted/min/max/etc.. let my try and give you an

[issue41220] add optional make_key argument to lru_cache

2020-07-06 Thread Raymond Hettinger
Change by Raymond Hettinger : -- Removed message: https://bugs.python.org/msg373196 ___ Python tracker ___ ___ Python-bugs-list mail

[issue41220] add optional make_key argument to lru_cache

2020-07-06 Thread Raymond Hettinger
Raymond Hettinger added the comment: >>> from functools import lru_cache >>> def my_make_key(my_list): ... return my_list[0] ... >>> @lru_cache(128, make_key=my_make_key) ... def cached_func(my_list): ... return sum(my_list) ... >>> cached_func([10, 20, 30]) 60 >>> cached_func([10, 11, 12])

[issue41220] add optional make_key argument to lru_cache

2020-07-06 Thread Raymond Hettinger
Raymond Hettinger added the comment: What is the given example trying to accomplish? It looks like every possible input list would be cached using just the first element of the list. Would cached_func([10, 20, 30]) and cached_func([10, 11, 12]) return the same sum? If so, why would that b

[issue41220] add optional make_key argument to lru_cache

2020-07-06 Thread Raymond Hettinger
Change by Raymond Hettinger : -- Removed message: https://bugs.python.org/msg373194 ___ Python tracker ___ ___ Python-bugs-list mail

[issue41220] add optional make_key argument to lru_cache

2020-07-06 Thread Raymond Hettinger
Raymond Hettinger added the comment: What is the given example trying to accomplish? It looks like every possible input list would be cached using just the first element of the list. Would cached_func([10, 20, 30]) and cached_func([10, 11, 12]) return the same sum? If not, why would that

[issue41220] add optional make_key argument to lru_cache

2020-07-06 Thread Karthikeyan Singaravelan
Change by Karthikeyan Singaravelan : -- nosy: +rhettinger ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe: htt

[issue41220] add optional make_key argument to lru_cache

2020-07-06 Thread Bar Harel
Bar Harel added the comment: May I suggest calling it key? Will comply with other Python functions. -- nosy: +bar.harel ___ Python tracker ___

[issue41220] add optional make_key argument to lru_cache

2020-07-06 Thread Itay azolay
Change by Itay azolay : -- keywords: +patch pull_requests: +20499 stage: -> patch review pull_request: https://github.com/python/cpython/pull/21353 ___ Python tracker ___

[issue41220] add optional make_key argument to lru_cache

2020-07-06 Thread Itay azolay
New submission from Itay azolay : I'd like to add optional argument to lru_cache. This argument is a user given function that will replace the default behaviour of creating a key from the args/kwds of the function. for example: def my_make_key(my_list): return my_list[0] @lru_cache(128, ma