Ticket #4301: 4301-cat-lookup.patch

File 4301-cat-lookup.patch, 3.0 KB (added by robertwb, 14 years ago)
  • sage/categories/category.py

    # HG changeset patch
    # User Robert Bradshaw <robertwb@math.washington.edu>
    # Date 1223936134 25200
    # Node ID 029b47ff119d51aef6f6d6dcc8fc53e294ad1f7f
    # Parent  e594bdbd639233c2915c5f4e25e6d9fea96fd59d
    Generic object and element methods on categories.
    
    diff --git a/sage/categories/category.py b/sage/categories/category.py
    a b  
    161161
    162162    def category(self):
    163163        return Objects()
     164       
     165    def get_object_method(self, method_name):
     166        return getattr(self, "object_%s" % method_name, None)
    164167
     168    def get_element_method(self, method_name):
     169        return getattr(self, "element_%s" % method_name, None)
     170       
    165171def is_Category(x):
    166172    """
    167173    Returns True if x is a category.
  • sage/structure/category_object.pyx

    diff --git a/sage/structure/category_object.pyx b/sage/structure/category_object.pyx
    a b  
    4040    sage: M.gens()
    4141    ((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1))
    4242"""
     43
     44import functools
    4345
    4446cdef int bad_parent_warnings = 0
    4547
     
    103105            self._category = Objects()
    104106        return self._category
    105107
     108    def __getattr__(self, name):
     109        """
     110        This function is implemented to provide the ability to place generic
     111        algorithms on the category itself. To give every object in a given
     112        category a method FOO, simply implement object_FOO on that category. 
     113        """
     114        if self._category is None:
     115            return sage_object.SageObject.__getattribute__(self, name)
     116        attr = self._category.get_object_method(name)
     117        if attr is None:
     118            return sage_object.SageObject.__getattribute__(self, name)
     119        else:
     120            if callable(attr):
     121                return functools.partial(attr, self)
     122            else:
     123                return attr
    106124
    107125    ##############################################################################
    108126    # Generators
  • sage/structure/element.pyx

    diff --git a/sage/structure/element.pyx b/sage/structure/element.pyx
    a b  
    141141   so if no-one has defined a python implementation, the correct pyrex
    142142   implementation will get called.
    143143"""
     144
     145import functools
    144146
    145147
    146148cdef extern from *:
     
    292294            return self._parent
    293295        else:
    294296            return self._parent(x)
     297
     298    def __getattr__(self, name):
     299        """
     300        This function is implemented to provide the ability to place generic
     301        algorithms on the category itself. To give every element in a given
     302        category a method FOO, simply implement element_FOO on that category. 
     303        """
     304        attr = self._parent.category().get_element_method(name)
     305        if attr is None:
     306            return sage_object.SageObject.__getattribute__(self, name)
     307        else:
     308            if callable(attr):
     309                return functools.partial(attr, self)
     310            else:
     311                return attr
    295312
    296313
    297314    def subs(self, in_dict=None, **kwds):