Skip to main content

Extra Python Collections - bags (multisets) and setlists (ordered sets)

Project description

Build Status Coverage

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 Python module 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.

Tested against Python 2.7, 3.4, 3.5, 3.6, 3.7, PyPy & 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 classes 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.

Author:

Michael Lenzen

Copyright:

2019 Michael Lenzen

License:

Apache License, Version 2.0

Project Homepage:

https://github.com/mlenzen/collections-extended

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

collections-extended-1.0.3.tar.gz (18.4 kB view details)

Uploaded Source

Built Distribution

collections_extended-1.0.3-py2.py3-none-any.whl (19.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file collections-extended-1.0.3.tar.gz.

File metadata

File hashes

Hashes for collections-extended-1.0.3.tar.gz
Algorithm Hash digest
SHA256 7cda4f6f93f6a42a0b815677628eb8792bca5cffd4f0d78045a66935444f6651
MD5 44c910acc32833b7ee587db8d2739d69
BLAKE2b-256 2a89d17202c87a7d50305612d23b382e2d70dcaf64327d0985141f343ea8906e

See more details on using hashes here.

File details

Details for the file collections_extended-1.0.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for collections_extended-1.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 47e2559603085c7444b68cac6735c985a011341c34cde578e8550e109b328832
MD5 1285678bb12947710ca64103d987658f
BLAKE2b-256 4e1e3440dfc8036621832e33634bd8ae6fe691c0ee951441903417c8879c242e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page