# HG changeset patch
# User Sebastien Labbe <slabqc at gmail.com>
# Date 1270071551 -7200
# Node ID b21664c9fef4cddacd14b25a6a37daaceb0bba69
# Parent 25a7c61c35422d611cd9886052b40edff8c79633
#8574: word length methods improvement
diff --git a/sage/combinat/words/abstract_word.py b/sage/combinat/words/abstract_word.py
a
|
b
|
from sage.structure.sage_object import S |
34 | 34 | from sage.misc.lazy_attribute import lazy_attribute |
35 | 35 | from sage.combinat.words.word_options import word_options |
36 | 36 | from itertools import islice, izip, groupby |
37 | | from sage.rings.all import Integers, ZZ |
| 37 | from sage.rings.all import Integers, ZZ, Infinity |
38 | 38 | |
39 | 39 | class Word_class(SageObject): |
40 | 40 | def parent(self): |
… |
… |
class Word_class(SageObject): |
147 | 147 | """ |
148 | 148 | return self._len |
149 | 149 | |
150 | | __len__ = length |
| 150 | def __len__(self): |
| 151 | r""" |
| 152 | Return the length of self (as a python integer). |
| 153 | |
| 154 | ..NOTE:: |
| 155 | |
| 156 | For infinite words or words of unknown length, use length method |
| 157 | instead. |
| 158 | |
| 159 | OUTPUT: |
| 160 | |
| 161 | positive integer |
| 162 | |
| 163 | EXAMPLES:: |
| 164 | |
| 165 | sage: len(Word(lambda n:n, length=1000)) |
| 166 | 1000 |
| 167 | sage: len(Word(iter('a'*200), length='finite')) |
| 168 | 200 |
| 169 | |
| 170 | We make sure #8574 is fixed:: |
| 171 | |
| 172 | sage: s = WordMorphism('0->000,1->%s'%('1'*100)) |
| 173 | sage: len(s('1')) |
| 174 | 100 |
| 175 | |
| 176 | For infinite word:: |
| 177 | |
| 178 | sage: len(Word(lambda n:n)) |
| 179 | Traceback (most recent call last): |
| 180 | ... |
| 181 | TypeError: Python len method can not return a non integer value (=+Infinity): use length method instead. |
| 182 | sage: len(Word(iter('a'*200))) |
| 183 | Traceback (most recent call last): |
| 184 | ... |
| 185 | TypeError: Python len method can not return a non integer value (=+Infinity): use length method instead. |
| 186 | |
| 187 | For word of unknown length:: |
| 188 | |
| 189 | sage: len(Word(iter('a'*200), length='unknown')) |
| 190 | Traceback (most recent call last): |
| 191 | ... |
| 192 | TypeError: Python len method can not return a non integer value (=None): use length method instead. |
| 193 | """ |
| 194 | L = self.length() |
| 195 | if L is None or L is Infinity: |
| 196 | msg = "Python len method can not return a non integer value (=%s): "%L |
| 197 | msg += "use length method instead." |
| 198 | raise TypeError, msg |
| 199 | return int(L) |
151 | 200 | |
152 | 201 | def __cmp__(self, other): |
153 | 202 | r""" |