Note
This module will be deleted in a future version of Sage. Most of the commands here have been already deleted; only the commands needed for unpickling word objects saved with older versions of Sage (pre 4.1) have been kept (for now).
Builds the content for a word.
INPUT:
OUTPUT:
TESTS:
sage: from sage.combinat.words.word_content import BuildWordContent
sage: from itertools import count, imap, repeat
sage: len(BuildWordContent(None))
doctest:...: DeprecationWarning: WordContentFromList is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
doctest:...: DeprecationWarning: WordContentFromList is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
0
sage: len(BuildWordContent(None, format='empty'))
0
sage: c = BuildWordContent(['a', 'b'], format='empty')
...
TypeError: trying to build an empty word with something other than None
sage: len(BuildWordContent(['0', '1', '1'], int))
doctest:...: DeprecationWarning: WordContentFromList is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
3
sage: len(BuildWordContent(['0', '1', '1'], int, format='list'))
3
sage: BuildWordContent(10, format='list')
...
TypeError: trying to build a word backed by a list with a sequence not providing the required operations
sage: c = BuildWordContent(lambda x: x%2)
doctest:...: DeprecationWarning: WordContentFromFunction is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
doctest:...: DeprecationWarning: WordContentFromFunction is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
sage: len(BuildWordContent(lambda x: x%3, part=slice(0,10)))
10
sage: c = BuildWordContent(repeat(1))
doctest:...: DeprecationWarning: WordContentFromIterator is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
doctest:...: DeprecationWarning: WordContentFromIterator is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
sage: len(BuildWordContent(count(), part=slice(10)))
10
sage: len(BuildWordContent(count(), part=slice(10, 0, -2)))
doctest:...: DeprecationWarning: WordContentFromList is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
5
Class that acts as a function to concatenate contents.
Returns the character at position key in the word.
EXAMPLES:
sage: from sage.combinat.words.word_content import ConcatenateContent, BuildWordContent
sage: c = ConcatenateContent((BuildWordContent('1221', int), BuildWordContent('2112', int)))
doctest:...: DeprecationWarning: ConcatenateContent is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
sage: c(0)
1
sage: c(7)
2
TESTS:
sage: from sage.combinat.words.word_content import ConcatenateContent, BuildWordContent
sage: c = ConcatenateContent((BuildWordContent([1, 2]),))
doctest:...: DeprecationWarning: ConcatenateContent is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
sage: len(c)
2
sage: c = ConcatenateContent((BuildWordContent('1221', int), BuildWordContent('2112', int)))
sage: len(c)
8
TESTS:
sage: from sage.combinat.words.word_content import ConcatenateContent, BuildWordContent
sage: list(ConcatenateContent((BuildWordContent('012', int), BuildWordContent([3, 4, 5])))) # indirect test
doctest:...: DeprecationWarning: ConcatenateContent is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
[0, 1, 2, 3, 4, 5]
Optimization: for ConcatenateContents return the internal list.
TESTS:
sage: from sage.combinat.words.word_content import ConcatenateContent, BuildWordContent
sage: type(ConcatenateContent((BuildWordContent([1, 2]),))._get_list())
doctest:...: DeprecationWarning: ConcatenateContent is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
<type 'tuple'>
Compares two contents according to the data they contain.
The types of the contents doesn’t matter.
TESTS:
sage: c1 = sage.combinat.words.word_content.WordContentFromList([1, 2, 1, 1])
doctest:...: DeprecationWarning: WordContentFromList is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
sage: c2 = sage.combinat.words.word_content.WordContentFromList([1, 2, 3])
sage: c3 = sage.combinat.words.word_content.WordContentFromList([1, 4])
sage: c2.__cmp__(c1) > 0
True
sage: c2.__cmp__(c2) == 0
True
sage: c2.__cmp__(c3) < 0
True
Does all the type and range checking for the key argument to __getitem__(). Also takes care of normalizing the ranges specified by slicing to values suitable for xrange() or similar in the finite case. In the infinite case a stop value of None, will stay as None since no end can be defined.
TESTS: Generic Cases
sage: w = sage.combinat.words.word_content.WordContent()
sage: w._check_getitem_args(1)
1
sage: w._check_getitem_args("abc")
...
TypeError: word indices must be integers
sage: w._check_getitem_args(slice(1, 2, 3))
slice(1, 2, 3)
sage: w._check_getitem_args(slice("a"))
...
TypeError: word indices must be integers
sage: w._check_getitem_args(slice("a", 1))
...
TypeError: word indices must be integers
sage: w._check_getitem_args(slice(1, 1, "a"))
...
TypeError: word indices must be integers
Finite Cases
sage: f = sage.combinat.words.word_content.WordContentFromList(range(10))
doctest:...: DeprecationWarning: WordContentFromList is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
sage: f._check_getitem_args(slice(None, None, None))
slice(0, 10, 1)
sage: f._check_getitem_args(slice(None, 0, None))
slice(0, 0, 1)
sage: f._check_getitem_args(slice(None, 1, None))
slice(0, 1, 1)
sage: f._check_getitem_args(slice(None, -1, None))
slice(0, 9, 1)
sage: f._check_getitem_args(slice(None, 11, None))
slice(0, 10, 1)
sage: f._check_getitem_args(slice(None, 10, None))
slice(0, 10, 1)
sage: f._check_getitem_args(slice(None, 9, None))
slice(0, 9, 1)
sage: f._check_getitem_args(slice(None, -9, None))
slice(0, 1, 1)
sage: f._check_getitem_args(slice(None, -10, None))
slice(0, 0, 1)
sage: f._check_getitem_args(slice(None, -11, None))
slice(0, 0, 1)
sage: f._check_getitem_args(slice(0, None, None))
slice(0, 10, 1)
sage: f._check_getitem_args(slice(1, None, None))
slice(1, 10, 1)
sage: f._check_getitem_args(slice(-1, None, None))
slice(9, 10, 1)
sage: f._check_getitem_args(slice(11, None, None))
slice(10, 10, 1)
sage: f._check_getitem_args(slice(10, None, None))
slice(10, 10, 1)
sage: f._check_getitem_args(slice(9, None, None))
slice(9, 10, 1)
sage: f._check_getitem_args(slice(-9, None, None))
slice(1, 10, 1)
sage: f._check_getitem_args(slice(-10, None, None))
slice(0, 10, 1)
sage: f._check_getitem_args(slice(-11, None, None))
slice(0, 10, 1)
sage: f._check_getitem_args(slice(None, None, -1))
slice(9, -1, -1)
sage: f._check_getitem_args(slice(None, 0, -1))
slice(9, 0, -1)
sage: f._check_getitem_args(slice(None, 1, -1))
slice(9, 1, -1)
sage: f._check_getitem_args(slice(None, -1, -1))
slice(9, 9, -1)
sage: f._check_getitem_args(slice(None, 11, -1))
slice(9, 9, -1)
sage: f._check_getitem_args(slice(None, 10, -1))
slice(9, 9, -1)
sage: f._check_getitem_args(slice(None, 9, -1))
slice(9, 9, -1)
sage: f._check_getitem_args(slice(None, -10, -1))
slice(9, 0, -1)
sage: f._check_getitem_args(slice(None, -11, -1))
slice(9, -1, -1)
sage: f._check_getitem_args(slice(None, -12, -1))
slice(9, -1, -1)
sage: f._check_getitem_args(slice(0, None, -1))
slice(0, -1, -1)
sage: f._check_getitem_args(slice(1, None, -1))
slice(1, -1, -1)
sage: f._check_getitem_args(slice(-1, None, -1))
slice(9, -1, -1)
sage: f._check_getitem_args(slice(10, None, -1))
slice(9, -1, -1)
sage: f._check_getitem_args(slice(9, None, -1))
slice(9, -1, -1)
sage: f._check_getitem_args(slice(8, None, -1))
slice(8, -1, -1)
sage: f._check_getitem_args(slice(-10, None, -1))
slice(0, -1, -1)
sage: f._check_getitem_args(slice(-11, None, -1))
slice(-1, -1, -1)
sage: f._check_getitem_args(slice(-12, None, -1))
slice(-1, -1, -1)
sage: f._check_getitem_args(slice(None, None, 0))
...
ValueError: slice step cannot be zero
sage: f._check_getitem_args(9)
9
sage: f._check_getitem_args(10)
...
IndexError: word index out of range
sage: f._check_getitem_args(0)
0
sage: f._check_getitem_args(-1)
9
sage: f._check_getitem_args(-10)
0
sage: f._check_getitem_args(-11)
...
IndexError: word index out of range
Infinite Cases
sage: from itertools import count
sage: i = sage.combinat.words.word_content.WordContentFromIterator(count())
doctest:...: DeprecationWarning: WordContentFromIterator is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
sage: i._check_getitem_args(slice(None, None, None))
slice(0, None, 1)
sage: i._check_getitem_args(slice(None, 0, None))
slice(0, 0, 1)
sage: i._check_getitem_args(slice(None, 1, None))
slice(0, 1, 1)
sage: i._check_getitem_args(slice(None, -1, None))
...
IndexError: negative index on infinite word
sage: i._check_getitem_args(slice(0, None, None))
slice(0, None, 1)
sage: i._check_getitem_args(slice(1, None, None))
slice(1, None, 1)
sage: i._check_getitem_args(slice(-1, None, None))
...
IndexError: negative index on infinite word
sage: i._check_getitem_args(slice(None, None, -1))
...
ValueError: negative step and no defined start
sage: i._check_getitem_args(slice(None, 0, -1))
...
ValueError: negative step and no defined start
sage: i._check_getitem_args(slice(None, -1, -1))
...
ValueError: negative step and no defined start
sage: i._check_getitem_args(slice(0, None, -1))
slice(0, -1, -1)
sage: i._check_getitem_args(slice(-1, None, -1))
...
IndexError: negative index on infinite word
sage: i._check_getitem_args(slice(None, None, 0))
...
ValueError: slice step cannot be zero
sage: i._check_getitem_args(1)
1
sage: i._check_getitem_args(-1)
...
IndexError: negative index on infinite word
Returns self as a list of contents suitable for concatenate
TESTS:
sage: type(sage.combinat.words.word_content.BuildWordContent([1, 2, 3])._get_list())
<type 'tuple'>
Method to concatenate two contents together.
TESTS:
sage: from sage.combinat.words.word_content import BuildWordContent
sage: list(BuildWordContent([1]).concatenate(BuildWordContent([2, 3, 4])))
doctest:...: DeprecationWarning: ConcatenateContent is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
[1, 2, 3, 4]
TESTS:
sage: from sage.combinat.words.utils import id_f
sage: from sage.combinat.words.word_content import WordContentFromFunction, WordContentFromList
sage: w = WordContentFromFunction(id_f);
doctest:...: DeprecationWarning: WordContentFromFunction is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
sage: e = WordContentFromList('', int)
doctest:...: DeprecationWarning: WordContentFromList is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
sage: w__2 = WordContentFromList('45', int)
sage: w_2 = WordContentFromList('01', int)
sage: w__1 = WordContentFromList('01234', int)
sage: w_s2 = WordContentFromList('024', int)
sage: w_s_2 = WordContentFromList('531', int)
sage: w[:] # random results
<sage.combinat.words.word_content.WordContentFromFunction object at 0xe499c30>
sage: w[0:] # random results
<sage.combinat.words.word_content.WordContentFromFunction object at 0xe499c50>
sage: w[1:] # random results
<sage.combinat.words.word_content.WordContentFromFunction object at 0xe499c30>
sage: w[:0] == e
True
sage: w[:5] == w__1
True
sage: w[::2] # random results
<sage.combinat.words.word_content.WordContentFromFunction object at 0xe499bf0>
sage: w[::0]
...
ValueError: slice step cannot be zero
sage: w[5]
5
sage: w[-1]
...
IndexError: negative index on infinite word
sage: w._len = 6
sage: w[:] == w
True
sage: w[0:] == w
True
sage: w[10:] == e
True
sage: w[-2:] == w__2
True
sage: w[-10:] == w
True
sage: w[:0] == e
True
sage: w[:2] == w_2
True
sage: w[:10] == w
True
sage: w[:-1] == w__1
True
sage: w[:-10] == e
True
sage: w[::2] == w_s2
True
sage: w[::-2] == w_s_2
True
sage: w[::0]
...
ValueError: slice step cannot be zero
sage: w[0]
0
sage: w[5]
5
sage: w[6]
...
IndexError: word index out of range
sage: w[-1]
5
sage: w[-6]
0
sage: w[-7]
...
IndexError: word index out of range
INPUT:
NOTE: Unnamed functions do not pickle. And if you arbitrarily slice this type of container then it may use lambdas and break pickling.
TESTS:
sage: c = sage.combinat.words.word_content.WordContentFromFunction(sage.combinat.words.utils.id_f)[:10]
doctest:...: DeprecationWarning: WordContentFromFunction is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
sage: c == loads(dumps(c))
True
TESTS:
sage: list(sage.combinat.words.word_content.WordContentFromFunction(sage.combinat.words.utils.id_f)[:6]) # indirect test
doctest:...: DeprecationWarning: WordContentFromFunction is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
[0, 1, 2, 3, 4, 5]
TESTS:
sage: c = sage.combinat.words.word_content.WordContentFromFunction(sage.combinat.words.utils.id_f)
doctest:...: DeprecationWarning: WordContentFromFunction is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
sage: len(c)
...
TypeError: an integer is required
sage: c = c[:10]
sage: len(c)
10
TESTS:
sage: from itertools import count
sage: from sage.combinat.words.word_content import WordContentFromIterator, WordContentFromList
sage: w = WordContentFromIterator(count())
doctest:...: DeprecationWarning: WordContentFromIterator is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
sage: e = WordContentFromList('', int)
doctest:...: DeprecationWarning: WordContentFromList is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
sage: w__2 = WordContentFromList('45', int)
sage: w_2 = WordContentFromList('01', int)
sage: w__1 = WordContentFromList('01234', int)
sage: w_s2 = WordContentFromList('024', int)
sage: w_s_2 = WordContentFromList('531', int)
sage: w[:] # random results
<sage.combinat.words.word_content.WordContentFromIterator object at 0xe499c30>
sage: w[0:] # random results
<sage.combinat.words.word_content.WordContentFromIterator object at 0xe499c50>
sage: w[1:] # random results
<sage.combinat.words.word_content.WordContentFromIterator object at 0xe499c30>
sage: w[:0] == e
True
sage: w[:5] == w__1
True
sage: w[::2] # random results
<sage.combinat.words.word_content.WordContentFromIterator object at 0xe499bf0>
sage: w[::0]
...
ValueError: slice step cannot be zero
sage: w[5]
5
sage: w[-1]
...
IndexError: negative index on infinite word
sage: w._len = 6
sage: w[:] == w
True
sage: w[0:] == w
True
sage: w[10:] == e
True
sage: w[-2:] == w__2
True
sage: w[-10:] == w
True
sage: w[:0] == e
True
sage: w[:2] == w_2
True
sage: w[:10] == w
True
sage: w[:-1] == w__1
True
sage: w[:-10] == e
True
sage: w[::2] == w_s2
True
sage: w[::-2] == w_s_2
doctest:...: DeprecationWarning: WordContentFromList is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
True
sage: w[::0]
...
ValueError: slice step cannot be zero
sage: w[0]
0
sage: w[5]
5
sage: w[6]
...
IndexError: word index out of range
sage: w[-1]
5
sage: w[-6]
0
sage: w[-7]
...
IndexError: word index out of range
INPUT:
NOTE: It appears that islice does not pickle correctly causing various errors when reloading. Also, most iterators do not support copying and should not support pickling by extension.
TESTS:
sage: from itertools import count
sage: c = sage.combinat.words.word_content.WordContentFromIterator(count())[:10]
doctest:...: DeprecationWarning: WordContentFromIterator is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
#sage: c == loads(dumps(c)) # broken because of islice
#True
TESTS:
sage: from itertools import count
sage: list(sage.combinat.words.word_content.WordContentFromIterator(count())[:6]) # indirect test
doctest:...: DeprecationWarning: WordContentFromIterator is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
[0, 1, 2, 3, 4, 5]
TESTS:
sage: from itertools import count
sage: c = sage.combinat.words.word_content.WordContentFromIterator(count())
doctest:...: DeprecationWarning: WordContentFromIterator is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
sage: len(c)
...
TypeError: an integer is required
sage: c = c[:10]
sage: len(c)
10
TESTS:
sage: from itertools import count
sage: c = sage.combinat.words.word_content.WordContentFromIterator(count())
doctest:...: DeprecationWarning: WordContentFromIterator is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
sage: it1 = c._get_it()
sage: it2 = c._get_it()
sage: it1.next()
0
sage: it2.next()
0
sage: list(c[:4])
[0, 1, 2, 3]
sage: it1.next()
1
sage: it2.next()
1
TESTS:
sage: from sage.combinat.words.word_content import WordContentFromList
sage: w = WordContentFromList('012345', int)
doctest:...: DeprecationWarning: WordContentFromList is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
sage: e = WordContentFromList('', int)
sage: w__2 = WordContentFromList('45', int)
sage: w_2 = WordContentFromList('01', int)
sage: w__1 = WordContentFromList('01234', int)
sage: w_s2 = WordContentFromList('024', int)
sage: w_s_2 = WordContentFromList('531', int)
sage: w[:] == w
True
sage: w[0:] == w
True
sage: w[10:] == e
True
sage: w[-2:] == w__2
True
sage: w[-10:] == w
True
sage: w[:0] == e
True
sage: w[:2] == w_2
True
sage: w[:10] == w
True
sage: w[:-1] == w__1
True
sage: w[:-10] == e
True
sage: w[::2] == w_s2
True
sage: w[::-2] == w_s_2
True
sage: w[::0]
...
ValueError: slice step cannot be zero
sage: w[0]
0
sage: w[5]
5
sage: w[6]
...
IndexError: word index out of range
sage: w[-1]
5
sage: w[-6]
0
sage: w[-7]
...
IndexError: word index out of range
INPUT:
TESTS:
sage: c = sage.combinat.words.word_content.WordContentFromList([0, 1, 1, 2, 1])
doctest:...: DeprecationWarning: WordContentFromList is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
sage: c == loads(dumps(c))
True
TESTS:
sage: list(sage.combinat.words.word_content.WordContentFromList('012345', int)) # indirect test
doctest:...: DeprecationWarning: WordContentFromList is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
[0, 1, 2, 3, 4, 5]
TESTS:
sage: len(sage.combinat.words.word_content.WordContentFromList([0, 1, 0, 0, 1]))
doctest:...: DeprecationWarning: WordContentFromList is deprecated and will be deleted in a future version of Sage; see sage.combinat.words.word_datatypes for alternatives
5
Returns True if obj is the content of a word.
EXAMPLES:
sage: from sage.combinat.words.word_content import BuildWordContent, is_WordContent
sage: is_WordContent(33)
False
sage: is_WordContent(BuildWordContent([0, 1, 0], lambda x : x))
True