ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 30 May 2018 00:25:00 -0500How to use @lru_cachehttps://ask.sagemath.org/question/42459/how-to-use-lru_cache/ I am using the notebook of Sagemath 8.1. Is there a way to use the python command @lru_cache in the files with extension .ipynb ? Or in Python 2?(it is the version that is provided in the notebook.)Tue, 29 May 2018 09:41:31 -0500https://ask.sagemath.org/question/42459/how-to-use-lru_cache/Answer by nbruin for <p>I am using the notebook of Sagemath 8.1. Is there a way to use the python command @lru_cache in the files with extension .ipynb ? Or in Python 2?(it is the version that is provided in the notebook.)</p>
https://ask.sagemath.org/question/42459/how-to-use-lru_cache/?answer=42460#post-id-42460As the question points out, this is really about python2. One would expect the answer to be "yes" and it is:
https://stackoverflow.com/questions/11815873/memoization-library-for-python-2-7
some care is needed to install the required library in sage's python and not in another. So use something like
$ sage -sh
sage-shell$ pip install repoze.lruTue, 29 May 2018 10:59:26 -0500https://ask.sagemath.org/question/42459/how-to-use-lru_cache/?answer=42460#post-id-42460Comment by nbruin for <p>As the question points out, this is really about python2. One would expect the answer to be "yes" and it is:</p>
<p><a href="https://stackoverflow.com/questions/11815873/memoization-library-for-python-2-7">https://stackoverflow.com/questions/1...</a></p>
<p>some care is needed to install the required library in sage's python and not in another. So use something like</p>
<pre><code>$ sage -sh
sage-shell$ pip install repoze.lru
</code></pre>
https://ask.sagemath.org/question/42459/how-to-use-lru_cache/?comment=42465#post-id-42465One way would be to implement hashing for the elements you want to hash. Alternatively, you could change the input of your function: it may be the case that you can, for instance, describe your element using a tuple of integers. That is hashable, so if you change your input to that then you can use lru_cache.
You could then wrap your cached function in a function that takes the input type you like.
In cases like this, it's usually easier and faster to implement your own caching rather than bend to the needs of a decorator.Wed, 30 May 2018 00:25:00 -0500https://ask.sagemath.org/question/42459/how-to-use-lru_cache/?comment=42465#post-id-42465Comment by Sara for <p>As the question points out, this is really about python2. One would expect the answer to be "yes" and it is:</p>
<p><a href="https://stackoverflow.com/questions/11815873/memoization-library-for-python-2-7">https://stackoverflow.com/questions/1...</a></p>
<p>some care is needed to install the required library in sage's python and not in another. So use something like</p>
<pre><code>$ sage -sh
sage-shell$ pip install repoze.lru
</code></pre>
https://ask.sagemath.org/question/42459/how-to-use-lru_cache/?comment=42464#post-id-42464I have used:
from functools32 import lru_cache
@lru_cache()
and it works. However my functions takes elements that probably are not hashable. Indeed I get this error
TypeError: <class 'sage.combinat.root_system.fundamental_group.FundamentalGroupOfExtendedAffineWeylGroup_Class_with_category.element_class'> is not hashable
Why elements of a group are not hashable? Is there a way to use @lru_cache() for my function?Tue, 29 May 2018 18:12:03 -0500https://ask.sagemath.org/question/42459/how-to-use-lru_cache/?comment=42464#post-id-42464