Skip to content

Commit 76ecc46

Browse files
committed
Changed as_tuples parameter in select/get_all functions to as_values as sometimes you really just want to have one value and don't want to deal with a 1-tuple.
1 parent 478003b commit 76ecc46

17 files changed

+121
-110
lines changed

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Changelog
22

3-
* Added `as_tuples` parameter to `get_all` and `select` functions of the Inventory, DeviceInventory,
3+
* Added `as_values` parameter to `get_all` and `select` functions of the Inventory, DeviceInventory,
44
DeviceGroupInventory, Events, Alarms, Users, Operations, and AuditRecords API.
55
* Added code coverage reporting to `test` target for _invoke_.
6-
* Updated `as_tuple` for complex objects as well as the `as_tuples` parameter for `select`
6+
* Updated `as_tuple` for complex objects as well as the `as_values` parameter for `select`
77
and `get_all` functions to work with strings or 2-tuples. The use of a dictionary
88
was removed as dictionaries don't define an order.
9-
* Added `as_tuples` parameter to the Measurements API `select` and `get_all` functions.
9+
* Added `as_values` parameter to the Measurements API `select` and `get_all` functions.
1010
* Adding `c8y_tk.analytics` package with `to_numpy`, `to_series` and `to_data_frame` functions to
1111
ease incorporating Cumulocity data into standard analytics pipelines.
1212

c8y_api/model/_parser.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
from c8y_api.model._util import _StringUtil
99

1010

11-
def as_tuples(json_data, paths: str | tuple | list[str|tuple]):
12-
"""Parse a JSON structure as tuples from paths.
11+
def as_values(json_data, paths: str | tuple | list[str | tuple]):
12+
"""Parse a JSON structure as value(s) from paths.
1313
1414
Args:
1515
json_data (dict): A JSON structure as Python dict
@@ -23,7 +23,8 @@ def as_tuples(json_data, paths: str | tuple | list[str|tuple]):
2323
2424
Returns:
2525
A tuple with `len(path)` elements containing the values as-is defined
26-
in the JSON structure; an invalid path will result in None.
26+
in the JSON structure or a single value if `len(path) == 1` ; an
27+
invalid path will result in None.
2728
"""
2829
def resolve(segments, default=None):
2930
json_level = json_data
@@ -39,9 +40,13 @@ def resolve(segments, default=None):
3940
return json_level.get(segments[-1], json_level.get(_StringUtil.to_pascal_case(segments[-1]), default))
4041

4142
# each p in path(s) can be a string or a tuple
43+
if isinstance(paths, str):
44+
return resolve(paths.split('.'))
45+
if isinstance(paths, tuple):
46+
return resolve(paths[0].split('.'), paths[1])
4247
return tuple(
4348
resolve(p[0].split('.'), p[1]) if isinstance(p, tuple)
44-
else resolve(p.split('.')) for p in ([paths] if isinstance(paths, (str, tuple)) else paths))
49+
else resolve(p.split('.')) for p in paths)
4550

4651
class SimpleObjectParser(object):
4752
"""A parser for simple (without fragments) Cumulocity database objects.

c8y_api/model/administration.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from c8y_api._base_api import CumulocityRestApi, AccessDeniedError
1111
from c8y_api.model._base import CumulocityResource, SimpleObject
12-
from c8y_api.model._parser import SimpleObjectParser, ComplexObjectParser, as_tuples as parse_as_tuples
12+
from c8y_api.model._parser import SimpleObjectParser, ComplexObjectParser, as_values as parse_as_values
1313
from c8y_api.model._util import _DateUtil
1414

1515

