44Filter containers
55=================
66
7- A filter container is just a regular python container that has some extra methods for filtering or ordering its
7+ A filter container is just a regular python container that has some extra methods for filtering and ordering its
88elements. It has the same interface (i.e. attributes and methods) as the primitive python type it inherits from, with
99these extra methods:
1010
1111- ``filter ``
1212- ``order_by ``
1313
14- There are two types of filter containers currently implemented:
14+ There are three types of filter containers currently implemented:
1515
1616- ``FilterSet ``
1717- ``FilterList ``
18+ - ``FilterDict ``
1819
19- ``FilterSets `` are currently used in:
20+ ``FilterSets `` are currently used in ``Dataset.files `` to store ``Datafiles `` and make them filterable, which is useful
21+ for dealing with a large number of datasets, while ``FilterList `` is returned when ordering any filter container.
2022
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 `` :doc: `here <dataset >`.
23+ You can see an example of filtering of a ``Dataset ``'s files :doc: `here <dataset >`.
2524
2625
2726---------
2827Filtering
2928---------
3029
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.
30+ Key points:
31+
32+ * Any attribute of a member of a filter container whose type or interface is supported can be used when filtering
33+ * Filters are named as ``"<name_of_attribute_to_check>__<filter_action>" ``
34+ * Multiple filters can be specified at once for chained filtering
35+ * ``<name_of_attribute_to_check> `` can be a single attribute name or a double-underscore-separated string of nested attribute names
36+ * Nested attribute names work for real attributes as well as dictionary keys (in any combination and to any depth)
3337
3438.. code-block :: python
3539
3640 filter_set = FilterSet(
37- {Datafile(timestamp = time.time(), path = " my_file.csv" ), Datafile(timestamp = time.time(), path = " your_file.txt" ), Datafile(timestamp = time.time(), path = " another_file.csv" )}
41+ {
42+ Datafile(path = " my_file.csv" , cluster = 0 , tags = {" manufacturer" : " Vestas" }),
43+ Datafile(path = " your_file.txt" , cluster = 1 , tags = {" manufacturer" : " Vergnet" }),
44+ Datafile(path = " another_file.csv" , cluster = 2 , tags = {" manufacturer" : " Enercon" })
45+ }
3846 )
3947
40- filter_set.filter(filter_name = " name__ends_with" , filter_value = " .csv" )
48+ # Single filter, non-nested attribute.
49+ filter_set.filter(name__ends_with = " .csv" )
4150 >> > < FilterSet({< Datafile(' my_file.csv' )> , < Datafile(' another_file.csv' )> })>
4251
43- The following filters are implemented for the following types:
52+ # Two filters, non-nested attributes.
53+ filter_set.filter(name__ends_with = " .csv" , cluster__gt = 1 )
54+ >> > < FilterSet({< Datafile(' another_file.csv' )> })>
55+
56+ # Single filter, nested attribute.
57+ filter_set.filter(tags__manufacturer__startswith(" V" ))
58+ >> > < FilterSet({< Datafile(' my_file.csv' )> , < Datafile(' your_file.csv' )> })>
59+
60+
61+ These filters are currently available for the following types:
4462
4563- ``bool ``:
4664
@@ -67,25 +85,60 @@ The following filters are implemented for the following types:
6785 * ``not_starts_with ``
6886 * ``ends_with ``
6987 * ``not_ends_with ``
88+ * ``in_range ``
89+ * ``not_in_range ``
7090
7191- ``NoneType ``:
7292
7393 * ``is ``
7494 * ``is_not ``
7595
76- - ``TagSet ``:
96+ - ``LabelSet ``:
7797
7898 * ``is ``
7999 * ``is_not ``
80100 * ``equals ``
81101 * ``not_equals ``
82- * ``any_tag_contains ``
83- * ``not_any_tag_contains ``
84- * ``any_tag_starts_with ``
85- * ``not_any_tag_starts_with ``
86- * ``any_tag_ends_with ``
87- * ``not_any_tag_ends_with ``
88-
102+ * ``contains ``
103+ * ``not_contains ``
104+ * ``any_label_contains ``
105+ * ``not_any_label_contains ``
106+ * ``any_label_starts_with ``
107+ * ``not_any_label_starts_with ``
108+ * ``any_label_ends_with ``
109+ * ``not_any_label_ends_with ``
110+
111+ - ``datetime.datetime ``:
112+ * ``is ``
113+ * ``is_not ``
114+ * ``equals ``
115+ * ``not_equals ``
116+ * ``lt `` (less than)
117+ * ``lte `` (less than or equal)
118+ * ``gt `` (greater than)
119+ * ``gte `` (greater than or equal)
120+ * ``in_range ``
121+ * ``not_in_range ``
122+ * ``year_equals ``
123+ * ``year_in ``
124+ * ``month_equals ``
125+ * ``month_in ``
126+ * ``day_equals ``
127+ * ``day_in ``
128+ * ``weekday_equals ``
129+ * ``weekday_in ``
130+ * ``iso_weekday_equals ``
131+ * ``iso_weekday_in ``
132+ * ``time_equals ``
133+ * ``time_in ``
134+ * ``hour_equals ``
135+ * ``hour_in ``
136+ * ``minute_equals ``
137+ * ``minute_in ``
138+ * ``second_equals ``
139+ * ``second_in ``
140+ * ``in_date_range ``
141+ * ``in_time_range ``
89142
90143
91144Additionally, these filters are defined for the following *interfaces * (duck-types). :
@@ -100,6 +153,8 @@ Additionally, these filters are defined for the following *interfaces* (duck-typ
100153 * ``lte ``
101154 * ``gt ``
102155 * ``gte ``
156+ * ``in_range ``
157+ * ``not_in_range ``
103158
104159- Iterables:
105160
@@ -118,14 +173,31 @@ list of filters.
118173--------
119174Ordering
120175--------
121- As sets are inherently orderless, ordering a `` FilterSet `` results in a new ``FilterList ``, which has the same extra
122- methods and behaviour as a `` FilterSet ``, but is based on the ``list `` type instead - meaning it can be ordered and
123- indexed etc. A `` FilterSet `` or `` FilterList `` can be ordered by any of the attributes of its members:
176+ As sets and dictionaries are inherently orderless, ordering any filter container results in a new ``FilterList ``, which
177+ has the same methods and behaviour but is based on ``list `` instead, meaning it can be ordered and indexed etc. A
178+ filter container can be ordered by any of the attributes of its members:
124179
125180.. code-block :: python
126181
127182 filter_set.order_by(" name" )
128183 >> > < FilterList([< Datafile(' another_file.csv' )> , < Datafile(' my_file.csv' )> , < Datafile(path = " your_file.txt" )> ])>
129184
185+ filter_set.order_by(" cluster" )
186+ >> > < FilterList([< Datafile(' my_file.csv' )> , < Datafile(' your_file.csv' )> , < Datafile(path = " another_file.txt" )> ])>
187+
130188 The ordering can also be carried out in reverse (i.e. descending order) by passing ``reverse=True `` as a second argument
131189to the ``order_by `` method.
190+
191+
192+ --------------
193+ ``FilterDict ``
194+ --------------
195+ The keys of a ``FilterDict `` can be anything, but each value must be a ``Filterable ``. Hence, a ``FilterDict `` is
196+ filtered and ordered by its values' attributes; when ordering, its items (key-value tuples) are returned in a
197+ ``FilterList ``.
198+
199+ -----------------------
200+ Using for your own data
201+ -----------------------
202+ If using filter containers for your own data, all the members must inherit from ``octue.mixins.filterable.Filterable ``
203+ to be filterable and orderable.
0 commit comments