Skip to main content

A list with built in filtering syntax.

Reason this release was yanked:

An additional build, with a different file_name, had accidentally been included, creating some confusion about what file to import (although both contained the same code.)

Project description

filterablelist

A subclass of Python's built in list, with two additional methods inspired by the filter and exclude methods from the Django queryset. These methods allows for complex list filtering with a very simple syntax.

Use examples

Create an example list:

from filterablelist import FilterableList

class MyClass:
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z

    def __repr__(self) -> str:
        return f"(x:{self.x}, y:{self.y}, z:{self.z})"


fl = FilterableList([
    MyClass(1, 1, 1),
    MyClass(1, 1, 2),
    MyClass(2, 1, 3),
    {"x": 1, "y": 2, "z": 1}
])

Make new lists with simple queries:

print(fl.filter(x=1))
print(fl.filter(x=1, y=1))
print(fl.filter(x=1, y=1, require_all=False))
print(fl.filter(y__in=[2,5,7]))

print(fl.exclude(x=1))
print(fl.exclude(x=1, y=1))
print(fl.exclude(x=1, y=1, require_all=False))
print(fl.exclude(y__in=[2,5,7]))

# Output
>>> [(x:1, y:1, z:1), (x:1, y:1, z:2), {'x': 1, 'y': 2, 'z': 1}]
>>> [(x:1, y:1, z:1), (x:1, y:1, z:2)]
>>> [(x:1, y:1, z:1), (x:1, y:1, z:2), (x:2, y:1, z:3), {'x': 1, 'y': 2, 'z': 1}]
>>> [{'x': 1, 'y': 2, 'z': 1}]

>>> [(x:2, y:1, z:3)]
>>> [(x:2, y:1, z:3), {'x': 1, 'y': 2, 'z': 1}]
>>> []
>>> [(x:1, y:1, z:1), (x:1, y:1, z:2), (x:2, y:1, z:3)]

Use comparison operator postfixes to make more complex queries:

print(fl.filter(z__lte=2))
print(fl.exclude(x__lt=2))
print(fl.filter(x__ne=1))

# Output
>>> [(x:1, y:1, z:1), (x:1, y:1, z:2), {'x': 1, 'y': 2, 'z': 1}]
>>> [(x:2, y:1, z:3)]
>>> [(x:2, y:1, z:3)]

Methods

Note: All methods and properties of the built-in list are also available.

filter(check_subscriptable=True, require_all=True, **kwargs)

Arguments

check_subscriptable: Boolean, True by default. If True, the filter will match against subscriptable indexes where available, as well as object properties. Set to False to only match against object properties.

require_all: Boolean, True by Default. When True, only objects that match all arguments will be included in the new list (similar to using AND in an SQL query.) When set to False, all objects that match any of the arguments will be included in the new list (similar to using OR in an SQL query.)

**kwargs: Any number of key-value pairs that you wish to filter the list by.

Description

The filter method takes any number of key-value pairs as arguments and returns a new FilterableList containing only the matching objects. By default, all arguments have to match for an object to be included, and both object properties and subscriptable indexes will be used to try to match objects. This default behaviour can be changed by setting require_all and/or check_subscriptable to False per your requirements.

exclude(check_subscriptable=True, require_all=True, **kwargs)

Arguments

check_subscriptable: Boolean, True by default. If True, exclude will match against subscriptable indexes where available, as well as object properties. Set to False to only match against object properties.

require_all: Boolean, True by Default. When True, only objects that match all arguments will be excluded in the new list (similar to using AND in an SQL query.) When set to False, all objects that match any of the arguments will be excluded in the new list (similar to using OR in an SQL query.)

**kwargs: Any number of key-value pairs that you want to use to exclude objects from the new list.

Description

Basically the same as filter except that items that match the query will be excluded from the new list, rather than included.

Comparison Operators

By default, both filter and exclude will match object properties to arguments using the equality operator ´==´, but by adding a comparison postfix to the keys in your query, you can create more complex filters. The comparison operators available are:

  • __gt : (Greater than) Uses Python's > operator for comparison.
  • __gte : (Greater than or equal to) Uses Python's >= operator for comparison.
  • __lt : (Less than) Uses Python's < operator for comparison.
  • __lte : (Less than or equal to) Uses Python's <= operator for comparison.
  • __ne : (Not equal to) Uses Python's != operator for comparison.
  • __in : (Not equal to) Uses Python's in operator for comparison.

Examples

# name is equal to "Isaac" (default comparison without postfix)
.filter(name="Isaac")

# count is greater than 5
.filter(count__gt=5)

# age is greater than or equal to 111
.filter(age__gte=111)

# clicks is less than 19
.filter(clicks__lt=19)

# score is less than or equal to 1200
.filter(score__lte=1200)

# state is not equal to None
.filter(state__ne=None)

# id is in [12, 13, 14]
.filter(id__in=[12, 13, 14])

Query Chaining

Both filter and exclude returns a new FilterableList, making it possible to chain queries together if needed.

from filterablelist import FilterableList

fl = FilterableList([
    {"a": 1, "b": False, "c": None},
    {"a": 2, "b": False, "c": None},
    {"a": 1, "b": False, "c": 5},
    {"a": 1, "b": False, "c": True},
    {"a": 1, "d": []}
])

new_fl = fl.filter(a=1).exclude(c__bte=4).filter(c__ne=True)

print(new_fl)

# Output
# [{'a': 1, 'b': False, 'c': None}, {'a': 1, 'b': False, 'c': 5}]

Performance

Most normal list operations have no performance drop at all compared to a regular list. Only the operations that return a new list, such as slice and copy will have any drop in performance, but even in those cases it's very negligible (on the scale of 20%-ish.)

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

filterablelist-0.1.0.1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

filterablelist-0.1.0.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file filterablelist-0.1.0.1.tar.gz.

File metadata

  • Download URL: filterablelist-0.1.0.1.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for filterablelist-0.1.0.1.tar.gz
Algorithm Hash digest
SHA256 eecdedddca4557dc09147cde6ea72eecd1c479b82447331f22e903e7408987f2
MD5 a2790e625ad19a5acbb3d8c228e2fb7a
BLAKE2b-256 69d4351e52d016cba063169d60d549dff4e54c73fe8cede397e6acc2d9a09f55

See more details on using hashes here.

File details

Details for the file filterablelist-0.1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for filterablelist-0.1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b3a870437a2dfdd174b1e87932f81ffd226a23e1b4cb2e78c138b7e42017c6c6
MD5 dc28bf92b2cf68ce1b2a51d734eb88db
BLAKE2b-256 c9ba7b7fd7f7cca2ef3d7d91142755efd28a28396a2536734f31d7a717151644

See more details on using hashes here.

Supported by

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