dtlibs.xcollections

dtlibs.xcollections.nameddict(name, *keys, **types)

Factory for DefinedDict.

A new DefinedDict subclass is created with keys. The key types may optionally be specified using types. An example is:

>>> Dict = nameddict('Dict', 'key1 key2 key3', key1=str, key2=list)
>>> d = Dict()
>>> d.key1 = 'a string'
>>> d.key2 = ['a', 'list']
>>> d.key3 = 'anything'
dtlibs.xcollections.typedlist(name, type_)

Factory function for specific TypedList object.

While TypeLists can be created directly, this function constructs a new TypeList subclass for a specific type, allowing it to be reused.

>>> StringList = typedlist('StringList', str)
>>> names = StringList()
>>> addresses = StringList()
>>> names.append('Mike')
>>> names.append(34)
Traceback (most recent call last):
    ...
TypeError: 34 is not type <class 'str'>
Traceback (most recent call last):
    ...
TypeError: 34 is not type <class 'str'>
class dtlibs.xcollections.TypedList(type_, initial=None)

A list that contains a specific type of object.

appendnew(*args, **kwargs)

Create a new instance the list type and append it.

Since the type of data stored by the list is known, a new object can be added to it by simply specifying the constructor arguments. The return value is the newly created object.

For example:

>>> import datetime
>>> l = TypedList(datetime.date)
>>> obj = l.appendnew(2001, 1, 1)
>>> print(obj)
2001-01-01

is the same as

>>> import datetime
>>> l = TypedList(datetime.date)
>>> obj = datetime.date(2001, 1, 1)
>>> l.append(obj)
insert(index, value)
type()
class dtlibs.xcollections.SelectList(iterable=None)

A list of which some items are selected.

The example below illustrates basic usage of this class.

>>> l = SelectList([1, 2, 3, 4, 5])
>>> l.select(3)
[1, 2, <3>, 4, 5]
>>> l.selection
(3,)
>>> l.select(6)
Traceback (most recent call last):
    ...
ValueError: 6
>>> l
[1, 2, <3>, 4, 5]
>>> l.clear()
>>> l.selection
()
>>> l.indexselect(0, 2)
[<1>, 2, <3>, 4, 5]
>>> l.indexselection
(0, 2)
>>> l.select(5, 3)
[<1>, 2, <3>, 4, <5>]
>>> l.selection
(1, 3, 5)
Traceback (most recent call last):
    ...
ValueError: 6

The following rules govern the processing of the selections:

  1. If options contains duplicates, the first matching value is selected when using select

    >>> l = SelectList([1, 2, 1])
    >>> l.select(1)
    [<1>, 2, 1]
    

    However, it is possible to explicitly select a value using indexselect().

    >>> l = SelectList([1, 2, 1])
    >>> l.indexselect(2)
    [1, 2, <1>]
    
  2. The selection lists are unordered.

    >>> l = SelectList([1, 2, 3, 4, 5])
    >>> _ = l.select(3)
    >>> _ = l.select(1)
    >>> l.selection
    (1, 3)
    
  3. If a selected value is changed in the SelectList, it is removed from the selection.

    >>> l = SelectList([1, 2, 3])
    >>> l.select(2)
    [1, <2>, 3]
    >>> l[1] = 4
    >>> l
    [1, 4, 3]
    
clear()

Clear the selection.

indexselect(*indexes)

Select by indexes and return the SelectList.

indexselection

Return a tuple of selected indexes.

indexunselect(*indexes)

Remove values by index and return the SelectList.

>>> l = SelectList('penguin')
>>> l.select('p', 'g')
[<'p'>, 'e', 'n', <'g'>, 'u', 'i', 'n']
>>> l.indexunselect(3)
[<'p'>, 'e', 'n', 'g', 'u', 'i', 'n']
>>> l.indexunselect(4)
Traceback (most recent call last):
    ...
IndexError: Item at position '4' is not selected
Traceback (most recent call last):
    ...
IndexError: Item at position '4' is not selected
insert(index, value)
select(*values)

Select a group of values and return the SelectList.

>>> l = SelectList('abcdefg')
>>> l.select('a', 'd', 'f')
[<'a'>, 'b', 'c', <'d'>, 'e', <'f'>, 'g']
selection

Return a tuple of the selected values.

unselect(*values)

Remove values from the selection list and return the SelectList.

If the selection contains duplicate values, then the first found is removed. If no matching values are found, then an exception is raised.

>>> l = SelectList('penguin')
>>> l.indexselect(2, 6)
['p', 'e', <'n'>, 'g', 'u', 'i', <'n'>]
>>> l.unselect('n')
['p', 'e', 'n', 'g', 'u', 'i', <'n'>]
>>> l.unselect('p')
Traceback (most recent call last):
    ...
ValueError: 'p' is not selected
Traceback (most recent call last):
    ...
ValueError: 'p' is not selected
class dtlibs.xcollections.FilterList(key, parent)

A filtered wrapper around a list.

This class wraps around a list, but filters all output. This is similar to using the built-in filter function, but allows changes through it to the underlying list. This is not as fast as filter, though, so should only be used when modifications are needed.

Initialisation follows the same format as filter:

>>> key = lambda n: n in [1, 3, 5, 7, 9]
>>> parent = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> f = FilterList(key, parent)

Iterating over the list returns only those values for which key returns True.

>>> [i for i in f]
[1, 3, 5, 7, 9]

Similarly, item indexes work according to this.

>>> f[3]
7

Assignments, insertions and deletions are supported, and affect the parent.

>>> f[3] = 1
>>> f.insert(4, 3)
>>> del f[1]
>>> parent
[1, 2, 4, 5, 6, 1, 8, 3, 9, 10]

Note that slices can only be used when querying. They cannot be used for assignments of deletions.

insert(index, value)
class dtlibs.xcollections.StrictList(accepts, initial=None)

A list that is fussy about what it contains.

A StrictList may only contain object for which accepts(obj) is True.

>>> largenumbers = StrictList(lambda n: n > 1000, range(2000, 2005))
>>> print(list(largenumbers))
[2000, 2001, 2002, 2003, 2004]
>>> largenumbers.append(3)
Traceback (most recent call last):
  ...
ValueError: 3
Traceback (most recent call last):
  ...
ValueError: 3
accepts(value)

Return False if value is not accepted.

insert(index, value)
class dtlibs.xcollections.MultiDict(*args, **kwargs)

A dictionary which allows multiple keys.

get(key, default=<class 'dtlibs.xcollections._multidict._Unused'>)
items(key=<class 'dtlibs.xcollections._multidict._Unused'>)
keys()
popitem(pair=None)
values(key=<class 'dtlibs.xcollections._multidict._Unused'>)