Core API Reference
This section documents the da_vinci (core runtime) package API.
ORM Module
Table Objects
- class da_vinci.core.orm.table_object.TableObjectAttributeType(*values)[source]
Bases:
StrEnum- STRING = 'string'
- NUMBER = 'number'
- BOOLEAN = 'boolean'
- DATETIME = 'datetime'
- JSON = 'json'
- JSON_STRING = 'json_string'
- STRING_LIST = 'string_list'
- NUMBER_LIST = 'number_list'
- JSON_LIST = 'json_list'
- JSON_STRING_LIST = 'json_string_list'
- COMPOSITE_STRING = 'composite_string'
- STRING_SET = 'string_set'
- NUMBER_SET = 'number_set'
- class da_vinci.core.orm.table_object.TableObjectAttribute(name, attribute_type, argument_names=None, custom_exporter=None, custom_importer=None, description=None, dynamodb_key_name=None, default=None, exclude_from_dict=False, exclude_from_schema_description=False, is_indexed=True, optional=False)[source]
Bases:
object- __init__(name, attribute_type, argument_names=None, custom_exporter=None, custom_importer=None, description=None, dynamodb_key_name=None, default=None, exclude_from_dict=False, exclude_from_schema_description=False, is_indexed=True, optional=False)[source]
Object representing an attribute of a TableObject
- Keyword Arguments:
attribute (attribute_type -- Type of the)
attribute
composite (argument_names -- The names of Python arguments that are merged into a) – string. This is required when the attribute type is COMPOSITE_STRING.
DynamoDB (custom_importer -- Custom importer function, called whenever data is loaded from)
DynamoDB
primarily (description -- Description of the attribute, annotation) – used for LLM context.
based (dynamodb_key_name -- Name of the DynamoDB key, defaults to the ORM naming convention) – on the attribute name
set. (default -- Default value for the attribute, attribute is considered optional when this is) – Accepts a value or a callable that returns a value.
to_dict() (exclude_from_dict -- Attribute is not added to a resulting Dict when calling)
description (exclude_from_schema_description -- Attribute is not included in the table object schema)
True (is_indexed -- Whether the attribute is able to be used to query with, defaults to)
provided (optional -- Whether the attribute optional, defaults to False unless a default is)
- static composite_string_value(values)[source]
Return a full composite string value given a list of attribute values
- Keyword Arguments:
value (values -- List of values to join into the full composite string)
- Return type:
- static default_dynamodb_key_name(name)[source]
Convert a name to a DynamoDB key name
- Keyword Arguments:
convert (name -- Name to)
- Return type:
- Returns:
str
- static timestamp_to_datetime(timestamp)[source]
Convert a timestamp string to a datetime
- Keyword Arguments:
string (timestamp -- Timestamp)
- Return type:
- Returns:
datetime
- static datetime_to_timestamp(dt)[source]
Convert a datetime to a timestamp
- Keyword Arguments:
Datetime (dt --)
- Return type:
- Returns:
float
- dynamodb_value(value)[source]
Convert a value to a DynamoDB supported value
- Keyword Arguments:
convert (value -- Value to)
- Return type:
- Returns:
Any
- class da_vinci.core.orm.table_object.TableObject(**kwargs)[source]
Bases:
objectBase class for Table object definitions
- Class Attributes:
attribute_lookup_prefix: Attribute lookup prefix, prefixes the attribute name when retrieving attributes attributes: List of attributes description: Description of the table object_name: Name of the object, defaults to the class name. This should be set when
dynamically defining table objects.
partition_key_attribute: Partition key attribute sort_key_attribute: Sort key attribute table_name: Name of the table ttl_attribute: Optional TTL attribute
Example
``` from uuid import uuid4
- from da_vinci.core.orm.table_object import (
TableObject, TableObjectAttribute, TableObjectAttributeType,
)
- class MyTableObject(TableObject):
- partition_key_attribute = TableObjectAttribute(
name=’my_pk’, attribute_type=TableObjectAttributeType.STRING,
)
- sort_key_attribute = TableObjectAttribute(
name=’my_sk’, attribute_type=TableObjectAttributeType.STRING, default=lambda: str(uuid4()),
)
table_name = ‘my_table’
- attributes = [
- TableObjectAttribute(
name=’created_on’, attribute_type=TableObjectAttributeType.DATETIME, default=lambda: datetime.now(),
),
]
-
partition_key_attribute:
TableObjectAttribute
-
attributes:
list[TableObjectAttribute] = []
-
sort_key_attribute:
TableObjectAttribute|None= None
-
ttl_attribute:
TableObjectAttribute|None= None
- classmethod define(partition_key_attribute, object_name, table_name, attribute_lookup_prefix=None, attributes=None, description=None, sort_key_attribute=None, ttl_attribute=None)[source]
Define a TableObject
- Keyword Arguments:
attribute_lookup_prefix – Attribute lookup prefix
attributes – List of attributes
description – Description of the table
partition_key_attribute – Partition key attribute
object_name – Name of the object
sort_key_attribute – Sort key attribute
table_name – Name of the table
ttl_attribute – Optional TTL attribute
- Return type:
- __getattr__(name)[source]
Get an attribute by name
- Keyword Arguments:
attribute (name -- Name of the)
- Return type:
- Returns:
Any
- attribute_value(name)[source]
Get the value of an attribute
Keyword arguments: name – Name of the attribute
- Return type:
- composite_attribute_values(name)[source]
Get a dictionary representation of the composite attribute’s values
Keyword arguments: name – Name of the attribute
- Return type:
- execute_on_update()[source]
Execute the on update function
Override this method to provide custom behavior when the object is saved to DynamoDB
- Return type:
- update(**kwargs)[source]
Update the attributes of the object and provide a list of attribute names that were updated.
Keyword arguments: kwargs – Attributes to update
- to_dict(exclude_attribute_names=None, json_compatible=False)[source]
Convert the object to a dict representation
Keyword Arguments: exclude_attribute_names – List of attribute names to exclude from the resulting dict json_compatible – Convert datetime objects to strings and sets to lists for JSON compatibility
- Return type:
- classmethod all_attributes()[source]
Class method that returns all defined attributes on the class
- Return type:
- classmethod attribute_definition(name)[source]
Get an attribute definition by name
- Keyword Arguments:
attribute (name -- Name of the)
- Return type:
- Returns:
TableObjectAttribute
- classmethod from_dynamodb_item(item)[source]
Create a TableObject from a DynamoDB item
- Keyword Arguments:
convert (item -- Item to)
- Return type:
- Returns:
TableObject
- classmethod gen_dynamodb_key(partition_key_value, sort_key_value=None)[source]
Generate a DynamoDB key
- Keyword Arguments:
value (sort_key_value -- Sort key)
value
- Return type:
- Returns:
Dict
- classmethod schema_description()[source]
Get the schema for the object in a human readable format
- Return type:
- Returns:
str
- classmethod schema_to_str(only_indexed_attributes=True)[source]
Describe the full schema for the object
- Keyword Arguments:
attributes (only_indexed_attributes -- Only describe indexed)
- Return type:
- Returns:
str
- static update_date_attributes(date_attribute_names, obj, to_datetime=None)[source]
Update the record_last_updated attribute on the object. Helper method that is commonly used to construct execute_on_update functions.
- Keyword Arguments:
attributes (date_attribute_names -- Names of the date)
update (obj -- Object to)
datetime.now() (to_datetime -- Datetime to set, defaults to)
- Return type:
Table Client
- class da_vinci.core.orm.client.TableResultSortOrder(*values)[source]
Bases:
StrEnumSort order for table query results
- ASCENDING = 'ascending'
- DESCENDING = 'descending'
- class da_vinci.core.orm.client.PaginatorCall[source]
Bases:
objectPaginator call types for DynamoDB operations
- QUERY = 'query'
- SCAN = 'scan'
- class da_vinci.core.orm.client.PaginatedResults(items, last_evaluated_key=None)[source]
Bases:
objectContainer for paginated DynamoDB query results
Keyword Arguments: items – List of table objects returned from the query last_evaluated_key – Key to use for fetching the next page (optional)
- class da_vinci.core.orm.client.TableScanDefinition(table_object_class, attribute_prefix=None)[source]
Bases:
objectDefine filters for scanning a DynamoDB table
Provides a builder pattern for constructing filter expressions for DynamoDB scan and query operations. Supports various comparison operators and handles attribute type conversions.
- __init__(table_object_class, attribute_prefix=None)[source]
Create a new scan definition for a DynamoDB table
- Keyword Arguments:
table_object_class – Table object class to scan
attribute_prefix – Prefix to use for attribute names (default: None)
- add(attribute_name, comparison, value)[source]
Add an attribute filter to the scan definition
- Keyword Arguments:
attribute_name – Name of the attribute to filter on
comparison – Comparison operator to use (ex: equal or greater_than)
value – Value to compare against
- Return type:
- __getattr__(attr)[source]
Override to dynamically add attribute filters to the scan definition using comparison names as method names.
Example: scan_definition.equal(‘foo’, ‘bar’) will add a filter for the attribute foo equal to bar
- Keyword Arguments:
attr – Name of the attribute to filter on
- Return type:
- class da_vinci.core.orm.client.TableClient(default_object_class, app_name=None, deployment_id=None, table_endpoint_name=None, resource_discovery_storage_solution=None)[source]
Bases:
objectClient for interacting with DynamoDB tables using Da Vinci table objects
Provides high-level operations for querying, scanning, and managing DynamoDB tables with automatic resource discovery and pagination support.
- __init__(default_object_class, app_name=None, deployment_id=None, table_endpoint_name=None, resource_discovery_storage_solution=None)[source]
Initialize the table client with resource discovery
Keyword Arguments: default_object_class – TableObject subclass defining the table schema app_name – Application name for resource discovery (optional) deployment_id – Deployment identifier for resource discovery (optional) table_endpoint_name – Explicit table name (optional, will be discovered if not provided) resource_discovery_storage_solution – Storage solution for resource discovery (optional)
- classmethod table_resource_exists(table_object_class, app_name=None, deployment_id=None)[source]
Check if a DaVinci based DynamoDB table exists
- paginated(call='query', last_evaluated_key=None, last_evaluated_object=None, limit=None, max_pages=None, parameters=None, sort_order=TableResultSortOrder.ASCENDING)[source]
Handle paginated DynamoDB table results. The last item in a page should be the last evaluated item.
- Keyword Arguments:
call – Name of the DynamoDB client method to call, either a scan or query (default: query)
last_evaluated_key – Last evaluated key from a previous page of results (default: None)
last_evaluated_object – Last evaluated object from a previous page of results (default: None), only supported for query
limit – Maximum number of items to retrieve per page (default: None)
max_pages – Maximum number of pages to retrieve, if None it will return all available (default: None)
parameters – Parameters to pass to the client method
sort_order – Sort order to use for the results, only works for query calls (default: ASCENDING)
- Return type:
- get_object(partition_key_value, sort_key_value=None, consistent_read=False)[source]
Retrieve a single object from the table by partition and sort key
- Keyword Arguments:
partition_key_value – Value of the partition key
sort_key_value – Value of the sort key (default: None)
consistent_read – Whether to use consistent read (default: False)
- Return type:
- put_object(table_object)[source]
Save a single object to the table
- Keyword Arguments:
table_object – Object to save
- Return type:
- delete_object_by_key(partition_key_value, sort_key_value=None)[source]
Delete a single object from the table by partition and sort key
- Keyword Arguments:
partition_key_value – Value of the partition key
sort_key_value – Value of the sort key (default: None)
- Return type:
- delete_object(table_object)[source]
Delete a single object from the table
- Keyword Arguments:
table_object – Object to remove
- Return type:
- scanner(scan_definition)[source]
Perform a scan on the table, works similar to the paginator.
- Keyword Arguments:
scan_definition – Scan definition to use (default: None)
- Return type:
- full_scan(scan_definition)[source]
Perform a full scan on the table, returns all items matching the scan definition at once.
- Keyword Arguments:
scan_definition – Scan definition to use (default: None)
- Return type:
- update_object(partition_key_value, sort_key_value, updates=None, remove_keys=None)[source]
Updates an item in the DynamoDB table by applying SET and REMOVE operations.
This method allows partial updates to items by setting new values for attributes or removing existing ones. It supports dot notation for nested JSON map updates, enabling the modification of specific keys within a JSON-like structure in DynamoDB.
- Parameters:
partition_key_value (Any) – The value of the partition key for the item to be updated.
sort_key_value (Any) – The value of the sort key for the item to be updated.
updates (Dict[str, Any], optional) – A dictionary containing attribute names (as keys) and their new values (as values) to be updated in the table. If dot notation is used in the attribute name (e.g., ‘json_map.sub_key’), it will update a nested key within a DynamoDB MAP type.
remove_keys (List[str], optional) – A list of attribute names to be removed from the item. Dot notation can be used to remove nested attributes from a DynamoDB MAP.
- Example Usage:
- To update a nested key inside a JSON map:
updates = {‘json_map.sub_key’: ‘new_value’} remove_keys = [‘json_map.another_sub_key’]
- To update a top-level attribute and remove another:
updates = {‘attribute1’: ‘new_value’} remove_keys = [‘attribute2’]
Notes
This method generates DynamoDB UpdateExpressions to execute the SET and REMOVE
operations in a single request. - If both updates and remove_keys are provided, they are combined in the final update expression. - Dot notation in updates or remove_keys will handle nested attributes within DynamoDB MAP types. - This method assumes the object’s table schema is already defined in the default_object_class.
ORM Exceptions
Custom Exceptions for the ORM
- exception da_vinci.core.orm.orm_exceptions.AugmentedRetrievalInvalidQueryError(query, details)[source]
Bases:
ValueError
- exception da_vinci.core.orm.orm_exceptions.MissingTableObjectAttributeError(attribute_name)[source]
Bases:
ValueError
- exception da_vinci.core.orm.orm_exceptions.TableScanQueryError(attribute_name, attribute_type)[source]
Bases:
ValueError
- exception da_vinci.core.orm.orm_exceptions.TableScanInvalidComparisonError(comparison_name)[source]
Bases:
TableScanQueryError
- exception da_vinci.core.orm.orm_exceptions.TableScanInvalidAttributeError(attribute_name)[source]
Bases:
TableScanQueryError
Core Module
Exceptions
All Da Vinci Base Exceptions
- exception da_vinci.core.exceptions.DuplicateRouteDefinitionError(route_name)[source]
Bases:
Exception
- exception da_vinci.core.exceptions.GlobalSettingNotFoundError(setting_key, namespace)[source]
Bases:
Exception
- exception da_vinci.core.exceptions.MissingRequiredRuntimeVariableError(variable_name)[source]
Bases:
RuntimeError
Resource Discovery
Resource Discovery Module
- class da_vinci.core.resource_discovery.ResourceType(*values)[source]
Bases:
StrEnumResource types registered with service discovery
- ASYNC_SERVICE = 'async_service'
- BUCKET = 'bucket'
- DOMAIN = 'domain'
- REST_SERVICE = 'rest_service'
- TABLE = 'table'
- class da_vinci.core.resource_discovery.ResourceDiscoveryStorageSolution(*values)[source]
Bases:
StrEnumResource discovery storage solutions
- DYNAMODB = 'dynamodb'
- SSM = 'ssm'
- class da_vinci.core.resource_discovery.ResourceDiscovery(resource_type, resource_name, app_name=None, deployment_id=None, storage_solution=None)[source]
Bases:
object- __init__(resource_type, resource_name, app_name=None, deployment_id=None, storage_solution=None)[source]
Initialize a ResourceDiscovery object
- Keyword Arguments:
resource_type – Type of the resource
resource_name – Name of the resource
app_name – Name of the application (default: None)
deployment_id – Unique identifier for the installation (default: None)
- classmethod ssm_parameter_name(resource_type, resource_name, app_name, deployment_id)[source]
Return the parameter for a resource registered with service discovery.
- Keyword Arguments:
resource_type – Type of the resource
resource_name – Name of the resource
app_name – Name of the application
deployment_id – Unique identifier for the installation
- Return type:
- endpoint_lookup()[source]
Return the endpoint for a resource registered with service discovery.
- Return type:
- service_registry_table_lookup()[source]
Return the endpoint for a resource registered with service discovery using the service registry table.
- Return type:
- ssm_resource_endpoint_lookup()[source]
Return the endpoint for a resource registered with service discovery. The app_name and deployment_id parameters are optional. If not provided, the values will be loaded from the environment.
- Keyword Arguments:
resource_type – Type of the resource
resource_name – Name of the resource
app_name – Name of the application (default: None)
deployment_id – Unique identifier for the installation (default: None)
- Return type:
Global Settings
Logging
- class da_vinci.core.logging.S3LogHandler(execution_id, namespace, metadata=None)[source]
Bases:
HandlerCustom log handler to store logs in memory and offload them to S3.
- __init__(execution_id, namespace, metadata=None)[source]
Set up the S3 log handler with the execution ID and namespace.
Keyword Arguments: execution_id – The execution ID namespace – The namespace for the logs metadata – Additional metadata to store with the logs
- emit(record)[source]
Capture log records and store them in memory.
Keyword Arguments: record – The log record
- Return type:
- class da_vinci.core.logging.Logger(namespace, log_level_name=None, s3_logging_enabled=False, s3_logging_bucket_name=None)[source]
Bases:
object- __init__(namespace, log_level_name=None, s3_logging_enabled=False, s3_logging_bucket_name=None)[source]
Initialize the logger with the namespace and log level name.
Keyword Arguments: namespace – The namespace for the logger log_level_name – The log level name (default: INFO) s3_logging_enabled – Enable logging to S3 (default: False) s3_logging_bucket_name – The S3 bucket name for logging (default: None)
- finalize()[source]
Call this method to finalize logging and upload logs to S3 if enabled.
- Return type:
- debug(message)[source]
Add a log message with level DEBUG.
Keyword Arguments: message – The log message
- Return type:
- info(message)[source]
Add a log message with level INFO.
Keyword Arguments: message – The log message
- Return type:
JSON Utilities
JSON Utility functionality
- class da_vinci.core.json.DateTimeEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]
Bases:
JSONEncoderJSONEncoder class that encodes datetime objects strings
- class da_vinci.core.json.DaVinciObjectEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]
Bases:
JSONEncoderJSONEncoder class that encodes commonly used framework objects
- default(obj)[source]
Encode Da Vinci framework objects and datetime objects
Handles ObjectBody instances by converting to dictionaries and datetime objects by converting to ISO format strings.
Keyword Arguments: obj – Object to encode
- Raises:
TypeError -- If object is not JSON serializable –
- Return type: