Skip to main content

listful

pypi Python: 3.7+ 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']
    ... )

(If you don't specify the fields, all the fields whose corresponding values are hashable will be chosen)

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)

Here too, if you don't specify the fields, all fields with hashable values will be chosen:

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

Performance

See scripts/timing.py.

A comparison of filtering with listful vs filtering with pandas (with/without index)

listful pandas pandas_with_index
init 7.63e-02 3.03e-01 5.24e-02
filter:1 2.07e-05 1.46e-03 1.79e-03
filter:n 2.02e-01 7.40e+01 1.54e+01

70x faster than pandas with indexing, 360x faster than pandas without indexing.

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.3.0 - 2021-01-17

  • ...

0.2.1 - 2020-04-08

  • ...

0.2.0 - 2020-04-08

  • Add support for default fields

0.1.3 - 2020-02-14

  • ...

0.1.1 - 2020-02-12

  • ...

0.1.0 - 2020-02-12

  • initial

Metadata

Release files for listful 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for listful 0.3.0
File Size Uploaded Provenance Actions
listful-0.3.0.tar.gz 7.6 kB Without provenance Details for listful-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for listful 0.3.0
File Interpreter ABI Platform Actions
listful-0.3.0-py3-none-any.whl Python 3 none any Details for listful-0.3.0-py3-none-any.whl

Total release size: 14.9 kB

Release files / listful-0.3.0.tar.gz

Download URL listful-0.3.0.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
7551f9c047e413bbfaf4938bfc1e4e79095a8460220570c5ffd8a85aed104974
BLAKE2b-256 checksum
How to use checksums
02158c50f176ba61a5597beeca7649f8f0ade01412bff07533793153d1108c74
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

Release files / listful-0.3.0-py3-none-any.whl

Download URL listful-0.3.0-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7023c5c1e5fb12938f841fe8fb7736e94219edf1a2fee1eec2f31fd5eeb69b13
BLAKE2b-256 checksum
How to use checksums
2306afabcde3613687333736818a1c2f8630910676b8f0292fb1a739cf85f711
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1

Security for listful 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page