clique.collection

class clique.collection.Collection(head, tail, padding, indexes=None)[source]

Bases: object

Represent group of items that differ only by numerical component.

indexes

Prevent standard setting of property.

Example:

>>> class Foo(object):
...
...     x = Unsettable('x')
...
...     def __init__(self):
...         self.__dict__['x'] = True
...
>>> foo = Foo()
>>> print(foo.x)
True
>>> foo.x = False
AttributeError: Cannot set attribute defined as unsettable.
__init__(head, tail, padding, indexes=None)[source]

Initialise collection.

head is the leading common part whilst tail is the trailing common part.

padding specifies the “width” of the numerical component. An index will be padded with zeros to fill this width. A padding of zero implies no padding and width may be any size so long as no leading zeros are present.

indexes can specify a set of numerical indexes to initially populate the collection with.

Note

After instantiation, the indexes attribute cannot be set to a new value using assignment:

>>> collection.indexes = [1, 2, 3]
AttributeError: Cannot set attribute defined as unsettable.

Instead, manipulate it directly:

>>> collection.indexes.clear()
>>> collection.indexes.update([1, 2, 3])
property head

Return common leading part.

property tail

Return common trailing part.

match(item)[source]

Return whether item matches this collection expression.

If a match is successful return data about the match otherwise return None.

add(item)[source]

Add item to collection.

raise CollectionError if item cannot be added to the collection.

remove(item)[source]

Remove item from collection.

raise CollectionError if item cannot be removed from the collection.

format(pattern='{head}{padding}{tail} [{ranges}]')[source]

Return string representation as specified by pattern.

Pattern can be any format accepted by Python’s standard format function and will receive the following keyword arguments as context:

  • head - Common leading part of the collection.

  • tail - Common trailing part of the collection.

  • padding - Padding value in %0d format.

  • range - Total range in the form start-end

  • ranges - Comma separated ranges of indexes.

  • holes - Comma separated ranges of missing indexes.

is_contiguous()[source]

Return whether entire collection is contiguous.

holes()[source]

Return holes in collection.

Return Collection of missing indexes.

is_compatible(collection)[source]

Return whether collection is compatible with this collection.

To be compatible collection must have the same head, tail and padding properties as this collection.

merge(collection)[source]

Merge collection into this collection.

If the collection is compatible with this collection then update indexes with all indexes in collection.

raise CollectionError if collection is not compatible with this collection.

separate()[source]

Return contiguous parts of collection as separate collections.

Return as list of Collection instances.