ExternalID Factories
cognite.pygen.utils.external_id_factories
This module contains a configurable external id factory that can be used to create external ids for each domain class.
This is useful when you want to use pygen for ingesting data into CDF from JSON or another format that does not have external ids. The external id factories can be set on the DomainModelWrite class and will be called when creating instances of the DomainModelWrite class.
It is, however, recommended that you create your own external ids for each domain class, as this will make it easier
to create external ids which are unique and meaningful. This can be done by creating a custom function with the
following signature def custom_factory(domain_cls: type, data: dict) -> str:
or using the ExternalIdFactory class.
For example, if you are ingesting windmills, you could create an external id based on an existing windmill field and write a custom factory function utilizing that field:
Example
import uuid as uuid_
from cognite.pygen.utils.external_id_factories import ExternalIdFactory
from windmill.client.data_classes import DomainModelWrite, WindmillWrite, RotorWrite
fallback_factory = ExternalIdFactory.uuid_factory()
def custom_factory_by_domain_class(domain_cls: type, data: dict) -> str:
if domain_cls is WindmillWrite and "name" in data:
return data["name"]
elif domain_cls is RotorWrite and "name" in data:
return data["name"]
else:
# Fallback to uuid factory
return fallback_factory(domain_cls, data)
# Finally, we can configure the new factory on the DomainModelWrite class a few different ways:
# Using the custom factory directly:
DomainModelWrite.external_id_factory = ExternalIdFactory(custom_factory_by_domain_class)
# Example input and output:
windmill_with_name = WindmillWrite(name="Oslo 1")
assert windmill_with_name.external_id == "Oslo 1"
rotor_no_name = RotorWrite(rotor_speed_controller="time_series_external_id")
assert uuid_.UUID(rotor_no_name.external_id)
windmill_with_external_id = WindmillWrite(external_id="custom_external_id")
assert uuid_.UUID(windmill_with_external_id.external_id)
# Using the create_external_id_factory function to have a prefix and suffix combination:
DomainModelWrite.external_id_factory = ExternalIdFactory.create_external_id_factory(
override_external_id=False,
separator="_",
prefix_ext_id_factory=ExternalIdFactory.domain_name_factory(),
suffix_ext_id_factory=ExternalIdFactory(custom_factory_by_domain_class),
)
# Example input and output:
windmill_with_name = WindmillWrite(name="Oslo 1")
assert windmill_with_name.external_id == "windmill_Oslo 1"
rotor_no_name = RotorWrite(rotor_speed_controller="time_series_external_id")
assert rotor_no_name.external_id.startswith("rotor_")
assert uuid_.UUID(rotor_no_name.external_id.removeprefix("rotor_"))
windmill_with_external_id = WindmillWrite(external_id="custom_external_id")
assert windmill_with_external_id.external_id == "custom_external_id"
The example above has created the SDK for the windmill data with the following configuration:
ExternalIdFactory
This is a factory class that can be used to create external ids for domain class instances.
Source code in cognite/pygen/utils/external_id_factories.py
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 |
|
create_external_id_factory(override_external_id=True, separator=':', prefix_ext_id_factory=None, suffix_ext_id_factory=None)
classmethod
This creates an external id factory with a prefix:suffix format that can be set on the DomainModelWrite class provided a custom configuration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
override_external_id
|
bool
|
If True, any existing external_id in the data will always be overridden. |
True
|
separator
|
str
|
The separator between the prefix and suffix, should be a single character. |
':'
|
prefix_ext_id_factory
|
Optional[Callable[[type, dict], str]]
|
The function to create the prefix, defaults to the domain_name_factory. |
None
|
suffix_ext_id_factory
|
Optional[Callable[[type, dict], str]]
|
The function to create the suffix, defaults to the uuid_factory. |
None
|
Returns:
Type | Description |
---|---|
Callable[[type, dict], str]
|
A factory function that can be set on the DomainModelWrite class. |
Source code in cognite/pygen/utils/external_id_factories.py
domain_name_factory()
classmethod
This creates a domain name external id factory instance of the ExternalIdFactory class.
external_id_generator(domain_cls, data, override_external_id=True, separator=':', prefix_callable=None, suffix_callable=None)
staticmethod
This creates an external id for each domain class instance provided a custom configuration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
domain_cls
|
type
|
The domain class |
required |
data
|
dict
|
The data for the domain class instance |
required |
override_external_id
|
bool
|
If True, any existing external_id in the data will be overridden by the factory. |
True
|
separator
|
str
|
The separator between the prefix and suffix. |
':'
|
prefix_callable
|
Optional[Callable[[type, dict], str]]
|
The function to create the prefix, defaults to the domain_name_factory. |
None
|
suffix_callable
|
Optional[Callable[[type, dict], str]]
|
The function to create the suffix, defaults to the uuid_factory. |
None
|
Returns:
Type | Description |
---|---|
str
|
An external id for the domain class instance given the configuration following the format |
Source code in cognite/pygen/utils/external_id_factories.py
incremental_factory()
classmethod
This creates an incremental external id factory instance of the ExternalIdFactory class.
Note
warning: This is not recommended for production use due to potential conflicts in threads and external services potentially using the same incremental indexing.
Source code in cognite/pygen/utils/external_id_factories.py
sha256_factory()
classmethod
This creates a sha256 hash external id factory instance of the ExternalIdFactory class.
uuid_factory()
classmethod
This creates a uuid external id factory instance of the ExternalIdFactory class.
create_incremental_factory()
This creates a factory that will create incremental external ids for each domain class.
Note
deprecation warning: This function is deprecated and will be removed in v1.0. Use ExternalIdFactory.create_external_id_factory( suffix_ext_id_factory=ExternalIdFactory.incremental_factory() ) instead. warning: This is not recommended for production use due to potential conflicts in threads and external services potentially using the same incremental indexing.
Returns:
Type | Description |
---|---|
Callable[[type, dict], str]
|
A factory function that can be set on the DomainModelApply class. |
Source code in cognite/pygen/utils/external_id_factories.py
create_sha256_factory(shorten=True)
This creates a sha256 hash external id factory.
Note
deprecation warning: This function is deprecated and will be removed in v1.0. Use ExternalIdFactory.create_external_id_factory( suffix_ext_id_factory=ExternalIdFactory.sha256_factory() ) instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
shorten
|
bool
|
If True, the external id will be shortened to 7 characters. |
True
|
Returns:
Type | Description |
---|---|
Callable[[type, dict], str]
|
A factory function that can be set on the DomainModelApply class. |
Source code in cognite/pygen/utils/external_id_factories.py
create_uuid_factory(shorten=True)
This creates a uuid external id factory.
Note
deprecation warning: This function is deprecated and will be removed in v1.0. Use ExternalIdFactory.create_external_id_factory( suffix_ext_id_factory=ExternalIdFactory.uuid_factory() ) instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
shorten
|
bool
|
If True, the external id will be shortened to 7 characters. |
True
|
Returns:
Type | Description |
---|---|
Callable[[type, dict], str]
|
A factory function that can be set on the DomainModelApply class. |
Source code in cognite/pygen/utils/external_id_factories.py
domain_name(domain_cls, data)
This creates an string based on the domain class name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
domain_cls
|
type
|
The domain class |
required |
data
|
dict
|
The data for the domain class instance |
required |
Returns:
Type | Description |
---|---|
str
|
A string based on the domain class name |
Source code in cognite/pygen/utils/external_id_factories.py
incremental_id(domain_cls, data)
This creates an incremental id for each domain class instance.
Note
warning: This is not recommended for production use due to potential conflicts in threads and external services potentially using the same incremental indexing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
domain_cls
|
type
|
The domain class |
required |
data
|
dict
|
The data for the domain class instance |
required |
Returns:
Type | Description |
---|---|
str
|
An incremental integer given the domain class |
Source code in cognite/pygen/utils/external_id_factories.py
sha256(domain_cls, data)
This creates a sha256 hash based on the data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
domain_cls
|
type
|
The domain class |
required |
data
|
dict
|
The data for the domain class instance |
required |
Returns:
Type | Description |
---|---|
str
|
A sha256 hash |
Source code in cognite/pygen/utils/external_id_factories.py
sha256_factory(domain_cls, data)
This creates a sha256 hash external id for each domain class.
Note
deprecation warning: This function is deprecated and will be removed in v1.0. Use ExternalIdFactory.create_external_id_factory( suffix_ext_id_factory=ExternalIdFactory.sha256_factory() ) instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
domain_cls
|
type
|
The domain class |
required |
data
|
dict
|
The data to create the external id from |
required |
Returns:
Type | Description |
---|---|
str
|
A sha256 hash of the data |
Source code in cognite/pygen/utils/external_id_factories.py
shorten_string(input_str, length=7)
This shortens the input string to the specified length.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_str
|
str
|
The string to shorten |
required |
length
|
int
|
The length to shorten the input string to |
7
|
Returns:
Type | Description |
---|---|
str
|
The shortened string |
Source code in cognite/pygen/utils/external_id_factories.py
uuid(domain_cls, data)
This creates a uuid.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
domain_cls
|
type
|
The domain class |
required |
data
|
dict
|
The data for the domain class instance |
required |
Returns:
Type | Description |
---|---|
str
|
A uuid |
Source code in cognite/pygen/utils/external_id_factories.py
uuid_factory(domain_cls, data)
This creates an uuid external id for each domain class.
Note
deprecation warning: This function is deprecated and will be removed in v1.0. Use ExternalIdFactory.create_external_id_factory( suffix_ext_id_factory=ExternalIdFactory.uuid_factory() ) instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
domain_cls
|
type
|
|
required |
data
|
dict
|
|
required |
Returns: