values
values ¶
Module for generating, sorting, and managing named values. This uses pydantic dataclasses for JSON serialization to avoid overloading system memory.
The module provides a robust framework for managing named values with type safety, serialization, and state management. It includes classes for individual named values, collections of named values in both list and hash (dictionary) formats, and utilities for type validation and serialization.
Classes:
Name | Description |
---|---|
NamedValueState |
Enum for tracking the state of named values |
NamedValue |
Base class for type-safe named values with state management |
NamedValueHash |
Dictionary-like container for managing named values |
NamedValueList |
List-like container for managing ordered named values |
Types
SerializableValue: Union type defining all allowed value types T: Generic type variable bound to SerializableValue
NamedValue ¶
NamedValue(name: str, value: T | None = None, **data)
Bases: NamedObject
, Generic[T]
A named value container with type safety and state management.
NamedValue provides a type-safe way to store and manage values with built-in state tracking, serialization, and validation. Values can be frozen after initial setting to prevent accidental modification.
Type Parameters
T: The type of value to store, must be a SerializableValue
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
Unique identifier for the value |
_stored_value |
T | NamedValueState
|
The actual stored value or UNSET state |
_state |
NamedValueState
|
Current state of the value |
_type |
type
|
Runtime type information for validation |
Properties
value (T): Access or modify the stored value
Example
Source code in src/data_handlers/values.py
value
property
writable
¶
Get the stored value.
Returns:
Name | Type | Description |
---|---|---|
T |
T
|
The currently stored value |
Raises:
Type | Description |
---|---|
ValueError
|
If attempting to access before a value has been set |
Note
This property provides read access to the stored value. Once set, the value is frozen and can only be changed using force_set_value().
__setattr__ ¶
Prevent direct modification of protected attributes.
Overrides attribute setting to prevent direct modification of internal state attributes. These attributes should only be modified through appropriate methods.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Name of the attribute to set |
required |
value
|
Any
|
Value to set |
required |
Raises:
Type | Description |
---|---|
AttributeError
|
If attempting to modify protected attributes directly |
Source code in src/data_handlers/values.py
append_to_value_list ¶
append_to_value_list(l: NamedValueList) -> Self
Appends this value instance to a NamedValueList.
Convenience method for adding this value to a list while enabling method chaining.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
l
|
NamedValueList
|
The list to append this value to |
required |
Returns:
Name | Type | Description |
---|---|---|
Self |
Self
|
This instance for method chaining |
Example
Source code in src/data_handlers/values.py
force_set_value ¶
Force set the value regardless of its current state.
This method bypasses the normal freezing mechanism and allows changing an already-set value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new_value
|
T
|
New value to store |
required |
Raises:
Type | Description |
---|---|
TypeError
|
If value doesn't match the expected type T |
Source code in src/data_handlers/values.py
model_dump ¶
Custom serialization to include value state and stored value.
Extends the parent class serialization to include the value's state and stored value (if set) in the serialized data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
Additional arguments passed to parent serialization |
{}
|
Returns:
Type | Description |
---|---|
dict[str, Any]
|
dict[str, Any]: Dictionary containing serialized state |
Example
Source code in src/data_handlers/values.py
model_dump_json ¶
model_dump_json(**kwargs) -> str
Custom JSON serialization of the named value.
Serializes the named value instance to a JSON string, including all state information and stored value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
JSON serialization options like indent, ensure_ascii, etc. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
JSON string representation |
Example
Source code in src/data_handlers/values.py
model_validate
classmethod
¶
model_validate(data: Any) -> NamedValue
Custom deserialization to restore value state and stored value.
Reconstructs a NamedValue instance from serialized data, properly restoring both the value state and any stored value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Any
|
Serialized data to deserialize |
required |
Returns:
Name | Type | Description |
---|---|---|
NamedValue |
NamedValue
|
New instance with restored state |
Example
Source code in src/data_handlers/values.py
model_validate_json
classmethod
¶
model_validate_json(json_data: str, **kwargs) -> NamedValue
Custom JSON deserialization to NamedValue instance.
Reconstructs a NamedValue instance from a JSON string representation, restoring all state and stored value information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_data
|
str
|
JSON string to deserialize |
required |
**kwargs
|
Additional validation options |
{}
|
Returns:
Name | Type | Description |
---|---|---|
NamedValue |
NamedValue
|
New instance with restored state |
Example
Source code in src/data_handlers/values.py
register_to_value_hash ¶
register_to_value_hash(h: NamedValueHash) -> Self
Registers this value instance in a NamedValueHash.
Registers this value in the provided hash container. If the hash contains value overrides, this value's current value may be overridden during registration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
h
|
NamedValueHash
|
The hash to register this value in |
required |
Returns:
Name | Type | Description |
---|---|---|
Self |
Self
|
This instance for method chaining |
Example
Source code in src/data_handlers/values.py
NamedValueHash ¶
Bases: NamedObjectHash
A type-safe dictionary for storing and managing NamedValue objects.
NamedValueHash provides a dictionary-like interface for managing a collection of NamedValue instances, using their names as keys. It ensures type safety and provides convenient methods for accessing and managing the stored values.
The hash maintains unique naming across all stored values and supports serialization/deserialization of the entire collection.
Attributes:
Name | Type | Description |
---|---|---|
_registry_category |
str
|
Category identifier for object registration |
model_config |
ConfigDict
|
Pydantic configuration for model behavior |
Example
get_raw_value ¶
Get the underlying value by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Name of the value to retrieve |
required |
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The actual value stored in the named value |
Raises:
Type | Description |
---|---|
KeyError
|
If no value exists with the given name |
ValueError
|
If the value hasn't been set yet |
Example
Source code in src/data_handlers/values.py
get_raw_values ¶
Get the underlying values of all named values.
Returns:
Type | Description |
---|---|
Iterable[Any]
|
Iterable[Any]: Iterator over the actual values stored in each NamedValue |
Example
Source code in src/data_handlers/values.py
get_value ¶
get_value(name: str) -> NamedValue
Retrieve a named value by its name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Name of the value to retrieve |
required |
Returns:
Name | Type | Description |
---|---|---|
NamedValue |
NamedValue
|
The requested named value |
Raises:
Type | Description |
---|---|
KeyError
|
If no value exists with the given name |
Example
Source code in src/data_handlers/values.py
get_value_by_type ¶
get_value_by_type(value_type: Type) -> Iterable[NamedValue]
Get all values of a specific type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value_type
|
Type
|
Type to filter values by |
required |
Returns:
Type | Description |
---|---|
Iterable[NamedValue]
|
Iterable[NamedValue]: Values matching the specified type |
Example
Source code in src/data_handlers/values.py
get_value_names ¶
Get names of all registered values.
Returns:
Type | Description |
---|---|
Iterable[str]
|
Iterable[str]: An iterator over all value names |
Example
Source code in src/data_handlers/values.py
get_values ¶
get_values() -> Iterable[NamedValue]
Get all registered named values.
Returns:
Type | Description |
---|---|
Iterable[NamedValue]
|
Iterable[NamedValue]: An iterator over all stored named values |
Example
Source code in src/data_handlers/values.py
model_dump ¶
Custom serialization to preserve stored values and their states.
Creates a dictionary representation of the hash that includes full serialization of all contained NamedValue objects, preserving their values and states.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
Additional serialization options passed to all nested objects |
{}
|
Returns:
Type | Description |
---|---|
dict[str, Any]
|
dict[str, Any]: Dictionary containing the complete hash state |
Example
Source code in src/data_handlers/values.py
model_dump_json ¶
model_dump_json(**kwargs) -> str
Custom JSON serialization of the entire hash.
Serializes the NamedValueHash instance and all contained NamedValue objects to a JSON string representation. Handles both the hash structure and the nested value serialization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
JSON serialization options such as: - indent: Number of spaces for pretty printing - ensure_ascii: Escape non-ASCII characters - separators: Tuple of (item_sep, key_sep) for custom formatting |
{}
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
JSON string representation of the hash |
Example
Source code in src/data_handlers/values.py
model_validate
classmethod
¶
model_validate(data: Any) -> NamedValueHash
Custom validation to restore hash state from serialized data.
Reconstructs a NamedValueHash instance from serialized data, including all contained NamedValue objects with their values and states.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Any
|
Serialized data to deserialize. Should be a dictionary containing an 'objects' key with serialized NamedValue instances |
required |
Returns:
Name | Type | Description |
---|---|---|
NamedValueHash |
NamedValueHash
|
New instance with all values restored |
Example
Source code in src/data_handlers/values.py
model_validate_json
classmethod
¶
model_validate_json(json_data: str, **kwargs) -> NamedValueHash
Custom JSON deserialization to NamedValueHash instance.
Reconstructs a NamedValueHash instance from a JSON string representation, including all contained NamedValue objects with their complete state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_data
|
str
|
JSON string containing serialized hash data |
required |
**kwargs
|
Additional validation options for nested objects |
{}
|
Returns:
Name | Type | Description |
---|---|---|
NamedValueHash |
NamedValueHash
|
New instance with all values restored |
Example
Source code in src/data_handlers/values.py
register_value ¶
register_value(value: NamedValue) -> Self
Register a named value in the hash.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
NamedValue
|
The value to register |
required |
Returns:
Name | Type | Description |
---|---|---|
Self |
Self
|
Returns self for method chaining |
Raises:
Type | Description |
---|---|
ValueError
|
If a value with the same name already exists |
Example
Source code in src/data_handlers/values.py
set_raw_value ¶
Set the underlying value for a named value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Name of the value to update |
required |
value
|
Any
|
New value to set |
required |
Raises:
Type | Description |
---|---|
KeyError
|
If no value exists with the given name |
TypeError
|
If value type doesn't match the expected type |
Example
Source code in src/data_handlers/values.py
NamedValueList ¶
Bases: NamedObjectList
An ordered list container for managing NamedValue objects.
NamedValueList maintains an ordered collection of NamedValue objects while providing type safety and convenient access methods. It preserves insertion order while also allowing access by name.
Attributes:
Name | Type | Description |
---|---|---|
_registry_category |
str
|
Category identifier for object registration |
objects |
List[SerializeAsAny[InstanceOf[NamedValue]]]
|
The list of stored values |
Example
__getitem__ ¶
__getitem__(idx: int) -> NamedValue
Get a named value by its index in the list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
idx
|
int
|
Index of the named value to retrieve |
required |
Returns:
Name | Type | Description |
---|---|---|
NamedValue |
NamedValue
|
The named value at the specified index |
Raises:
Type | Description |
---|---|
IndexError
|
If the index is out of range |
Example
Source code in src/data_handlers/values.py
append ¶
append(value: NamedValue) -> Self
Append a named value to the end of the list.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
NamedValue
|
Named value to append |
required |
Returns:
Name | Type | Description |
---|---|---|
Self |
Self
|
The list instance for method chaining |
Example
Source code in src/data_handlers/values.py
extend ¶
extend(values: Iterable[NamedValue]) -> Self
Extend the list with multiple named values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values
|
Iterable[NamedValue]
|
Collection of named values to add |
required |
Returns:
Name | Type | Description |
---|---|---|
Self |
Self
|
The list instance for method chaining |
Example
Source code in src/data_handlers/values.py
get_value ¶
get_value(name: str) -> NamedValue
Get a registered named value by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Name of the value to retrieve |
required |
Returns:
Name | Type | Description |
---|---|---|
NamedValue |
NamedValue
|
The requested named value |
Raises:
Type | Description |
---|---|
KeyError
|
If no value exists with the given name |
Example
Source code in src/data_handlers/values.py
get_value_by_type ¶
get_value_by_type(value_type: Type) -> Iterable[NamedValue]
Get all values whose stored value is of a specific type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value_type
|
Type
|
Type to filter values by |
required |
Returns:
Type | Description |
---|---|
Iterable[NamedValue]
|
Iterable[NamedValue]: Values whose stored value matches the specified type |
Example
Source code in src/data_handlers/values.py
get_values ¶
get_values() -> Iterable[NamedValue]
Get all registered named values.
Returns:
Type | Description |
---|---|
Iterable[NamedValue]
|
Iterable[NamedValue]: Iterator over all stored named values |
Example
Source code in src/data_handlers/values.py
model_dump ¶
Custom serialization to preserve stored values.
Extends the parent class serialization to ensure proper serialization of all stored named values and their states.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
Additional serialization options |
{}
|
Returns:
Type | Description |
---|---|
dict[str, Any]
|
dict[str, Any]: Dictionary containing serialized state |
Source code in src/data_handlers/values.py
model_dump_json ¶
model_dump_json(**kwargs) -> str
Custom JSON serialization of the value list.
Serializes the NamedValueList instance and all contained NamedValue objects to a JSON string representation. Preserves the order of values and their complete state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
JSON serialization options such as: - indent: Number of spaces for pretty printing - ensure_ascii: Escape non-ASCII characters - separators: Tuple of (item_sep, key_sep) for custom formatting |
{}
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
JSON string representation of the list |
Example
Source code in src/data_handlers/values.py
model_validate
classmethod
¶
model_validate(data: Any) -> NamedValueList
Custom validation to restore stored values.
Reconstructs a NamedValueList instance from serialized data, properly restoring all contained named values and their states.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
Any
|
Serialized data to deserialize |
required |
Returns:
Name | Type | Description |
---|---|---|
NamedValueList |
NamedValueList
|
New instance with restored values |
Source code in src/data_handlers/values.py
model_validate_json
classmethod
¶
model_validate_json(json_data: str, **kwargs) -> NamedValueList
Custom JSON deserialization to NamedValueList instance.
Reconstructs a NamedValueList instance from a JSON string representation, preserving the order of values and restoring their complete state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_data
|
str
|
JSON string containing serialized list data |
required |
**kwargs
|
Additional validation options for nested objects |
{}
|
Returns:
Name | Type | Description |
---|---|---|
NamedValueList |
NamedValueList
|
New instance with all values restored in order |
Example
Source code in src/data_handlers/values.py
register_value ¶
register_value(value: NamedValue) -> Self
Register a named value to the list.
Similar to append but uses the register_object method internally, which may perform additional validation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
NamedValue
|
Named value to register |
required |
Returns:
Name | Type | Description |
---|---|---|
Self |
Self
|
The list instance for method chaining |
Source code in src/data_handlers/values.py
NamedValueState ¶
State enumeration for NamedValue objects.
This enum tracks whether a named value has been set or remains unset. Once set, values are typically frozen unless explicitly forced to change.
Attributes:
Name | Type | Description |
---|---|---|
UNSET |
Indicates no value has been set yet |
|
SET |
Indicates value has been set and is frozen |
__repr__ ¶
__repr__() -> str
Get string representation for debugging.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The state value as a string ("unset" or "set") |