| 1 |
"""collections_extended contains a few extra basic data structures.""" |
| 2 |
from collections.abc import Collection |
| 3 |
|
| 4 |
from .bags import bag, frozenbag, UniqueElementsView, CountsView, Bag # noqa |
| 5 |
from .bijection import bijection |
| 6 |
from .indexed_dict import IndexedDict |
| 7 |
from .range_map import RangeMap, MappedRange # noqa |
| 8 |
from .setlists import frozensetlist, setlist, SetList |
| 9 |
|
| 10 |
__all__ = ( |
| 11 |
'Collection', |
| 12 |
'Bag', |
| 13 |
'bag', |
| 14 |
'frozenbag', |
| 15 |
'bijection', |
| 16 |
'IndexedDict', |
| 17 |
'RangeMap', |
| 18 |
'SetList', |
| 19 |
'frozensetlist', |
| 20 |
'setlist', |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
def collection(iterable=None, mutable=True, ordered=False, unique=False): |
| 25 |
"""Return a :class:`Collection` with the specified properties. |
| 26 |
|
| 27 |
Args: |
| 28 |
iterable (Iterable): collection to instantiate new collection from. |
| 29 |
mutable (bool): Whether or not the new collection is mutable. |
| 30 |
ordered (bool): Whether or not the new collection is ordered. |
| 31 |
unique (bool): Whether or not the new collection contains only unique values. |
| 32 |
""" |
| 33 |
if iterable is None: |
| 34 |
iterable = tuple() |
| 35 |
if unique: |
| 36 |
if ordered: |
| 37 |
if mutable: |
| 38 |
return setlist(iterable) |
| 39 |
else: |
| 40 |
return frozensetlist(iterable) |
| 41 |
else: |
| 42 |
if mutable: |
| 43 |
return set(iterable) |
| 44 |
else: |
| 45 |
return frozenset(iterable) |
| 46 |
else: |
| 47 |
if ordered: |
| 48 |
if mutable: |
| 49 |
return list(iterable) |
| 50 |
else: |
| 51 |
return tuple(iterable) |
| 52 |
else: |
| 53 |
if mutable: |
| 54 |
return bag(iterable) |
| 55 |
else: |
| 56 |
return frozenbag(iterable) |