Pygen
cognite.pygen
This is the main entry point for the pygen package. It contains the main functions for generating SDKs.
_QueryExecutor
Class for executing queries against the Domain Model Storage (DMS) endpoints in CDF.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
CogniteClient
|
An instance of the CogniteClient. |
required |
views
|
Sequence[View]
|
A list of views to use for the queries. Defaults to None. If not passed, the views will be fetched from the server when needed. |
None
|
Source code in cognite/pygen/_query/interface.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
|
_equals_none_to_not_exists(filter)
classmethod
Converts all Equals([property], None) filters to Not(Exists([property])) filters.
The motivation is that the DMS API does not support Equals([property], None) filters, and it is more intuitive to use Equals([property], None) filters in the query builder.
Source code in cognite/pygen/_query/interface.py
aggregate(view, aggregates, group_by=None, filter=None, query=None, search_properties=None, limit=None)
Aggregate nodes/edges in a view.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
view
|
ViewId
|
The view in which the nodes/edges have properties. |
required |
aggregates
|
Aggregation | Sequence[Aggregation]
|
The aggregations to perform. |
required |
group_by
|
str | SequenceNotStr[str] | None
|
The properties to group by. |
None
|
filter
|
Filter | None
|
The filter to apply ahead of the aggregation. |
None
|
query
|
str | None
|
The search query. This is useful when you want to show the number of results of a specific search query. It is useful for combining with the search method. |
None
|
search_properties
|
str | SequenceNotStr[str] | None
|
The properties to search. If None, all text properties are searched. |
None
|
limit
|
int | None
|
The maximum number of results to return. Max 1000. |
None
|
Returns:
Type | Description |
---|---|
dict[str, Any] | list[dict[str, Any]]
|
dict[str, Any] | list[dict[str, Any]]: The aggregation results. |
Source code in cognite/pygen/_query/interface.py
list(view, properties, filter=None, sort=None, limit=None)
List nodes/edges in a view.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
view
|
ViewId
|
The view in which the nodes/edges have properties. |
required |
properties
|
SelectedProperties
|
The properties to include in the result. |
required |
filter
|
Filter | None
|
The filter to apply ahead of the list operation. |
None
|
sort
|
Sequence[InstanceSort] | InstanceSort | None
|
The sort order of the results. |
None
|
limit
|
int | None
|
The maximum number of results to return. Pagination is handled automatically. |
None
|
Source code in cognite/pygen/_query/interface.py
search(view, properties=None, query=None, filter=None, search_properties=None, sort=None, limit=None)
Search for nodes/edges in a view.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
view
|
ViewId
|
The view in which the nodes/edges have properties. |
required |
properties
|
SelectedProperties | None
|
The properties to in include in the result. If None, all properties are included. |
None
|
query
|
str | None
|
The search query. |
None
|
filter
|
Filter | None
|
The filter to apply ahead of the search. |
None
|
search_properties
|
str | SequenceNotStr[str] | None
|
The properties to search. If None, all text properties are searched. |
None
|
sort
|
Sequence[InstanceSort] | InstanceSort | None
|
The sort order of the results. |
None
|
limit
|
int | None
|
The maximum number of results to return. Max 1000. |
None
|
Returns:
Type | Description |
---|---|
list[dict[str, Any]]
|
list[dict[str, Any]]: The search results. |
Source code in cognite/pygen/_query/interface.py
build_wheel(model_id, client=None, *, top_level_package=None, client_name=None, default_instance_space=None, output_dir=Path('dist'), format_code=True, config=None)
Generates a wheel with Python SDK tailored to the given Data Model(s).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_id
|
DataModel | Sequence[DataModel]
|
The ID(s) of the data model(s) used to create a tailored SDK. You can also pass in the data model(s) directly to avoid fetching them from CDF. |
required |
client
|
Optional[CogniteClient]
|
The cognite client used for fetching the data model. This is required if you pass in
data models ID(s) in the |
None
|
top_level_package
|
Optional[str]
|
The name of the top level package for the SDK. For example,
if we have top_level_package= |
None
|
client_name
|
Optional[str]
|
The name of the client class. For example, |
None
|
default_instance_space
|
str | None
|
The default instance space to use for the generated SDK. If not provided, the space must be specified when creating, deleting, and retrieving nodes and edges. |
None
|
output_dir
|
Path
|
The location to output the generated SDK wheel. Defaults to "dist". |
Path('dist')
|
format_code
|
bool
|
Whether to format the generated code using black. Defaults to True. |
True
|
config
|
Optional[PygenConfig]
|
The configuration used to control how to generate the SDK. |
None
|
Source code in cognite/pygen/_build.py
generate_sdk(model_id, client=None, top_level_package=None, client_name=None, default_instance_space=None, output_dir=None, logger=None, overwrite=False, format_code=False, config=None, return_sdk_files=False)
generate_sdk(model_id: DataModel | Sequence[DataModel], client: Optional[CogniteClient] = None, top_level_package: Optional[str] = None, client_name: Optional[str] = None, default_instance_space: str | None = None, output_dir: Optional[Path] = None, logger: Optional[Callable[[str], None]] = None, overwrite: bool = False, format_code: bool = False, config: Optional[PygenConfig] = None, return_sdk_files: Literal[False] = False) -> None
generate_sdk(model_id: DataModel | Sequence[DataModel], client: Optional[CogniteClient] = None, top_level_package: Optional[str] = None, client_name: Optional[str] = None, default_instance_space: str | None = None, output_dir: Optional[Path] = None, logger: Optional[Callable[[str], None]] = None, overwrite: bool = False, format_code: bool = False, config: Optional[PygenConfig] = None, return_sdk_files: Literal[True] = False) -> dict[Path, str]
Generates a Python SDK tailored to the given Data Model(s).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_id
|
DataModel | Sequence[DataModel]
|
The ID(s) of the data model(s) used to create a tailored SDK. You can also pass in the data model(s) directly to avoid fetching them from CDF. |
required |
client
|
Optional[CogniteClient]
|
The cognite client used for fetching the data model. This is required if you pass in
data models ID(s) in the |
None
|
top_level_package
|
Optional[str]
|
The name of the top level package for the SDK. For example,
if we have top_level_package= |
None
|
client_name
|
Optional[str]
|
The name of the client class. For example, |
None
|
default_instance_space
|
str | None
|
The default instance space to use for the generated SDK. If not provided, the space must be specified when creating, deleting, and retrieving nodes and edges. |
None
|
output_dir
|
Optional[Path]
|
The location to output the generated SDK. Defaults: Path.cwd() / Path(top_level_package.replace(".", "/")). |
None
|
logger
|
Optional[Callable[[str], None]]
|
A logger function to log progress. Defaults to print. |
None
|
overwrite
|
bool
|
Whether to overwrite the output directory if it already exists. Defaults to False. |
False
|
format_code
|
bool
|
Whether to format the generated code using black. Defaults to False. |
False
|
config
|
Optional[PygenConfig]
|
The configuration used to control how to generate the SDK. |
None
|
return_sdk_files
|
bool
|
Whether to return the generated SDK files as a dictionary. Defaults to False. This is useful for granular control of how to write the SDK to disk. |
False
|
Source code in cognite/pygen/_generator.py
generate_sdk_notebook(model_id, client=None, top_level_package=None, client_name=None, default_instance_space=None, config=None, clean_pygen_temp_dir=True)
Generates a Python SDK tailored to the given Data Model(s) and imports it into the current Python session.
This function is wrapper around generate_sdk. It is intended to be used in a Jupyter notebook. The differences are that it:
- The SDK is generated in a temporary directory and added to the sys.path. This is such that it becomes available to be imported in the current Python session.
- The signature is simplified.
- An instantiated client of the generated SDK is returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_id
|
DataModel | Sequence[DataModel]
|
The ID(s) of the data model(s) used to create a tailored SDK. You can also pass in the data model(s) directly to avoid fetching them from CDF. |
required |
client
|
Optional[CogniteClient]
|
The cognite client used for fetching the data model. This is required if you pass in
data models ID(s) in the |
None
|
top_level_package
|
Optional[str]
|
The name of the top level package for the SDK. For example,
if we have top_level_package= |
None
|
client_name
|
Optional[str]
|
The name of the client class. For example, |
None
|
default_instance_space
|
str | None
|
The default instance space to use for the generated SDK. If not provided, the space must be specified when creating, deleting, and retrieving nodes and edges. |
None
|
config
|
Optional[PygenConfig]
|
The configuration used to control how to generate the SDK. |
None
|
clean_pygen_temp_dir
|
bool
|
Whether to clean the temporary directory used to store the generated SDK. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
Any
|
The instantiated generated client class. |
Source code in cognite/pygen/_generator.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
|
load_cognite_client_from_toml(toml_file='config.toml', section='cognite')
This is a small helper function to load a CogniteClient from a toml file.
The default name of the config file is "config.toml" and it should look like this:
[cognite]
project = "<cdf-project>"
tenant_id = "<tenant-id>"
cdf_cluster = "<cdf-cluster>"
client_id = "<client-id>"
client_secret = "<client-secret>"
Parameters:
Name | Type | Description | Default |
---|---|---|---|
toml_file
|
Path | str
|
Path to toml file |
'config.toml'
|
section
|
str | None
|
Name of the section in the toml file to use. If None, use the top level of the toml file. Defaults to "cognite". |
'cognite'
|
Returns:
Type | Description |
---|---|
CogniteClient
|
A CogniteClient with configurations from the toml file. |