Skip to main content

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

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, provides a bag class, AKA multiset, a setlist class, which is a unique list or ordered set, a bijection class and RangeMap which is a mapping from ranges to values. There are also frozen (hashable) varieties of bags and setlists.

Tested against Python 2.6, 2.7, 3.1, 3.2, 3.3, 3.4, 3.5, PyPy & PyPy3.

Python 3.1 and 3.2 are unsupported by coverage so they are not included in Travis CI.

Getting Started

>>> from collections_extended import bag, setlist, bijection, RangeMap
>>> 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

>>> us_presidents = RangeMap()
>>> us_presidents[date(1993, 1, 20):date(2001, 1, 20)] = 'Bill Clinton'
>>> us_presidents[date(2001, 1, 20):date(2009, 1, 20)] = 'George W. Bush'
>>> us_presidents[date(2009, 1, 20):] = 'Barack Obama'
>>> us_presidents[date(1995, 5, 10)]
'Bill Clinton'
>>> us_presidents[date(2001, 1, 20)]
'George W. Bush'
>>> us_presidents[date(2021, 3, 1)]
'Barack Obama'
>>> us_presidents[date(2017, 1, 20):] = 'Someone New'
>>> us_presidents[date(2021, 3, 1)]
'Someone New'

Installation

pip install collections-extended

Usage

from collections_extended import bag, frozenbag, setlist, frozensetlist, bijection

Classes

There are six 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)

Author:

Michael Lenzen

Copyright:

2016 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-0.7.0.tar.gz (33.2 kB view details)

Uploaded Source

Built Distribution

collections_extended-0.7.0-py2.py3-none-any.whl (14.6 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

File hashes

Hashes for collections-extended-0.7.0.tar.gz
Algorithm Hash digest
SHA256 8f6439dca103c2b67dd212c9325b3ea37ea964acb7d6471d05434b2ab07ededc
MD5 5d4e6cf8408bc7296722a7476eea26c6
BLAKE2b-256 c2103b271bac3045af787d7fa828f5cc7bc64aa977d44cd95c70e20e9e06ef2e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for collections_extended-0.7.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c7e43a644d11457e57ebcb85227221e81b2b6c511640c60e766c2d1d946760de
MD5 fb187ffc671eea85c93742ea64d9f2e6
BLAKE2b-256 af38067a2330d59b68144ef44526cbb6c5c5721eb8aa09d0cd8031ec4f34adc1

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