|
2 | 2 |
|
3 | 3 | from c8y_api.app import SimpleCumulocityApp |
4 | 4 | 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 |
6 | 6 | from util.testing_util import load_dotenv |
7 | 7 |
|
8 | 8 | logging.basicConfig(level=logging.DEBUG) |
|
45 | 45 | for d in c8y.device_inventory.select(type='c8y_TestDevice'): |
46 | 46 | print(f" - {d.name}") |
47 | 47 |
|
48 | | -# Option 1: filtering devices by name using Python filters |
| 48 | +# Option 1: filtering devices by name using standard Python filters |
49 | 49 | # The following select statement will simply list "all" devices (there are |
50 | 50 | # no DB filters) and subsequently filter the results using a standard Python |
51 | 51 | # list comprehension: |
|
67 | 67 | for d in filtered_devices_2: |
68 | 68 | print(f" - {d.name}") |
69 | 69 |
|
70 | | -# Option 3: the client-side filtering with Python filters |
| 70 | +# Option 3: the client-side filtering with custom filters |
71 | 71 | # The following statement will simply list "all" devices (there are no DB |
72 | 72 | # filters) and subsequently filter the results using Python matchers. There |
73 | 73 | # is quite a list of predefined matchers (see c8y_api.model.matchers) and |
|
82 | 82 | for d in filtered_devices_3: |
83 | 83 | print(f" - {d.name}") |
84 | 84 |
|
| 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 | + |
85 | 102 |
|
86 | 103 | # cleanup |
87 | 104 | d1.delete() |
|
0 commit comments