Skip to main content
Coverage 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

>>> 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

Metadata

Release files for collections-extended 2.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for collections-extended 2.0.2
File Size Uploaded
collections-extended-2.0.2.tar.gz 22.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for collections-extended 2.0.2
File Interpreter ABI Platform
collections_extended-2.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 47.1 kB

Release files / collections-extended-2.0.2.tar.gz

Download URL collections-extended-2.0.2.tar.gz
Size 22.8 kB
Tags Source
SHA-256 checksum
How to use checksums
d68ebd1e2208444e392aa0016e5c085bd897f89296f5c1860787de5935994114
BLAKE2b-256 checksum
How to use checksums
6240e92bd575a99b7fb1fe9f3198ff4addc7d9f66e3d7e976b4d1b5bca6e049d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.10.1 Linux/5.4.151-16908-gff376e5d5ee1

Release files / collections_extended-2.0.2-py3-none-any.whl

Download URL collections_extended-2.0.2-py3-none-any.whl
Size 24.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bc096dda430b9455f51287943f7f63fbe53a4de78c444cc38c44e996af306ac5
BLAKE2b-256 checksum
How to use checksums
d5658f409bbb1faf21714ebebfa553a34023a6657bbb2f85f43141be4e4ffe5e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.10.1 Linux/5.4.151-16908-gff376e5d5ee1

Release history Release notifications | RSS feed

This release

2.0.2 This release

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.10.1

2 release files

0.9.0

2 release files

0.8.2

2 release files

0.8.0

2 release files

0.7.2

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

1 release file

0.3.1

2 release files

0.3.0

1 release file

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page