#14214: Cythoned homsets
----------------------------------------------+-----------------------------
       Reporter:  SimonKing                   |         Owner:  tbd             
                 
           Type:  enhancement                 |        Status:  needs_work      
                 
       Priority:  major                       |     Milestone:  sage-5.10       
                 
      Component:  performance                 |    Resolution:                  
                 
       Keywords:  Hom, cython, cached method  |   Work issues:  How to store 
domain and codomain?
Report Upstream:  N/A                         |     Reviewers:                  
                 
        Authors:  Simon King                  |     Merged in:                  
                 
   Dependencies:  #14159, #12951, #13184      |      Stopgaps:                  
                 
----------------------------------------------+-----------------------------

Comment (by SimonKing):

 My frustration level raises.

 Ideally, one would write a descriptor
 - that takes the value from `__cached_methods`, because this is much
 faster than relying on `__getattr__` (which looks into `__cached_methods`
 as well)
 - that can be overridden on the instance's `__dict__`, because python
 attribute access is faster than accessing the property, and in addition
 the property access tends to get slower on sub-classes.


 Didn't you say that a property that only defines `__get__` can be
 overridden in the instance's dict? So, I thought I just define `__get__`
 and not set, and put data into `__dict__` when possible.

 However, it does not work:
 {{{
 sage: cython("""
 ....: from sage.all import Sets
 ....: from sage.structure.parent cimport Parent
 ....: cdef class Bla(Parent):
 ....:     def __init__(self, D,C):
 ....:         try:
 ....:             self.__dict__['_domain'] = D
 ....:             self.__dict__['_codomain'] = C
 ....:         except AttributeError:
 ....:             if self.__cached_methods is None:
 ....:                 self.__cached_methods = {'_domain':D, '_codomain':C}
 ....:             else:
 ....:                 self.__cached_methods['_domain'] = D
 ....:                 self.__cached_methods['_codomain'] = C
 ....:         Parent.__init__(self, category = Sets())
 ....:     property _domain:
 ....:         def __get__(self):
 ....:             try:
 ....:                 return self.__cached_methods['_domain']
 ....:             except KeyError:
 ....:                 raise AttributeError("%s instance has no attribute
 '_domain'"%type(self))
 ....: """)
 ....:
 sage: class Blubb(Bla): pass
 sage: b = Bla(1,2)
 sage: c = Blubb(1,2)
 sage: b._domain
 1
 sage: c._domain
 ---------------------------------------------------------------------------
 AttributeError                            Traceback (most recent call
 last)
 <ipython-input-19-9505626a8e7d> in <module>()
 ----> 1 c._domain

 /home/simon/SAGE/prerelease/sage-5.9.rc0/local/lib/python2.7/site-
 packages/sage/structure/parent.so in
 sage.structure.parent.Parent.__getattr__ (sage/structure/parent.c:6501)()

 /home/simon/SAGE/prerelease/sage-5.9.rc0/local/lib/python2.7/site-
 packages/sage/structure/misc.so in
 sage.structure.misc.getattr_from_other_class
 (sage/structure/misc.c:1591)()

 AttributeError: 'Blubb_with_category' object has no attribute '_domain'
 sage: c.__dict__['_domain']
 1
 }}}

 So, can you please clarify: What kind of descriptors can be overridden in
 `__dict__`?

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/14214#comment:38>
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 unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to