File: README.rst

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 (156 lines) | stat: -rw-r--r-- 3,646 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
README
######

.. image:: https://coveralls.io/repos/github/mlenzen/collections-extended/badge.svg?branch=master
	:target: https://coveralls.io/github/mlenzen/collections-extended?branch=master
	:alt: Coverage


.. image:: https://pepy.tech/badge/collections-extended/week
	:target: https://pepy.tech/project/collections-extended/
	:alt: Downloads


Documentation:
	http://collections-extended.lenzm.net/
GitHub:
	https://github.com/mlenzen/collections-extended
PyPI:
	https://pypi.python.org/pypi/collections-extended

Overview
========

``collections_extended`` is a pure Python module with no dependencies providing:

- a ``bag`` class, AKA **multiset**,
- a ``setlist`` class, which is a **unique list** or **ordered set**,
- a ``bijection`` class, ``RangeMap`` which is a mapping from ranges to values and
- a ``IndexedDict`` class, which is an ordered mapping whose elements can be accessed using index, in addition to key.

There are also frozen (hashable) varieties of bags and setlists.

Compatible with and tested against Python 3.6, 3.7, 3.8, 3.9, 3.10 & PyPy3.

Getting Started
===============

.. code-block:: python

	>>> from collections_extended import bag, setlist, bijection, RangeMap, IndexedDict
	>>> from datetime import date
	>>> b = bag('abracadabra')
	>>> b.count('a')
	5
	>>> b.remove('a')
	>>> b.count('a')
	4
	>>> 'a' in b
	True
	>>> b.count('d')
	1
	>>> b.remove('d')
	>>> b.count('d')
	0
	>>> 'd' in b
	False

	>>> sl = setlist('abracadabra')
	>>> sl
	setlist(('a', 'b', 'r', 'c', 'd'))
	>>> sl[3]
	'c'
	>>> sl[-1]
	'd'
	>>> 'r' in sl  # testing for inclusion is fast
	True
	>>> sl.index('d')  # so is finding the index of an element
	4
	>>> sl.insert(1, 'd')  # inserting an element already in raises a ValueError
	Traceback (most recent call last):
	...
		raise ValueError
	ValueError
	>>> sl.index('d')
	4

	>>> bij = bijection({'a': 1, 'b': 2, 'c': 3})
	>>> bij.inverse[2]
	'b'
	>>> bij['a'] = 2
	>>> bij == bijection({'a': 2, 'c': 3})
	True
	>>> bij.inverse[1] = 'a'
	>>> bij == bijection({'a': 1, 'c': 3})
	True

	>>> version = RangeMap()
	>>> version[date(2017, 10, 20): date(2017, 10, 27)] = '0.10.1'
	>>> version[date(2017, 10, 27): date(2018, 2, 14)] = '1.0.0'
	>>> version[date(2018, 2, 14):] = '1.0.1'
	>>> version[date(2017, 10, 24)]
	'0.10.1'
	>>> version[date(2018, 7, 1)]
	'1.0.1'
	>>> version[date(2018, 6, 30):] = '1.0.2'
	>>> version[date(2018, 7, 1)]
	'1.0.2'

	>>> idict = IndexedDict()
	>>> idict['a'] = "A"
	>>> idict['b'] = "B"
	>>> idict['c'] = "C"
	>>> idict.get(key='a')
	'A'
	>>> idict.get(index=2)
	'C'
	>>> idict.index('b')
	1

Installation
============

``pip install collections-extended``

Usage
=====
	``from collections_extended import bag, frozenbag, setlist, frozensetlist, bijection``

Classes
=======
There are seven new collections provided:

Bags
----
bag
	This is a bag AKA multiset.
frozenbag
	This is a frozen (hashable) version of a bag.

Setlists
--------
setlist
	An ordered set or a list of unique elements depending on how you look at it.
frozensetlist
	This is a frozen (hashable) version of a setlist.

Mappings
--------
bijection
	A one-to-one mapping.
RangeMap
	A mapping from ranges (of numbers/dates/etc)
IndexedDict
	A mapping that keeps insertion order and allows access by index.

Python 2
--------

The package no longer supports Python 2. The last version to support
Python 2.7, 3.4 & 3.5 was 1.0. No new feature releases will be done for 1.x but
any significant bugs that come up may be fixed.

:Author: Michael Lenzen
:Copyright: 2021 Michael Lenzen
:License: Apache License, Version 2.0
:Project Homepage: https://github.com/mlenzen/collections-extended