Skip to content

Commit 5309e35

Browse files
authored
chore: change docstring examples to showcase instance_id first (#2676)
1 parent d8e8d83 commit 5309e35

4 files changed

Lines changed: 172 additions & 88 deletions

File tree

cognite/client/_api/datapoints.py

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
LatestDatapointList,
4646
LatestDatapointQuery,
4747
)
48-
from cognite.client.data_classes.data_modeling.ids import NodeId
48+
from cognite.client.data_classes.data_modeling import NodeId
4949
from cognite.client.data_classes.datapoint_aggregates import Aggregate
5050
from cognite.client.exceptions import CogniteAPIError, CogniteNotFoundError
5151
from cognite.client.utils import _json_extended as _json
@@ -1029,16 +1029,16 @@ async def retrieve(
10291029
Examples:
10301030
10311031
You can specify the identifiers of the datapoints you wish to retrieve in a number of ways. In this example
1032-
we are using the time-ago format, ``"2w-ago"`` to get raw data for the time series with id=42 from 2 weeks ago up until now.
1032+
we are using the time-ago format, ``"2w-ago"`` to get raw data for a time series from 2 weeks ago up until now.
10331033
You can also use the time-ahead format, like ``"3d-ahead"``, to specify a relative time in the future.
10341034
10351035
>>> from cognite.client import CogniteClient, AsyncCogniteClient
1036+
>>> from cognite.client.data_classes.data_modeling import NodeId
10361037
>>> client = CogniteClient()
10371038
>>> # async_client = AsyncCogniteClient() # another option
1038-
>>> dps = client.time_series.data.retrieve(id=42, start="2w-ago")
1039-
>>> # You can also use instance_id:
1040-
>>> from cognite.client.data_classes.data_modeling import NodeId
1041-
>>> dps = client.time_series.data.retrieve(instance_id=NodeId("ts-space", "foo"))
1039+
>>> dps = client.time_series.data.retrieve(
1040+
... instance_id=NodeId("ts-space", "foo"), start="2w-ago"
1041+
... )
10421042
10431043
Although raw datapoints are returned by default, you can also get aggregated values, such as `max` or `average`. You may also fetch more than one time series simultaneously. Here we are
10441044
getting daily averages and maximum values for all of 2018, for two different time series, where we're specifying `start` and `end` as integers
@@ -1396,14 +1396,15 @@ async def retrieve_arrays(
13961396
13971397
Examples:
13981398
1399-
Get weekly ``min`` and ``max`` aggregates for a time series with id=42 since the year 2000, then compute the range of values:
1399+
Get weekly ``min`` and ``max`` aggregates for a time series using instance_id, then compute the range of values:
14001400
14011401
>>> from cognite.client import CogniteClient
1402+
>>> from cognite.client.data_classes.data_modeling import NodeId
14021403
>>> from datetime import datetime, timezone
14031404
>>> client = CogniteClient()
14041405
>>> # async_client = AsyncCogniteClient() # another option
14051406
>>> dps = client.time_series.data.retrieve_arrays(
1406-
... id=42,
1407+
... instance_id=NodeId("my-space", "my-ts-xid"),
14071408
... start=datetime(2020, 1, 1, tzinfo=timezone.utc),
14081409
... aggregates=["min", "max"],
14091410
... granularity="7d",
@@ -1536,14 +1537,15 @@ async def retrieve_dataframe(
15361537
15371538
Examples:
15381539
1539-
Get a pandas dataframe using a single time series external ID, with data from the last two weeks,
1540+
Get a pandas dataframe using a single time series instance ID, with data from the last two weeks,
15401541
but with no more than 100 datapoints:
15411542
15421543
>>> from cognite.client import CogniteClient, AsyncCogniteClient
1544+
>>> from cognite.client.data_classes.data_modeling import NodeId
15431545
>>> client = CogniteClient()
15441546
>>> # async_client = AsyncCogniteClient() # another option
15451547
>>> df = client.time_series.data.retrieve_dataframe(
1546-
... external_id="foo", start="2w-ago", end="now", limit=100
1548+
... instance_id=NodeId("my-space", "my-ts-xid"), start="2w-ago", end="now", limit=100
15471549
... )
15481550
15491551
Get the pandas dataframe with a uniform index (fixed spacing between points) of 1 day, for two time series with
@@ -1574,11 +1576,11 @@ async def retrieve_dataframe(
15741576
... include_aggregate_name=False,
15751577
... )
15761578
1577-
You may also use ``pandas.Timestamp`` to define start and end. Here we fetch using instance_id:
1579+
You may also use ``pandas.Timestamp`` to define start and end:
15781580
15791581
>>> import pandas as pd
15801582
>>> df = client.time_series.data.retrieve_dataframe(
1581-
... instance_id=NodeId("my-space", "my-ts-xid"),
1583+
... external_id="foo",
15821584
... start=pd.Timestamp("2023-01-01"),
15831585
... end=pd.Timestamp("2023-02-01"),
15841586
... )
@@ -1849,18 +1851,18 @@ async def retrieve_latest(
18491851
Getting the latest datapoint in a time series:
18501852
18511853
>>> from cognite.client import CogniteClient, AsyncCogniteClient
1854+
>>> from cognite.client.data_classes.data_modeling import NodeId
18521855
>>> client = CogniteClient()
18531856
>>> # async_client = AsyncCogniteClient() # another option
1854-
>>> res = client.time_series.data.retrieve_latest(id=1)
1857+
>>> res = client.time_series.data.retrieve_latest(
1858+
... instance_id=NodeId("my-space", "my-ts-xid")
1859+
... )
18551860
>>> if res: # Check if datapoint exists
18561861
... print(res.timestamp, res.value)
18571862
1858-
You can also use external_id or instance_id; single identifier or list of identifiers:
1863+
You can also use id or external_id; single identifier or list of identifiers:
18591864
1860-
>>> from cognite.client.data_classes.data_modeling import NodeId
1861-
>>> res = client.time_series.data.retrieve_latest(
1862-
... external_id=["foo", "bar"], instance_id=NodeId("my-space", "my-ts-xid")
1863-
... )
1865+
>>> res = client.time_series.data.retrieve_latest(id=1, external_id=["foo", "bar"])
18641866
18651867
You can also get the latest datapoint before a specific time:
18661868
@@ -1978,6 +1980,7 @@ async def insert(
19781980
19791981
>>> from cognite.client import CogniteClient
19801982
>>> from cognite.client.data_classes import StatusCode
1983+
>>> from cognite.client.data_classes.data_modeling import NodeId
19811984
>>> from datetime import datetime, timezone
19821985
>>> client = CogniteClient()
19831986
>>> # async_client = AsyncCogniteClient() # another option
@@ -1987,20 +1990,19 @@ async def insert(
19871990
... (datetime(2018, 1, 3, tzinfo=timezone.utc), 3000, StatusCode.Uncertain),
19881991
... (datetime(2018, 1, 4, tzinfo=timezone.utc), None, StatusCode.Bad),
19891992
... ]
1990-
>>> client.time_series.data.insert(datapoints, id=1)
1993+
>>> client.time_series.data.insert(
1994+
... datapoints, instance_id=NodeId("my-space", "my-ts-xid")
1995+
... )
19911996
19921997
The timestamp can be given by datetime as above, or in milliseconds since epoch. Status codes can also be
19931998
passed as normal integers; this is necessary if a subcategory or modifier flag is needed, e.g. 3145728: 'GoodClamped':
19941999
1995-
>>> from cognite.client.data_classes.data_modeling import NodeId
19962000
>>> datapoints = [
19972001
... (150000000000, 1000),
19982002
... (160000000000, 2000, 3145728),
19992003
... (170000000000, 2000, 2147483648), # Same as StatusCode.Bad
20002004
... ]
2001-
>>> client.time_series.data.insert(
2002-
... datapoints, instance_id=NodeId("my-space", "my-ts-xid")
2003-
... )
2005+
>>> client.time_series.data.insert(datapoints, id=1)
20042006
20052007
Or they can be a list of dictionaries:
20062008
@@ -2161,9 +2163,12 @@ async def delete_range(
21612163
Deleting the last week of data from a time series:
21622164
21632165
>>> from cognite.client import CogniteClient, AsyncCogniteClient
2166+
>>> from cognite.client.data_classes.data_modeling import NodeId
21642167
>>> client = CogniteClient()
21652168
>>> # async_client = AsyncCogniteClient() # another option
2166-
>>> client.time_series.data.delete_range(start="1w-ago", end="now", id=1)
2169+
>>> client.time_series.data.delete_range(
2170+
... start="1w-ago", end="now", instance_id=NodeId("my-space", "my-ts-xid")
2171+
... )
21672172
21682173
Deleting the data from now until 2 days in the future from a time series containing e.g. forecasted data:
21692174

cognite/client/_api/files.py

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,13 @@ async def retrieve(
272272
273273
Examples:
274274
275-
Get file metadata by id:
275+
Get file metadata by instance id:
276276
277277
>>> from cognite.client import CogniteClient, AsyncCogniteClient
278+
>>> from cognite.client.data_classes.data_modeling import NodeId
278279
>>> client = CogniteClient()
279280
>>> # async_client = AsyncCogniteClient() # another option
280-
>>> res = client.files.retrieve(id=1)
281+
>>> res = client.files.retrieve(instance_id=NodeId("my-space", "my-file-xid"))
281282
282283
Get file metadata by external id:
283284
@@ -308,14 +309,17 @@ async def retrieve_multiple(
308309
309310
Examples:
310311
311-
Get file metadatas by id:
312+
Get file metadatas by instance id:
312313
313314
>>> from cognite.client import CogniteClient, AsyncCogniteClient
315+
>>> from cognite.client.data_classes.data_modeling import NodeId
314316
>>> client = CogniteClient()
315317
>>> # async_client = AsyncCogniteClient() # another option
316-
>>> res = client.files.retrieve_multiple(ids=[1, 2, 3])
318+
>>> res = client.files.retrieve_multiple(
319+
... instance_ids=[NodeId("my-space", "my-file-xid")]
320+
... )
317321
318-
Get file_metadatas by external id:
322+
Get file metadatas by external id:
319323
320324
>>> res = client.files.retrieve_multiple(external_ids=["abc", "def"])
321325
"""
@@ -501,8 +505,21 @@ async def upload_content(
501505
path (Path | str): Local file path.
502506
external_id (str | None): The external ID provided by the client. Must be unique within the project.
503507
instance_id (NodeId | tuple[str, str] | None): Instance ID of the file (CogniteFile).
508+
504509
Returns:
505510
FileMetadata: No description.
511+
512+
Examples:
513+
514+
Upload file content using instance_id:
515+
516+
>>> from cognite.client import CogniteClient, AsyncCogniteClient
517+
>>> from cognite.client.data_classes.data_modeling import NodeId
518+
>>> client = CogniteClient()
519+
>>> # async_client = AsyncCogniteClient() # another option
520+
>>> res = client.files.upload_content(
521+
... "/path/to/file.txt", instance_id=NodeId("my-space", "my-file-xid")
522+
... )
506523
"""
507524
path = Path(path)
508525
if path.is_dir():
@@ -735,19 +752,19 @@ async def upload_content_bytes(
735752
736753
Examples:
737754
738-
Finish a file creation by uploading the content using external_id:
755+
Finish a file creation by uploading the content using instance_id:
739756
740757
>>> from cognite.client import CogniteClient, AsyncCogniteClient
758+
>>> from cognite.client.data_classes.data_modeling import NodeId
741759
>>> client = CogniteClient()
742760
>>> # async_client = AsyncCogniteClient() # another option
743-
>>> res = client.files.upload_content_bytes(b"some content", external_id="my_file_xid")
744-
745-
...or by using instance_id:
746-
747-
>>> from cognite.client.data_classes.data_modeling import NodeId
748761
>>> res = client.files.upload_content_bytes(
749762
... b"some content", instance_id=NodeId("my-space", "my_file_xid")
750763
... )
764+
765+
...or by using external_id:
766+
767+
>>> res = client.files.upload_content_bytes(b"some content", external_id="my_file_xid")
751768
"""
752769
identifiers = IdentifierSequence.load(external_ids=external_id, instance_ids=instance_id).as_singleton()
753770

@@ -1027,10 +1044,11 @@ async def multipart_upload_content_session(
10271044
Upload binary data in two chunks:
10281045
10291046
>>> from cognite.client import CogniteClient, AsyncCogniteClient
1047+
>>> from cognite.client.data_classes.data_modeling import NodeId
10301048
>>> client = CogniteClient()
10311049
>>> # async_client = AsyncCogniteClient() # another option
10321050
>>> with client.files.multipart_upload_content_session(
1033-
... external_id="external-id", parts=2
1051+
... instance_id=NodeId("my-space", "my-file-xid"), parts=2
10341052
... ) as session:
10351053
... # Note that the minimum chunk size is 5 MiB.
10361054
... session.upload_part(0, "hello" * 1_200_000)
@@ -1159,6 +1177,18 @@ async def retrieve_download_urls(
11591177
11601178
Returns:
11611179
dict[int, str] | dict[str, str] | dict[NodeId, str] | dict[int | str | NodeId, str]: Dictionary containing download urls.
1180+
1181+
Examples:
1182+
1183+
Get download URLs by instance id:
1184+
1185+
>>> from cognite.client import CogniteClient, AsyncCogniteClient
1186+
>>> from cognite.client.data_classes.data_modeling import NodeId
1187+
>>> client = CogniteClient()
1188+
>>> # async_client = AsyncCogniteClient() # another option
1189+
>>> urls = client.files.retrieve_download_urls(
1190+
... instance_id=NodeId("my-space", "my-file-xid")
1191+
... )
11621192
"""
11631193
identifiers = IdentifierSequence.load(ids=id, external_ids=external_id, instance_ids=instance_id)
11641194

@@ -1251,9 +1281,10 @@ async def download(
12511281
... directory="my_directory", id=[1, 2, 3], external_id=["abc", "def"]
12521282
... )
12531283
1254-
Download files by id to the current directory:
1284+
Download files by instance id to the current directory:
12551285
1256-
>>> client.files.download(directory=".", id=[1, 2, 3])
1286+
>>> from cognite.client.data_classes.data_modeling import NodeId
1287+
>>> client.files.download(directory=".", instance_id=NodeId("my-space", "my-file-xid"))
12571288
"""
12581289
identifiers = IdentifierSequence.load(ids=id, external_ids=external_id, instance_ids=instance_id)
12591290

@@ -1389,11 +1420,14 @@ async def download_to_path(
13891420
13901421
Examples:
13911422
1392-
Download a file by id:
1423+
Download a file by instance id:
13931424
>>> from cognite.client import CogniteClient, AsyncCogniteClient
1425+
>>> from cognite.client.data_classes.data_modeling import NodeId
13941426
>>> client = CogniteClient()
13951427
>>> # async_client = AsyncCogniteClient() # another option
1396-
>>> client.files.download_to_path("~/mydir/my_downloaded_file.txt", id=123)
1428+
>>> client.files.download_to_path(
1429+
... "~/mydir/my_downloaded_file.txt", instance_id=NodeId("my-space", "my-file-xid")
1430+
... )
13971431
"""
13981432
path = Path(path)
13991433
if not path.parent.is_dir():
@@ -1418,9 +1452,12 @@ async def download_bytes(
14181452
Download a file's content into memory:
14191453
14201454
>>> from cognite.client import CogniteClient, AsyncCogniteClient
1455+
>>> from cognite.client.data_classes.data_modeling import NodeId
14211456
>>> client = CogniteClient()
14221457
>>> # async_client = AsyncCogniteClient() # another option
1423-
>>> file_content = client.files.download_bytes(id=1)
1458+
>>> file_content = client.files.download_bytes(
1459+
... instance_id=NodeId("my-space", "my-file-xid")
1460+
... )
14241461
14251462
Returns:
14261463
bytes: The file in binary format

0 commit comments

Comments
 (0)