#11115: Rewrite cached_method in Cython
---------------------------+------------------------------------------------
   Reporter:  SimonKing    |          Owner:  jason                
       Type:  enhancement  |         Status:  needs_review         
   Priority:  major        |      Milestone:  sage-4.7             
  Component:  misc         |       Keywords:  category cython cache
Work_issues:               |       Upstream:  N/A                  
   Reviewer:               |         Author:  Simon King           
     Merged:               |   Dependencies:                       
---------------------------+------------------------------------------------
Changes (by SimonKing):

  * status:  needs_work => needs_review


Comment:

 The new patch is posted. Apart from fixing the categorical trouble with
 cached_in_parent methods, it turns several doc strings into raw strings
 (they contain backslashes). Please look at the documentation (I'll build
 it as well)!

 Here is the new doc test, demonstrating that cached_in_parent works with
 categories:
 {{{
 sage: cython_code = ["from sage.structure.parent cimport Parent",
 ... "from sage.structure.element cimport Element",
 ... "from sage.all import Category, cached_in_parent_method",
 ... "cdef class MyElement(Element):",
 ... "    cdef object x",
 ... "    def __init__(self,P,x):",
 ... "        self.x=x",
 ... "        Element.__init__(self,P)",
 ... "    def _repr_(self):",
 ... "        return '<%s>'%self.x",
 ... "    def __neg__(self):",
 ... "        return MyElement(self.parent(),-self.x)",
 ... "    def __hash__(self):",
 ... "        return hash(self.x)",
 ... "    def __cmp__(self, other):",
 ... "        return cmp(self.x, (<MyElement>other).x)",
 ... "cdef class MyParent(Parent): pass",
 ... "class MyCategory(Category):",
 ... "    def super_categories(self):",
 ... "        return [Objects()]",
 ... "    class ElementMethods:",
 ... "        @cached_in_parent_method",
 ... "        def test_cache(self):",
 ... "            return -self"]
 sage: cython('\n'.join(cython_code))
 sage: C = MyCategory()
 sage: P = MyParent(category=C)
 sage: e1 = MyElement(P,5)
 sage: e2 = MyElement(P,5)
 sage: e3 = MyElement(P,6)
 }}}
 We verify that attribute assignment does not work:
 {{{
 sage: e1.bla = 1
 Traceback (most recent call last):
 ...
 AttributeError: '...MyElement' object has no attribute 'bla'
 sage: P.bla = 1
 Traceback (most recent call last):
 ...
 AttributeError: '...MyParent' object has no attribute 'bla'
 }}}
 Nevertheless, the cached method works, and it returns identical
 output for equal elements, as expected:
 {{{
 sage: e1.test_cache()
 <-5>
 sage: e1 is e2
 False
 sage: e1 == e2
 True
 sage: e2.test_cache() is e1.test_cache()
 True
 sage: e1 == e3
 False
 sage: e2.test_cache() == e3.test_cache()
 False
 }}}

 Back to the discussion about attributes: It is due to the
 `__cached_methods` attribute of `P` that the cache does not break in the
 example. It is not needed that the elements have that attribute.

 Depends on #9976

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/11115#comment:27>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica, 
and MATLAB

-- 
You received this message because you are subscribed to the Google Groups 
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sage-trac?hl=en.

Reply via email to