Skip to main content

efficient filtering of lists of objects

Project description

listful

pypi Python: 3.6+ Downloads Build Status Code coverage License: MIT Code style: black

Description

Efficient filtering of lists of objects

Installation

pip install listful

Usage

Initialize with the fields you want to filter by:

>>> from listful import Listful
>>> data = Listful(
...    [{'x': 1, 'y': 10}, {'x': 2, 'y': 20}, {'x': 2, 'y': 30}], 
...    fields=['x', 'y']
... )

Filtering:

  • By one field:
>>> data.filter(x=1).one_or_none()
{'x': 1, 'y': 10}
>>> data.filter(y=20).one_or_none()
{'x': 2, 'y': 20}
  • By one field, with more than one result:
>>> data.filter(x=2).to_list()
[{'x': 2, 'y': 20}, {'x': 2, 'y': 30}]
  • By two fields:
>>> data.filter(x=2, y=30).one_or_none()
{'x': 2, 'y': 30}
  • Raise exception if more than one found
>>> data.filter(x=2).one_or_raise()
Traceback (most recent call last):
<...>
listful.exceptions.MoreThanOneResultException: Found more than one result for filter {'x': 2}: [{'x': 2, 'y': 20}, {'x': 2, 'y': 30}]
  • Get all values for a specific field
>>> data.get_all_for_field('x')
[1, 2, 2]

Updating indexes:

Listful has the same api as list, so you can get/set/delete items the same way and the indices will be updated automatically

>>> data[0] = {'x': 17, 'y': 17}
>>> data.filter(x=17).one_or_none()
{'x': 17, 'y': 17}
>>> data[0]
{'x': 17, 'y': 17}
>>> del data[0]
>>> data.filter(x=17).one_or_none()

If you want to modify an element and update the indices you can do so explicitly:

>>> data[0]['x'] = 1
>>> data.rebuild_indexes_for_item(data[0])
>>> data.filter(x=1).one_or_none()
{'x': 1, 'y': 20}

Objects:

Listful supports also lists of objects:

>>> class Item:
...     def __init__(self, x, y):
...         self.x = x
...         self.y = y
...
...     def __repr__(self):
...         return f"Item(x={self.x}, y={self.y})"

>>> items = Listful(
...    [Item(x=1, y=10), Item(x=2, y=20), Item(x=2, y=30)], 
...    fields=['x', 'y']
... )
>>> items.filter(x=1).one_or_none()
Item(x=1, y=10)

For developers

Create venv and install deps

make init

Install git precommit hook

make precommit_install

Run linters, autoformat, tests etc.

make pretty lint test

Bump new version

make bump_major
make bump_minor
make bump_patch

License

MIT

Change Log

Unreleased

  • ...

0.1.1 - 2020-02-12

  • ...

0.1.0 - 2020-02-12

  • initial

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

listful-0.1.2.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

listful-0.1.2-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file listful-0.1.2.tar.gz.

File metadata

  • Download URL: listful-0.1.2.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.8.1

File hashes

Hashes for listful-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f59bf6609256760775c18c04eeef00edf9d17f5a00570a8cefb84bb1c523345e
MD5 d160490351334e3e1a2bb17e48db2de5
BLAKE2b-256 387af64f11cf6ec96e4db7c6af5c70542ad5003ae34811468837ed15178e68c4

See more details on using hashes here.

File details

Details for the file listful-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: listful-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.8.1

File hashes

Hashes for listful-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4c9424d594c97035a0e3bf412bf4da7f2539c808ddfce812fd1d72469d2a3ece
MD5 c2eee10fde0e6822af43b224ade397cd
BLAKE2b-256 8e7a6f2171885e6b373f061bae92ce1f6c5cc16b89eadc1fc9c53135f97fd3ca

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