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. 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=True, 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 = True, 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 = True, 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 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. 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
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 |
|
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. |