@@ -1067,7 +1067,7 @@ def select(
10671067
limit: int = None,
10681068
page_size: int = 5,
10691069
page_number: int = None,
1070-
as_tuples: str | tuple | list[str | tuple] = None,
1070+
as_values: str | tuple | list[str | tuple] = None,
10711071
**kwargs
10721072
) -> Generator[User]:
10731073
"""Lazily select and yield User instances.
@@ -1092,7 +1092,7 @@ def select(
10921092
parsed in one chunk). This is a performance related setting.
10931093
page_number (int): Pull a specific page; this effectively disables
10941094
automatic follow-up page retrieval.
1095-
as_tuples: (*str|tuple): Don't parse objects, but directly extract
1095+
as_values: (*str|tuple): Don't parse objects, but directly extract
10961096
the values at certain JSON paths as tuples; If the path is not
10971097
defined in a result, None is used; Specify a tuple to define
10981098
a proper default value for each path.
@@ -1131,8 +1131,8 @@ def select(
11311131
base_query,
11321132
page_number,
11331133
limit,
1134-
User.from_json if not as_tuples else
1135-
lambda x: parse_as_tuples(x, as_tuples))
1134+
User.from_json if not as_values else
1135+
lambda x: parse_as_values(x, as_values))
11361136

11371137
def get_all(
11381138
self,
@@ -1142,7 +1142,7 @@ def get_all(
11421142
only_devices: bool = None,
11431143
with_subusers_count: bool = None,
11441144
page_size: int = 1000,
1145-
as_tuples: str | tuple | list[str|tuple] = None,
1145+
as_values: str | tuple | list[str|tuple] = None,
11461146
**kwargs
11471147
) -> list[User]:
11481148
"""Select and retrieve User instances as list.
@@ -1161,7 +1161,7 @@ def get_all(
11611161
only_devices=only_devices,
11621162
with_subusers_count=with_subusers_count,
11631163
page_size=page_size,
1164-
as_tuples=as_tuples,
1164+
as_values=as_values,
11651165
**kwargs))
11661166

11671167
def create(self, *users):

c8y_api/model/alarms.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from c8y_api._base_api import CumulocityRestApi
99
from c8y_api.model._base import CumulocityResource, SimpleObject, ComplexObject
10-
from c8y_api.model._parser import as_tuples as parse_as_tuples, ComplexObjectParser
10+
from c8y_api.model._parser import as_values as parse_as_values, ComplexObjectParser
1111
from c8y_api.model._util import _DateUtil
1212

1313

@@ -255,7 +255,7 @@ def select(self,
255255
reverse: bool = False, limit: int = None,
256256
with_source_assets: bool = None, with_source_devices: bool = None,
257257
page_size: int = 1000, page_number: int = None,
258-
as_tuples: str | tuple | list[str | tuple] = None,
258+
as_values: str | tuple | list[str | tuple] = None,
259259
**kwargs) -> Generator[Alarm]:
260260
"""Query the database for alarms and iterate over the results.
261261
@@ -307,7 +307,7 @@ def select(self,
307307
parsed in one chunk). This is a performance related setting.
308308
page_number (int): Pull a specific page; this effectively disables
309309
automatic follow-up page retrieval.
310-
as_tuples: (*str|tuple): Don't parse objects, but directly extract
310+
as_values: (*str|tuple): Don't parse objects, but directly extract
311311
the values at certain JSON paths as tuples; If the path is not
312312
defined in a result, None is used; Specify a tuple to define
313313
a proper default value for each path.
@@ -333,8 +333,8 @@ def select(self,
333333
base_query,
334334
page_number,
335335
limit,
336-
Alarm.from_json if not as_tuples else
337-
lambda x: parse_as_tuples(x, as_tuples))
336+
Alarm.from_json if not as_values else
337+
lambda x: parse_as_values(x, as_values))
338338

339339
def get_all(
340340
self,
@@ -351,7 +351,7 @@ def get_all(
351351
with_source_assets: bool = None, with_source_devices: bool = None,
352352
reverse: bool = False, limit: int = None,
353353
page_size: int = 1000, page_number: int = None,
354-
as_tuples: str | tuple | list[str|tuple] = None,
354+
as_values: str | tuple | list[str | tuple] = None,
355355
**kwargs) -> List[Alarm]:
356356
"""Query the database for alarms and return the results as list.
357357
@@ -376,7 +376,7 @@ def get_all(
376376
min_age=min_age, max_age=max_age, reverse=reverse,
377377
with_source_devices=with_source_devices, with_source_assets=with_source_assets,
378378
limit=limit, page_size=page_size, page_number=page_number,
379-
as_tuples=as_tuples,
379+
as_values=as_values,
380380
**kwargs))
381381

