Ticket #8910: trac_8910-combinatorial_class_parent-fh.patch

File trac_8910-combinatorial_class_parent-fh.patch, 6.5 KB (added by hivert, 10 years ago)
  • sage/combinat/combinat.py

    # HG changeset patch
    # User Florent Hivert <Florent.Hivert@univ-rouen.fr>
    # Date 1275319895 -7200
    # Node ID 396ecfecef3602f2d2b350bd257fc176a3cc2d71
    # Parent  1f74ab23ad4815ebf83fdb98f4f5c227304095e2
    #8910: CombinatorialClass inherits for Parent (see the roadmap).
    * * *
    Reduce import loop risk by lazy importing CombinatorialClass
    
    diff --git a/sage/combinat/combinat.py b/sage/combinat/combinat.py
    a b class CombinatorialObject(SageObject): 
    837837        return self._list.index(key)
    838838
    839839
    840    
    841 class CombinatorialClass(SageObject):
     840from sage.misc.classcall_metaclass import ClasscallMetaclass
     841from sage.categories.enumerated_sets import EnumeratedSets
     842class CombinatorialClass(Parent):
     843    """
     844    This class is deprecated, and will disappear as soon as all derived
     845    classes in Sage's library will have been fixed. Please derive
     846    directly from Parent and use the category :class:`EnumeratedSets`,
     847    :class:`FiniteEnumeratedSets`, or :class:`InfiniteEnumeratedSets`, as
     848    appropriate.
     849
     850    For examples, see::
     851
     852        sage: FiniteEnumeratedSets().example()
     853        An example of a finite enumerated set: {1,2,3}
     854        sage: InfiniteEnumeratedSets().example()
     855        An example of an infinite enumerated set: the non negative integers
     856    """
     857    __metaclass__ = ClasscallMetaclass
     858
     859    def __init__(self, category = None, *keys, **opts):
     860        """
     861        TESTS::
     862
     863            sage: C = sage.combinat.combinat.CombinatorialClass()
     864            sage: C.category()
     865            Category of enumerated sets
     866            sage: C.__class__
     867            <class 'sage.combinat.combinat.CombinatorialClass_with_category'>
     868            sage: isinstance(C, Parent)
     869            True
     870            sage: C = sage.combinat.combinat.CombinatorialClass(category = FiniteEnumeratedSets())
     871            sage: C.category()
     872            Category of finite enumerated sets
     873        """
     874        Parent.__init__(self, category = EnumeratedSets().or_subcategory(category))
     875
     876
    842877    def __len__(self):
    843878        """
    844879        __len__ has been removed ! to get the number of element in a
    class CombinatorialClass(SageObject): 
    10301065            sage: P5.element_class
    10311066            <class 'sage.combinat.partition.Partition_class'>
    10321067        """
    1033         assert not isinstance(self, Parent) # Raises an alert if we override the proper definition from Parent
     1068        # assert not isinstance(self, Parent) # Raises an alert if we override the proper definition from Parent
    10341069        if hasattr(self, "object_class"):
    10351070            from sage.misc.misc import deprecation
    10361071            deprecation("Using object_class for specifying the class of the elements of a combinatorial class is deprecated. Please use Element instead")
    class CombinatorialClass(SageObject): 
    10501085            sage: type(p)
    10511086            <class 'sage.combinat.partition.Partition_class'>
    10521087        """
    1053         assert not isinstance(self, Parent) # Raises an alert if we override the proper definition from Parent
     1088        # assert not isinstance(self, Parent) # Raises an alert if we override the proper definition from Parent
    10541089        return self.element_class(x)
    10551090
    10561091    def __list_from_iterator(self):
  • sage/combinat/partition.py

    diff --git a/sage/combinat/partition.py b/sage/combinat/partition.py
    a b class Partitions_all(InfiniteAbstractCom 
    31183118         """
    31193119        return Partitions_n(n)
    31203120
    3121        
    3122        
    3123    
    3124        
     3121
     3122
     3123
     3124
    31253125class Partitions_n(CombinatorialClass):
    31263126    Element = Partition_class
    31273127    def __init__(self, n):
    31283128        """
    31293129        TESTS::
    3130        
     3130
    31313131            sage: p = Partitions(5)
    31323132            sage: p == loads(dumps(p))
    31333133            True
    class Partitions_n(CombinatorialClass): 
    31373137    def __contains__(self, x):
    31383138        """
    31393139        TESTS::
    3140        
     3140
    31413141            sage: p = Partitions(5)
    31423142            sage: [2,1] in p
    31433143            False
    class Partitions_n(CombinatorialClass): 
    31513151    def __repr__(self):
    31523152        """
    31533153        TESTS::
    3154        
     3154
    31553155            sage: repr( Partitions(5) )
    31563156            'Partitions of the integer 5'
    31573157        """
    31583158        return "Partitions of the integer %s"%self.n
    31593159
     3160    def _an_element_(self):
     3161        """
     3162        Returns a partition in ``self``.
     3163
     3164        EXAMPLES::
     3165
     3166            sage: Partitions(4)._an_element_()
     3167            [3, 1]
     3168            sage: Partitions(0)._an_element_()
     3169            []
     3170            sage: Partitions(1)._an_element_()
     3171            [1]
     3172        """
     3173        if self.n == 0:
     3174            lst = []
     3175        elif self.n == 1:
     3176            lst = [1]
     3177        else:
     3178            lst = [self.n-1, 1]
     3179        return self._element_constructor_(lst)
     3180
    31603181    def cardinality(self, algorithm='default'):
    31613182        r"""
    31623183        INPUT:
    class Partitions_n(CombinatorialClass): 
    31753196            faster. *But* PARI has a bug, e.g., on 64-bit Linux
    31763197            PARI-2.3.2 outputs numbpart(147007)%1000 as 536, but it
    31773198            should be 533!.  So do not use this option.
    3178        
     3199
    31793200          - ``'default'`` - ``'bober'`` when k is not specified;
    31803201            otherwise use ``'gap'``.
    31813202       
  • sage/combinat/words/words.py

    diff --git a/sage/combinat/words/words.py b/sage/combinat/words/words.py
    a b def Words(alphabet=None, length=None, fi 
    109109                return FiniteWords_length_k_over_OrderedAlphabet(alphabet, length)
    110110    raise ValueError, "do not know how to make a combinatorial class of words from your input"
    111111
    112 from sage.misc.nested_class import NestedClassMetaclass
    113112from sage.structure.unique_representation import UniqueRepresentation
    114113class Words_all(InfiniteAbstractCombinatorialClass):
    115     __metaclass__ = NestedClassMetaclass
    116114    r"""
    117115    TESTS::
    118116
  • sage/sets/family.py

    diff --git a/sage/sets/family.py b/sage/sets/family.py
    a b from sage.categories.enumerated_sets imp 
    2828from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets
    2929from sage.categories.infinite_enumerated_sets import InfiniteEnumeratedSets
    3030from sage.sets.finite_enumerated_set import FiniteEnumeratedSet
    31 from sage.combinat.combinat import CombinatorialClass
     31from sage.misc.lazy_import import lazy_import
     32lazy_import('sage.combinat.combinat', 'CombinatorialClass')
    3233from sage.rings.integer import Integer
    3334from sage.misc.misc import AttrCallObject
    3435from sage.misc.constant_function import ConstantFunction