Skip to content

Commit 2a75cf6

Browse files
author
Bashar
authored
[RSDK 4604] Refine Examples & Refactor Code (#395)
1 parent fc86a2b commit 2a75cf6

File tree

4 files changed

+53
-33
lines changed

4 files changed

+53
-33
lines changed

docs/examples/example.ipynb

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,12 +1041,12 @@
10411041
"source": [
10421042
"## Connect as a client to app\n",
10431043
"\n",
1044-
"To connect to app as a client and make calls to the data API, you should instantiate an instance of a `ViamClient` and retrieve its `DataClient` member."
1044+
"To connect to app as a client and make calls to the data API, you should create an instance of a `ViamClient` and retrieve its `DataClient` member."
10451045
]
10461046
},
10471047
{
10481048
"cell_type": "code",
1049-
"execution_count": 7,
1049+
"execution_count": 1,
10501050
"metadata": {
10511051
"tags": []
10521052
},
@@ -1075,7 +1075,7 @@
10751075
},
10761076
{
10771077
"cell_type": "code",
1078-
"execution_count": 8,
1078+
"execution_count": 2,
10791079
"metadata": {
10801080
"tags": [
10811081
"remove-input"
@@ -1113,7 +1113,7 @@
11131113
},
11141114
{
11151115
"cell_type": "code",
1116-
"execution_count": 9,
1116+
"execution_count": 3,
11171117
"metadata": {
11181118
"tags": [
11191119
"hide-output"
@@ -1133,14 +1133,27 @@
11331133
},
11341134
{
11351135
"cell_type": "code",
1136-
"execution_count": 10,
1136+
"execution_count": 4,
11371137
"metadata": {},
11381138
"outputs": [
11391139
{
11401140
"name": "stdout",
11411141
"output_type": "stream",
11421142
"text": [
1143-
"[{'IsPowered': False, 'PowerPct': 0.0, 'Position': 0.0}, {'IsPowered': False, 'PowerPct': 0.0, 'Position': 0.0}, {'IsPowered': False, 'PowerPct': 0.0, 'Position': 0.0}]\n"
1143+
"{'IsPowered': False, 'PowerPct': 0.0}\n",
1144+
"method_name: \"IsPowered\"\n",
1145+
"Time requested: 2022-01-01 01:01:01\n",
1146+
"Time received: 2022-12-31 23:59:59\n",
1147+
"\n",
1148+
"{'IsPowered': False, 'PowerPct': 0.0}\n",
1149+
"location_id: \"loc-id\"\n",
1150+
"Time requested: 2023-01-02 00:00:00\n",
1151+
"Time received: 2023-03-04 00:00:00\n",
1152+
"\n",
1153+
"{'Position': 0.0}\n",
1154+
"Time requested: 2023-05-06 00:00:00\n",
1155+
"Time received: 2023-07-08 00:00:00\n",
1156+
"\n"
11441157
]
11451158
}
11461159
],
@@ -1155,7 +1168,8 @@
11551168
")\n",
11561169
"\n",
11571170
"data = await data_client.tabular_data_by_filter(filter=left_motor_filter)\n",
1158-
"print(data)"
1171+
"for tab in data:\n",
1172+
" print(tab)"
11591173
]
11601174
},
11611175
{
@@ -1167,7 +1181,7 @@
11671181
},
11681182
{
11691183
"cell_type": "code",
1170-
"execution_count": 11,
1184+
"execution_count": 5,
11711185
"metadata": {
11721186
"tags": []
11731187
},
@@ -1197,7 +1211,7 @@
11971211
},
11981212
{
11991213
"cell_type": "code",
1200-
"execution_count": 13,
1214+
"execution_count": 6,
12011215
"metadata": {
12021216
"tags": [
12031217
"hide-output"
@@ -1217,7 +1231,7 @@
12171231
},
12181232
{
12191233
"cell_type": "code",
1220-
"execution_count": 14,
1234+
"execution_count": 7,
12211235
"metadata": {},
12221236
"outputs": [
12231237
{
@@ -1254,7 +1268,7 @@
12541268
},
12551269
{
12561270
"cell_type": "code",
1257-
"execution_count": 15,
1271+
"execution_count": 8,
12581272
"metadata": {
12591273
"tags": []
12601274
},
@@ -1274,12 +1288,12 @@
12741288
"cell_type": "markdown",
12751289
"metadata": {},
12761290
"source": [
1277-
"Certain types returned by `AppClient` methods (namely, `RobotPart`, `LogEntry`, and `Fragment` objects) are wrapped in mirror classes that convert proto data into a friendlier, more readable format. Each of these mirrors still allows access to the intial proto type returned by the response as a property."
1291+
"Certain types returned by `AppClient` methods (namely, `RobotPart`, `LogEntry`, `Fragment`, and `RobotPartHistoryEntry` objects) are wrapped in mirror classes that convert proto data into a friendlier, more readable format. Each of these mirrors still allows access to the intial proto type returned by the response as a property."
12781292
]
12791293
},
12801294
{
12811295
"cell_type": "code",
1282-
"execution_count": 16,
1296+
"execution_count": 9,
12831297
"metadata": {},
12841298
"outputs": [
12851299
{
@@ -1340,7 +1354,7 @@
13401354
},
13411355
{
13421356
"cell_type": "code",
1343-
"execution_count": 17,
1357+
"execution_count": 10,
13441358
"metadata": {
13451359
"tags": []
13461360
},

src/viam/app/data_client.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -470,28 +470,31 @@ async def tabular_data_capture_upload(
470470
471471
Raises:
472472
GRPCError: If an invalid part ID is passed.
473-
AssertionError: If a list of `Timestamp` objects is provided and its length does not match the length of the list of tabular
473+
ValueError: If a list of `Timestamp` objects is provided and its length does not match the length of the list of tabular
474474
data.
475475
"""
476-
sensor_contents = [SensorData()] * len(tabular_data)
476+
sensor_contents = []
477477
if data_request_times:
478-
assert len(data_request_times) == len(tabular_data)
478+
if len(data_request_times) != len(tabular_data):
479+
raise ValueError("data_request_times and tabular_data lengths must be equal.")
479480

480-
for i in range(len(tabular_data)):
481+
for idx, tab in enumerate(tabular_data):
481482
s = Struct()
482-
s.update(tabular_data[i])
483-
sensor_contents[i] = SensorData(
484-
metadata=(
485-
SensorMetadata(
486-
time_requested=datetime_to_timestamp(data_request_times[i][0]) if data_request_times[i][0] else None,
487-
time_received=datetime_to_timestamp(data_request_times[i][1]) if data_request_times[i][1] else None,
483+
s.update(tab)
484+
sensor_contents.append(
485+
SensorData(
486+
metadata=(
487+
SensorMetadata(
488+
time_requested=datetime_to_timestamp(data_request_times[idx][0]) if data_request_times[idx][0] else None,
489+
time_received=datetime_to_timestamp(data_request_times[idx][1]) if data_request_times[idx][1] else None,
490+
)
491+
if data_request_times[idx]
492+
else None
488493
)
489-
if data_request_times[i]
490-
else None
494+
if data_request_times
495+
else None,
496+
struct=s,
491497
)
492-
if data_request_times
493-
else None,
494-
struct=s,
495498
)
496499

497500
metadata = UploadMetadata(
@@ -675,11 +678,11 @@ def create_filter(
675678
mime_type=mime_type,
676679
interval=(
677680
CaptureInterval(
678-
start=datetime_to_timestamp(start_time) if start_time else None,
679-
end=datetime_to_timestamp(end_time) if end_time else None,
681+
start=datetime_to_timestamp(start_time),
682+
end=datetime_to_timestamp(end_time),
680683
)
681684
)
682-
if start_time and end_time
685+
if start_time or end_time
683686
else None,
684687
tags_filter=TagsFilter(tags=tags),
685688
bbox_labels=bbox_labels,

src/viam/services/motion/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from viam.proto.service.motion import MotionConfiguration
2+
23
from .client import MotionClient
34

45
__all__ = [

src/viam/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ def struct_to_dict(struct: Struct) -> Dict[str, ValueTypes]:
153153
return {key: value_to_primitive(value) for (key, value) in struct.fields.items()}
154154

155155

156-
def datetime_to_timestamp(dt: datetime) -> Timestamp:
156+
def datetime_to_timestamp(dt: Optional[datetime]) -> Optional[Timestamp]:
157+
if dt is None:
158+
return None
157159
timestamp = Timestamp()
158160
timestamp.FromDatetime(dt)
159161
return timestamp

0 commit comments

Comments
 (0)