Pygen
cognite.pygen
This is the main entry point for the pygen package. It contains the main functions for generating SDKs.
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. Defaults to the instance space of the first data model given. |
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, pydantic_version='infer', overwrite=False, format_code=True, config=None, return_sdk_files=False)
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. Defaults to the instance space of the first data model given. |
None
|
output_dir |
Optional[Path]
|
The location to output the generated SDK. Defaults to the current working directory. |
None
|
logger |
Optional[Callable[[str], None]]
|
A logger function to log progress. Defaults to print. |
None
|
pydantic_version |
Literal['v1', 'v2', 'infer']
|
The version of pydantic to use. Defaults to "infer" which will use the environment to detect the installed version of pydantic. |
'infer'
|
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 True. |
True
|
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. Defaults to the instance space of the first data model given. |
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
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 |
|
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. |