Skip to main content

Shelves and buckets for easier work with grouped data

Project description

===================
Shelves and Buckets
===================


.. image:: https://img.shields.io/pypi/v/shelves_and_buckets.svg
:target: https://pypi.python.org/pypi/shelves_and_buckets

.. image:: https://img.shields.io/travis/lifesum/shelves_and_buckets.svg
:target: https://travis-ci.org/lifesum/shelves_and_buckets

.. image:: https://readthedocs.org/projects/shelves-and-buckets/badge/?version=latest
:target: https://shelves-and-buckets.readthedocs.io/en/latest/?badge=latest
:alt: Documentation Status

.. image:: https://pyup.io/repos/github/lifesum/shelves_and_buckets/shield.svg
:target: https://pyup.io/repos/github/lifesum/shelves_and_buckets/
:alt: Updates


Shelves and buckets is a small package for easier work with grouped data or data ranges. It originates from the need to easily find data in multidimensional space using intervals. Lets look at the following examples:

* Free software: MIT license
* Documentation: https://shelves-and-buckets.readthedocs.io.


Features
--------

Interval shelves are using the intervals_ package

.. _intervals: https://pypi.python.org/pypi/intervals

- IntIntervalShelf - interval boundaries are integers
- FloatIntervalShelf - interval boundaries are floats
- NamedShelf - takes a dictionary as input and behaves similarly but could be combined with other shelves


Examples
--------

Consider the following data for physical state based on number of pushups a person can make

+------------+------+-----------+-----------+
+ Pushup + <20 | 21 - 40 | 41+ +
+============+======+===========+===========+
+ Grade + Poor | Average | Excellent +
+------------+------+-----------+-----------+


How to find in which group the user belongs based on their score::

>>> from shelves_and_buckets import IntIntervalShelf
>>> shelf = IntIntervalShelf([
('(, 20]', 'poor'),
('[21, 40]', 'average'),
('[41, )', 'excellent'),
])
>>> shelf.get(5)
'poor'
>>> shelf.get(25)
'average

Ok, let's take a look at a bit more complicated example, what if we have another dimension e.g. the age of the person.

+-------------+-----------+---------------+---------+
+ Age / Grade + excellent + above average + average +
+=============+===========+===============+=========+
+ 20-29 + > 54 + 45-54 + 35-44 +
+-------------+-----------+---------------+---------+
+ 30-39 + > 44 + 35-44 + 24-34 +
+-------------+-----------+---------------+---------+
+ 40-49 + > 39 + 30-39 + 20-29 +
+-------------+-----------+---------------+---------+

Example::

male_20_29_shelf = IntIntervalShelf([
('[35, 44]', 'average'),
('[45, 54]', 'above average'),
('(54, )', 'excellent'),
])

male_30_39_shelf = IntIntervalShelf([
('[24, 34]', 'average'),
('[35, 44]', 'above average'),
('(44, )', 'excellent'),
])

age_shelf = IntIntervalShelf([
('[20, 29]', male_20_29_shelf),
('[30, 39]', male_30_39_shelf),
])

>>> age_shelf.get_multi(24, 42) # value are passed from the outer shelves in, in this case -> age, pushups
'average'


Shelves also support usage with indexing for adding and getting buckets::

shelf = IntIntervalShelf()
shelf[[0, 10]] = 'Bucket A'
shelf[[11, 20]] = 'Bucket B'
>>> shelf[5]
'Bucket A'


TODOs
-----

- Add check for intervals overlap
- Add option to avoid holes between intervals
- Add option to pass directly Interval object when creating bucket
- Consider using bisect for faster search in intervals http://www.ilian.io/working-with-intervals-in-python/

Credits
---------

This package was created with Cookiecutter_ and the `audreyr/cookiecutter-pypackage`_ project template.

.. _Cookiecutter: https://github.com/audreyr/cookiecutter
.. _`audreyr/cookiecutter-pypackage`: https://github.com/audreyr/cookiecutter-pypackage

This package uses Intervals_ by `Konsta Vesterinen`_

.. _`Konsta Vesterinen`: https://github.com/kvesteri


=======
History
=======

0.1.0.beta (2017-10-26)
------------------

* First release on PyPI.

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

shelves_and_buckets-0.1.4.tar.gz (22.1 kB view details)

Uploaded Source

Built Distribution

shelves_and_buckets-0.1.4-py2.py3-none-any.whl (6.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file shelves_and_buckets-0.1.4.tar.gz.

File metadata

File hashes

Hashes for shelves_and_buckets-0.1.4.tar.gz
Algorithm Hash digest
SHA256 81e9b60245cd6be4f35fc63f8d4ed201842042dc28400877d166bf6cf97e6fee
MD5 48b3b4a4417c81b50bfe869fb3a310bc
BLAKE2b-256 f94a3955d5b7fda018367856db44393724f54a8ab8aaa07d94d4a455928553d1

See more details on using hashes here.

File details

Details for the file shelves_and_buckets-0.1.4-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for shelves_and_buckets-0.1.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 092248eec02fc6eb51e1976ec18edc91f9f5d8e1cae31649fe1a8a41efa7389c
MD5 c842620a6e12e457b288d63c497c2654
BLAKE2b-256 bf293b0659b98d92566a9a060519aa4cf33f0cbc217dd06cd62e7b1f174ffee2

See more details on using hashes here.

Supported by

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