Data Types
The Model Generator API provides a comprehensive type system for defining the data types of fields and named values.
DataType Enum
The DataType enumeration defines primitive data types and their array counterparts.
Primitive Types:
Type |
Description |
|---|---|
|
Whole numbers |
|
Floating-point numbers |
|
Text values |
|
True/false values |
|
Date values (year, month, day) |
|
Date and time values |
|
Time values (hour, minute, second) |
Array Types:
Each primitive type has a corresponding array type: INTEGER_ARRAY, DECIMAL_ARRAY, STRING_ARRAY, BOOLEAN_ARRAY, DATE_ARRAY, DATETIME_ARRAY, TIME_ARRAY.
Example:
from daitum_model import DataType
# Using primitive types
products.add_data_field("product_id", DataType.INTEGER)
products.add_data_field("name", DataType.STRING)
products.add_data_field("price", DataType.DECIMAL)
products.add_data_field("in_stock", DataType.BOOLEAN)
products.add_data_field("created_date", DataType.DATE)
# Using array types
products.add_data_field("tags", DataType.STRING_ARRAY)
products.add_data_field("monthly_sales", DataType.INTEGER_ARRAY)
Type Conversion Methods:
# Convert to array type
int_array = DataType.INTEGER.to_array() # Returns DataType.INTEGER_ARRAY
# Convert from array type
int_type = DataType.INTEGER_ARRAY.from_array() # Returns DataType.INTEGER
# Check if array type
is_array = DataType.STRING_ARRAY.is_array() # Returns True
- class DataType(*values)[source]
Bases:
EnumEnumeration representing various data types.
This enumeration is used to specify different data types that can be used in calculations, parameters, or tables within a model.
- INTEGER = 'INTEGER'
Integer data type.
- DECIMAL = 'DECIMAL'
Decimal data type.
- STRING = 'STRING'
String data type.
- BOOLEAN = 'BOOLEAN'
Boolean data type.
- DATE = 'DATE'
Date data type.
- DATETIME = 'DATETIME'
Datetime data type.
- TIME = 'TIME'
Time data type.
- INTEGER_ARRAY = 'INTEGER_ARRAY'
Array of INTEGER data type.
- DECIMAL_ARRAY = 'DECIMAL_ARRAY'
Array of DECIMAL data type.
- STRING_ARRAY = 'STRING_ARRAY'
Array of STRING data type.
- BOOLEAN_ARRAY = 'BOOLEAN_ARRAY'
Array of BOOLEAN data type.
- DATE_ARRAY = 'DATE_ARRAY'
Array of DATE data type.
- DATETIME_ARRAY = 'DATETIME_ARRAY'
Array of DATETIME data type.
- TIME_ARRAY = 'TIME_ARRAY'
Array of TIME data type.
- NULL = 'NULL'
Used to identify BLANK() formulae. Should never be used explicitly in models
ObjectDataType
The ObjectDataType represents references to other tables, enabling relationships between tables.
Key features:
References another table
Can be singular or array
Enables table relationships and lookups
Example:
from daitum_model import ObjectDataType
# Create tables
products = model.add_data_table("products", id_field="product_id")
orders = model.add_data_table("orders", id_field="order_id")
# Add a field that references the products table
orders.add_data_field(
"product_ref",
ObjectDataType(products)
)
# Add a field that references multiple products (array)
orders.add_data_field(
"related_products",
ObjectDataType(products, is_array=True)
)
# Access fields from referenced table in formulas
orders.add_calculated_field(
"product_name",
DataType.STRING,
formula=orders["product_ref"]["name"]
)
- class ObjectDataType(source_table, is_array=False)[source]
Bases:
BuildableA composite data type used for references to other Tables.
- __init__(source_table, is_array=False)[source]
- Parameters:
source_table (
_TableBase) – The table this data type references.is_array (
bool) – Whether this is anOBJECT_ARRAYtype rather than a singularOBJECT.
- is_array()[source]
Checks if the data type is an array.
- Returns:
True if the data type is an array, otherwise False.
- Return type:
- to_array()[source]
Converts an object type to an object array type.
- Returns:
The array equivalent of the data type.
- Return type:
- Raises:
ValueError – If the data type is already an array.
- from_array()[source]
Converts an object array type to a singular object type.
- Returns:
The singular (non-array) equivalent of the data type.
- Return type:
- Raises:
ValueError – If the data type is not an array.
MapDataType
The MapDataType represents key-value mappings where keys reference records in another table.
Key features:
Maps table records to primitive values
Useful for lookups and configurations
Cannot be nested (values must be primitive types)
Example:
from daitum_model import MapDataType, DataType
products = model.add_data_table("products", id_field="product_id")
warehouses = model.add_data_table("warehouses", id_field="warehouse_id")
# Add a map field: warehouse -> stock quantity
products.add_data_field(
"stock_by_warehouse",
MapDataType(DataType.INTEGER, warehouses)
)
# Map from products to prices
orders.add_data_field(
"product_prices",
MapDataType(DataType.DECIMAL, products)
)
Restrictions:
Value type must be a primitive
DataType(not an array)Cannot nest maps or objects as values
- class MapDataType(data_type, source_table)[source]
Bases:
BuildableA composite data type used to represent key-value maps into other Tables.
- Raises:
TypeError – If the data type is an array type.
- property data_type: DataType
The primitive data type of the map values.
- Returns:
The primitive data type.
- Return type:
- is_array()[source]
Always False. Used to simplify array checks without having to check if a data type is a MapDataType
- Returns:
False
- Return type:
- to_array()[source]
Always raises. Mirrors
from_array()so callers can treatMapDataTypeuniformly withDataTypeandObjectDataTypewithout isinstance checks.- Raises:
ValueError – Always — map data types cannot be arrays.
- Return type:
- from_array()[source]
Always raises an error. Used to simplify array checks without having to check if a data type is a MapDataType.
- Returns:
The singular (non-array) equivalent of the data type.
- Return type:
- Raises:
ValueError – Always, as map data types cannot be an array.