|
| 1 | +.. _filter_containers: |
| 2 | + |
| 3 | +================= |
| 4 | +Filter containers |
| 5 | +================= |
| 6 | + |
| 7 | +A filter container is just a regular python container that has some extra methods for filtering or ordering its |
| 8 | +elements. It has the same interface (i.e. attributes and methods) as the primitive python type it inherits from, with |
| 9 | +these extra methods: |
| 10 | + |
| 11 | +- ``filter`` |
| 12 | +- ``order_by`` |
| 13 | + |
| 14 | +There are two types of filter containers currently implemented: |
| 15 | + |
| 16 | +- ``FilterSet`` |
| 17 | +- ``FilterList`` |
| 18 | + |
| 19 | +``FilterSets`` are currently used in: |
| 20 | + |
| 21 | +- ``Dataset.files`` to store ``Datafiles`` |
| 22 | +- ``TagSet.tags`` to store ``Tags`` |
| 23 | + |
| 24 | +You can see filtering in action on the files of a ``Dataset`` `here <dataset.rst>`_. |
| 25 | + |
| 26 | + |
| 27 | +--------- |
| 28 | +Filtering |
| 29 | +--------- |
| 30 | + |
| 31 | +Filters are named as ``"<name_of_attribute_to_check>__<filter_action>"``, and any attribute of a member of the |
| 32 | +``FilterSet`` whose type or interface is supported can be filtered. |
| 33 | +.. code-block:: python |
| 34 | + filter_set = FilterSet( |
| 35 | + {Datafile(path="my_file.csv"), Datafile(path="your_file.txt"), Datafile(path="another_file.csv")} |
| 36 | + ) |
| 37 | +
|
| 38 | + filter_set.filter(filter_name="name__ends_with", filter_value=".csv") |
| 39 | + >>> <FilterSet({<Datafile('my_file.csv')>, <Datafile('another_file.csv')>})> |
| 40 | +
|
| 41 | +The following filters are implemented for the following types: |
| 42 | + |
| 43 | +- ``bool``: |
| 44 | + |
| 45 | + * ``is`` |
| 46 | + * ``is_not`` |
| 47 | + |
| 48 | +- ``str``: |
| 49 | + |
| 50 | + * ``is`` |
| 51 | + * ``is_not`` |
| 52 | + * ``equals`` |
| 53 | + * ``not_equals`` |
| 54 | + * ``iequals`` |
| 55 | + * ``not_iequals`` |
| 56 | + * ``lt`` (less than) |
| 57 | + * ``lte`` (less than or equal) |
| 58 | + * ``gt`` (greater than) |
| 59 | + * ``gte`` (greater than or equal) |
| 60 | + * ``contains`` |
| 61 | + * ``not_contains`` |
| 62 | + * ``icontains`` (case-insensitive contains) |
| 63 | + * ``not_icontains`` |
| 64 | + * ``starts_with`` |
| 65 | + * ``not_starts_with`` |
| 66 | + * ``ends_with`` |
| 67 | + * ``not_ends_with`` |
| 68 | + |
| 69 | +- ``NoneType``: |
| 70 | + |
| 71 | + * ``is`` |
| 72 | + * ``is_not`` |
| 73 | + |
| 74 | +- ``TagSet``: |
| 75 | + |
| 76 | + * ``is`` |
| 77 | + * ``is_not`` |
| 78 | + * ``equals`` |
| 79 | + * ``not_equals`` |
| 80 | + * ``any_tag_contains`` |
| 81 | + * ``not_any_tag_contains`` |
| 82 | + * ``any_tag_starts_with`` |
| 83 | + * ``not_any_tag_starts_with`` |
| 84 | + * ``any_tag_ends_with`` |
| 85 | + * ``not_any_tag_ends_with`` |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +Additionally, these filters are defined for the following *interfaces* (duck-types). : |
| 90 | + |
| 91 | +- Numbers: |
| 92 | + |
| 93 | + * ``is`` |
| 94 | + * ``is_not`` |
| 95 | + * ``equals`` |
| 96 | + * ``not_equals`` |
| 97 | + * ``lt`` |
| 98 | + * ``lte`` |
| 99 | + * ``gt`` |
| 100 | + * ``gte`` |
| 101 | + |
| 102 | +- Iterables: |
| 103 | + |
| 104 | + * ``is`` |
| 105 | + * ``is_not`` |
| 106 | + * ``equals`` |
| 107 | + * ``not_equals`` |
| 108 | + * ``contains`` |
| 109 | + * ``not_contains`` |
| 110 | + * ``icontains`` |
| 111 | + * ``not_icontains`` |
| 112 | + |
| 113 | +The interface filters are only used if the type of the attribute of the element being filtered is not found in the first |
| 114 | +list of filters. |
| 115 | + |
| 116 | +-------- |
| 117 | +Ordering |
| 118 | +-------- |
| 119 | +As sets are inherently orderless, ordering a ``FilterSet`` results in a new ``FilterList``, which has the same extra |
| 120 | +methods and behaviour as a ``FilterSet``, but is based on the ``list`` type instead - meaning it can be ordered and |
| 121 | +indexed etc. A ``FilterSet`` or ``FilterList`` can be ordered by any of the attributes of its members: |
| 122 | +.. code-block:: python |
| 123 | + filter_set.order_by("name") |
| 124 | + >>> <FilterList([<Datafile('another_file.csv')>, <Datafile('my_file.csv')>, <Datafile(path="your_file.txt")>])> |
| 125 | +
|
| 126 | +The ordering can also be carried out in reverse (i.e. descending order) by passing ``reverse=True`` as a second argument |
| 127 | +to the ``order_by`` method. |
0 commit comments