File: __init__.py

package info (click to toggle)
python-collections-extended 2.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 492 kB
  • sloc: python: 2,917; makefile: 59
file content (56 lines) | stat: -rw-r--r-- 1,375 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""collections_extended contains a few extra basic data structures."""
from collections.abc import Collection

from .bags import bag, frozenbag, UniqueElementsView, CountsView, Bag  # noqa
from .bijection import bijection
from .indexed_dict import IndexedDict
from .range_map import RangeMap, MappedRange  # noqa
from .setlists import frozensetlist, setlist, SetList

__all__ = (
	'Collection',
	'Bag',
	'bag',
	'frozenbag',
	'bijection',
	'IndexedDict',
	'RangeMap',
	'SetList',
	'frozensetlist',
	'setlist',
	)


def collection(iterable=None, mutable=True, ordered=False, unique=False):
	"""Return a :class:`Collection` with the specified properties.

	Args:
		iterable (Iterable): collection to instantiate new collection from.
		mutable (bool): Whether or not the new collection is mutable.
		ordered (bool): Whether or not the new collection is ordered.
		unique (bool): Whether or not the new collection contains only unique values.
	"""
	if iterable is None:
		iterable = tuple()
	if unique:
		if ordered:
			if mutable:
				return setlist(iterable)
			else:
				return frozensetlist(iterable)
		else:
			if mutable:
				return set(iterable)
			else:
				return frozenset(iterable)
	else:
		if ordered:
			if mutable:
				return list(iterable)
			else:
				return tuple(iterable)
		else:
			if mutable:
				return bag(iterable)
			else:
				return frozenbag(iterable)