I wonder if it is possible  to use CachedFunction in Cython.
Caching a python function in (s)pyx file does not seem to work:

On Jan 15, 12:31 am, Dima Pasechnik <dimp...@gmail.com> wrote:
> I wonder if it is possible  to use CachedFunction in Cython.
> I try doing
> sage: load 'cf.spyx'
>
> where the file is
>
> # ---- cf.spyx
> from sage.misc.cachefunc import CachedFunction
> @CachedFunction
> def y():
>   return 2000*1234
> #-------------
>
> and it does not work --- I get
> Compiling ./cf.spyx...
> ---------------------------------------------------------------------------
> AttributeError                            Traceback (most recent call
> last)
>
> /Users/dima/Downloads/<ipython console> in <module>()
>
> /usr/local/src/sage/current/local/lib/python2.6/site-packages/sage/
> misc/preparser.pyc in load(filename, globals, attach)
>    1593     elif fpath.endswith('.spyx') or fpath.endswith('.pyx'):
>    1594         import interpreter
> -> 1595         exec(interpreter.load_cython(fpath), globals)
>    1596     elif fpath.endswith('.m'):
>    1597         # Assume magma for now, though maybe .m is used by
> maple and
>
> /Users/dima/Downloads/<string> in <module>()
>
> /Users/dima/Downloads/_Users_dima_Downloads_cf_spyx_1.pyx in init
> _Users_dima_Downloads_cf_spyx_1 (_Users_dima_Downloads_cf_spyx_1.c:589)
> ()
>       5 include "cdefs.pxi"
>       6 from sage.misc.cachefunc import CachedFunction
> ----> 7 @CachedFunction
>       8 def y():
>       9    return 2000*1234
>
> /usr/local/src/sage/current/local/lib/python2.6/site-packages/sage/
> misc/cachefunc.pyc in __init__(self, f, classmethod)
>      56
>      57         """
> ---> 58         self._common_init(f,
> ArgumentFixer(f,classmethod=classmethod))
>      59         self.cache = {}
>      60
>
> /usr/local/src/sage/current/local/lib/python2.6/site-packages/sage/
> misc/function_mangling.pyc in __init__(self, f, classmethod)
>     107         """
>     108
> --> 109         defaults = f.func_defaults
>     110         if defaults is None:
>     111             defaults = []
>
> AttributeError: 'builtin_function_or_method' object has no attribute
> 'func_defaults'
> sage:
>
> Thanks,
> Dima
>
> On Dec 13 2010, 2:36 pm, John H Palmieri <jhpalmier...@gmail.com>
> wrote:
>
>
>
> > On Dec 13, 2:02 pm, Gagan Sekhon <gagan.d.sek...@gmail.com> wrote:
>
> > > I am currently working on Hyperelliptic curves over finite fields.
> > > There are few functions defined and most depend of the function
> > > before. I was hoping to use caching, so it saves time. Does anyone
> > > have a reference on how to use caching in Python. I have done several
> > > search_src and google searches, but had very little luck.
>
> > I don't know about Python in general, but in Sage, look at SAGE_ROOT/
> > devel/sage/sage/misc/cachefunc.py.  (You can find this, along with
> > many other hits, if you do search_src('cached', 'function') from
> > within Sage.)  With it, you can do
>
> >   from sage.misc.cachefunc import CachedFunction
>
> >   def not_cached(...):
> >      ....
> >   this_is_cached = CachedFunction(not_cached)
>
> > or
>
> >   from sage.misc.cachefunc import CachedFunction
>
> >   @CachedFunction
> >   def this_is_also_cached(...):
> >      ...
>
> > Then you can call 'this_is_cached' with the same arguments as
> > 'not_cached', or call 'this_is_also_cached' with the appropriate
> > arguments.
>
> > --
> > John

-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to