Slight correction: The @cached_property and the @property @cache decorator combo are functionally identical as I stated. While they do the same thing, they do it in different ways.. For example, you can't use a @cached_property in a slotted class, but you can use the other method just fine. @cached_property also allows writes by default.
Careful with an example like the one at 7:00. If instead of 3, x was bound to a mutable object, and then a mutable operation was done to that object, the change would be reflected in partial.
Thanks for the video! Only just discovered your channel and I've already discovered a fair few new things or finally grasp things I didn't fully understand before :) Really like the way you explain things!
That partial function seems useful when you don't have all arguments at the same time. Some times you'll need to process something or wait for an api response to get the rest of the arguments.
great video, i have a question. In the next script, why does the function only print "adding" twice?, i think it should print 3 times, one for each different sequence number. @cache def add(value1, value2): print('adding') return value1 + value2 sequence = [1, 1, 2, 3, 1, 2, 2] for num in sequence: print(add(2, num))
You might use cache with a function that accesses a slow resource such over a network, where you don't care about any updates that may happen to that resource. If you needed to read a configuration file from say a web server or s3 for example.
that only works if you only use the wrapper on the add function, but the goal of a decorator is to be used on many different functions on which you want to add the same bit of actions. In this example if you wrote another function called "substract" you can apply the with_greetings decorator on both add and substract and it will still work properly for both
Thank you, glad you like it! Afaik you can't do that with @cache, but there are some options available: 1. Build your own implementation on top of @cache 2. Use Redis -- it's designed for on-disk caching (so it'll persist between runtimes), and it's ludicrously quick. I don't have a video on the channel, but sounds like it could be a nice idea.
For basic problems where running redis, you could wrap wrap read/write a pickled object. Obviously, this has some security and performance implications, so may not be appropriate
I am guessing that if you have some random process in a function, using the @cache decorator will not be convenient. That would be useful for deterministic functions....a word of caution! Also, maybe calculating some Fibonacci with the @cached property would be an interesting exercise!
Slight correction: The @cached_property and the @property @cache decorator combo are functionally identical as I stated. While they do the same thing, they do it in different ways.. For example, you can't use a @cached_property in a slotted class, but you can use the other method just fine. @cached_property also allows writes by default.
Careful with an example like the one at 7:00. If instead of 3, x was bound to a mutable object, and then a mutable operation was done to that object, the change would be reflected in partial.
thank you for clarifying,
had functools question with the wraps and didn’t know how to answer, now i do
Thanks for the video! Only just discovered your channel and I've already discovered a fair few new things or finally grasp things I didn't fully understand before :)
Really like the way you explain things!
Glad I could help out! (:
always great stuff man 👌 Definitely will be using cache and partial on some projects
Thanks! Yeah I've used partial before, but never played too much with caching, something I'll deffo be changing.
Please increase the font size in your editor for the video. It's very hard for me to read on my phone.
You cAn zoom now
How do you zoom?
@@johnstarfire
That partial function seems useful when you don't have all arguments at the same time. Some times you'll need to process something or wait for an api response to get the rest of the arguments.
Very useful lesson. Thank you!
you are amazing man! cheers and thanks from Chile!
Thank you!
Thanks, I'd not actually seen wraps or cache before. I've been manually implementing these for years...
great video, i have a question. In the next script, why does the function only print "adding" twice?, i think it should print 3 times, one for each different sequence number.
@cache
def add(value1, value2):
print('adding')
return value1 + value2
sequence = [1, 1, 2, 3, 1, 2, 2]
for num in sequence:
print(add(2, num))
Thanks! Just ran it myself and it prints three times for me. I almost missed the first time it printed adding, maybe you did the same?
10:00 it's weird to me to use time.sleep() and @cache in/on the same function, what can be a true use of that combination ?
I just used the sleep to represent a function that takes a long time to execute. You'd probably never need to combine the two in the real world.
You might use cache with a function that accesses a slow resource such over a network, where you don't care about any updates that may happen to that resource. If you needed to read a configuration file from say a web server or s3 for example.
ohhh partial is supposed to be the way to do it
i have always been setting up an anonymous lambda function to preload the variables
What if you called the wrapper add?
that only works if you only use the wrapper on the add function, but the goal of a decorator is to be used on many different functions on which you want to add the same bit of actions. In this example if you wrote another function called "substract" you can apply the with_greetings decorator on both add and substract and it will still work properly for both
Really nice channel!
How can @cache be exported or saved? I'd like it to not reset every runtime for certain functions and properties :)
Thanks🙏 👍
Thank you, glad you like it! Afaik you can't do that with @cache, but there are some options available:
1. Build your own implementation on top of @cache
2. Use Redis -- it's designed for on-disk caching (so it'll persist between runtimes), and it's ludicrously quick. I don't have a video on the channel, but sounds like it could be a nice idea.
@@Carberra thanks for the quick reply! Already using redis :P
Can probably make a @cache_redis pretty easily, thanks :)
Yeah, I think the way my work does it is by creating a decorator and decorating the wrapper function with the @cache deco.
For basic problems where running redis, you could wrap wrap read/write a pickled object. Obviously, this has some security and performance implications, so may not be appropriate
Joblib's cache does it this way (w/ pickle) (no need to build it yourself)
I am guessing that if you have some random process in a function, using the @cache decorator will not be convenient. That would be useful for deterministic functions....a word of caution! Also, maybe calculating some Fibonacci with the @cached property would be an interesting exercise!
You've more or less predicted a video going live in a few weeks! I focus specifically on caching there, including a Fibonacci example! 😄
If only it were possible to include even MORE unused space on the right and bottom of the screen so that the text was even smaller . . .
get a bigger screen
that's helpful - thanks@@7DYNAMIN
partial does here what curry does in other languages.
Ok, half of the screen is useless and the other half is unreadable
I think the explanation should be structured more, and terminology like "some weirdness" doesn't help me comprehend the topic either.
Nice way of implementing en.wikipedia.org/wiki/Currying and en.wikipedia.org/wiki/Memoization in Python!