Skip to content

Commit 9a04d96

Browse files
committed
Optimized client-side filtering example.
1 parent 1cc6c2c commit 9a04d96

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

c8y_api/model/matcher/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
'match_all',
2525
'AnyMatcher',
2626
'match_any',
27+
'NotMatcher',
28+
'match_not',
2729
'FragmentMatcher',
2830
'fragment',
2931
'FieldMatcher',

samples/client_side_filtering.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from c8y_api.app import SimpleCumulocityApp
44
from c8y_api.model import Device
5-
from c8y_api.model.matcher import field
5+
from c8y_api.model.matcher import field, match_all, match_not
66
from util.testing_util import load_dotenv
77

88
logging.basicConfig(level=logging.DEBUG)
@@ -45,7 +45,7 @@
4545
for d in c8y.device_inventory.select(type='c8y_TestDevice'):
4646
print(f" - {d.name}")
4747

48-
# Option 1: filtering devices by name using Python filters
48+
# Option 1: filtering devices by name using standard Python filters
4949
# The following select statement will simply list "all" devices (there are
5050
# no DB filters) and subsequently filter the results using a standard Python
5151
# list comprehension:
@@ -67,7 +67,7 @@
6767
for d in filtered_devices_2:
6868
print(f" - {d.name}")
6969

70-
# Option 3: the client-side filtering with Python filters
70+
# Option 3: the client-side filtering with custom filters
7171
# The following statement will simply list "all" devices (there are no DB
7272
# filters) and subsequently filter the results using Python matchers. There
7373
# is quite a list of predefined matchers (see c8y_api.model.matchers) and
@@ -82,6 +82,23 @@
8282
for d in filtered_devices_3:
8383
print(f" - {d.name}")
8484

85+
# Option 4: the client-side filtering with nested filters
86+
# The following statement applies the same as above, but using ridiculously
87+
# nested Python matchers to get the same logic.
88+
89+
filtered_devices_4 = c8y.device_inventory.get_all(
90+
type='c8y_TestDevice',
91+
include=match_all(
92+
field('name', '*Device*'),
93+
match_not(
94+
field('name', '*#*')
95+
)
96+
))
97+
# -> Same result
98+
print("Option #4 result (All the same)")
99+
for d in filtered_devices_4:
100+
print(f" - {d.name}")
101+
85102

86103
# cleanup
87104
d1.delete()

0 commit comments

Comments
 (0)