GithubHelp home page GithubHelp logo

mlenzen / collections-extended Goto Github PK

View Code? Open in Web Editor NEW
45.0 3.0 9.0 1.08 MB

Extra Python Collections - bags (multisets), setlists (unique list / indexed set), RangeMap and IndexedDict

Home Page: http://collections-extended.lenzm.net/

License: Apache License 2.0

Python 98.68% Makefile 1.32%
python data-structures collections

collections-extended's Introduction

README

Coverage Downloads
Documentation:
https://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 extra collections. The new collections include bags AKA multisets, setlists AKA unique lists or ordered sets, a bijection, a RangeMap which is a mapping from ranges to values, and an IndexedDict class. There are also frozen (hashable) varieties of bags and setlists.

Compatible with and tested against CPython 3.6, 3.7, 3.8, 3.9, 3.10, PyPy3.6, PyPy3.7 & PyPy3.8.

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

New Collections

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:2022 Michael Lenzen
License:Apache License, Version 2.0
Project Homepage:https://github.com/mlenzen/collections-extended

collections-extended's People

Contributors

bluecube avatar mlenzen avatar tirkarthi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

collections-extended's Issues

Collection comparison

What types of Collections should be comparable to each other?
If a set and a bag contain the same elements with the same multiplicity
should they be equal? For a list and a setlist?

I think sets and bags should be comparable, a bag can be a subset of a set
and vice versa. The relationship is well defined and already in Python for
set comparisons.

setlist.extend should be atomic

Extending a setlist should either add all of the items or none, right now it appends elements until it hits one that already exists.

RangeMap comparison

rm = RangeMap({1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'})
del rm[1:2]
assert rm == RangeMap({2: 'b', 3: 'c', 4: 'd', 5: 'e'})

Comparing the _key_mapping dicts doesn't work because 1 is mapped to _empty

bidict

Hi @mlenzen, I am the author of bidict, a Python library that focuses on bidirectional maps. I just came across your library's bijection, and was wondering if you already knew about bidict when you wrote bijection, and either way, what you think of it. (Also, fun to see how similarly the .inverse property is implemented under the hood.) Just wanted to invite you to join forces if you're interested, as we're covering some overlapping ground. Best wishes!

requirements.txt cleanup

general requirements.txt unnecessary - setup.py handles it

consolidate requirements-dev.txt and requirements-doc.txt?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.