382382
def count(

c8y_api/model/audit.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from c8y_api._base_api import CumulocityRestApi
1010
from c8y_api.model._base import CumulocityResource, ComplexObject
11-
from c8y_api.model._parser import ComplexObjectParser, SimpleObjectParser, as_tuples as parse_as_tuples
11+
from c8y_api.model._parser import ComplexObjectParser, SimpleObjectParser, as_values as parse_as_values
1212
from c8y_api.model._util import _DateUtil
1313

1414

@@ -207,7 +207,7 @@ def select(
207207
min_age: timedelta = None, max_age: timedelta = None,
208208
reverse: bool = False, limit: int = None,
209209
page_size: int = 1000, page_number: int = None,
210-
as_tuples: str | tuple | list[str | tuple] = None,
210+
as_values: str | tuple | list[str | tuple] = None,
211211
**kwargs
212212
) -> Generator[AuditRecord]:
213213
"""Query the database for audit records and iterate over the results.
@@ -240,7 +240,7 @@ def select(
240240
parsed in one chunk). This is a performance related setting.
241241
page_number (int): Pull a specific page; this effectively disables
242242
automatic follow-up page retrieval.
243-
as_tuples: (*str|tuple): Don't parse objects, but directly extract
243+
as_values: (*str|tuple): Don't parse objects, but directly extract
244244
the values at certain JSON paths as tuples; If the path is not
245245
defined in a result, None is used; Specify a tuple to define
246246
a proper default value for each path.
@@ -259,8 +259,8 @@ def select(
259259
base_query,
260260
page_number,
261261
limit,
262-
AuditRecord.from_json if not as_tuples else
263-
lambda x: parse_as_tuples(x, as_tuples))
262+
AuditRecord.from_json if not as_values else
263+
lambda x: parse_as_values(x, as_values))
264264

265265
def get_all(
266266
self,
@@ -270,7 +270,7 @@ def get_all(
270270
min_age: timedelta = None, max_age: timedelta = None,
271271
reverse: bool = False, limit: int = None,
272272
page_size: int = 1000, page_number: int = None,
273-
as_tuples: str | tuple | list[str | tuple] = None,
273+
as_values: str | tuple | list[str | tuple] = None,
274274
**kwargs
275275
) -> List[AuditRecord]:
276276
"""Query the database for audit records and return the results as list.
@@ -289,7 +289,7 @@ def get_all(
289289
before=before, after=after,
290290
min_age=min_age, max_age=max_age,
291291
reverse=reverse, limit=limit, page_size=page_size, page_number=page_number,
292-
as_tuples=as_tuples,
292+
as_values=as_values,
293293
**kwargs,
294294
))
295295

c8y_api/model/events.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from c8y_api._base_api import CumulocityRestApi
1212
from c8y_api.model._base import CumulocityResource, SimpleObject, ComplexObject
13-
from c8y_api.model._parser import as_tuples as parse_as_tuples, ComplexObjectParser
13+
from c8y_api.model._parser import as_values as parse_as_values, ComplexObjectParser
1414
from c8y_api.model._util import _DateUtil
1515

1616

@@ -264,7 +264,7 @@ def select(self,
264264
reverse: bool = False, limit: int = None,
265265
with_source_assets: bool = None, with_source_devices: bool = None,
266266
page_size: int = 1000, page_number: int = None,
267-
as_tuples: str | tuple | list[str|tuple] = None,
267+
as_values: str | tuple | list[str | tuple] = None,
268268
**kwargs) -> Generator[Event]:
269269
"""Query the database for events and iterate over the results.
270270
@@ -315,7 +315,7 @@ def select(self,
315315
parsed in one chunk). This is a performance related setting.
316316
page_number (int): Pull a specific page; this effectively disables
317317
automatic follow-up page retrieval.
318-
as_tuples: (*str|tuple): Don't parse objects, but directly extract
318+
as_values: (*str|tuple): Don't parse objects, but directly extract
319319
the values at certain JSON paths as tuples; If the path is not
320320
defined in a result, None is used; Specify a tuple to define
321321
a proper default value for each path.
@@ -344,8 +344,8 @@ def select(self,
344344
base_query,
345345
page_number,
346346
limit,
347-
Event.from_json if not as_tuples else
348-
lambda x: parse_as_tuples(x, as_tuples))
347+
Event.from_json if not as_values else
348+
lambda x: parse_as_values(x, as_values))
349349

350350
def get_all(
351351
self,
@@ -362,7 +362,7 @@ def get_all(
362362
reverse: bool = False, limit: int = None,
363363
with_source_assets: bool = None, with_source_devices: bool = None,
364364
page_size: int = 1000, page_number: int = None,
365-
as_tuples: str | tuple | list[str|tuple] = None,
365+
as_values: str | tuple | list[str | tuple] = None,
366366
**kwargs) -> List[Event]:
367367
"""Query the database for events and return the results as list.
368368
@@ -388,7 +388,7 @@ def get_all(
388388
reverse=reverse,
389389
with_source_devices=with_source_devices, with_source_assets=with_source_assets,
390390
limit=limit, page_size=page_size, page_number=page_number,
391-
as_tuples=as_tuples,
391+
as_values=as_values,
392392
**kwargs
393393
))
394394

0 commit comments

Comments
 (0)