Utilities Module

The utilities module provides simple utility functions for dynamic class loading within the OptiX framework. It contains basic functions that support object serialization and deserialization by enabling runtime class resolution.

Module Functions

Class Loading Functions

utilities.get_fully_qualified_name(cls: type) str[source]

Generate a fully qualified name string for a Python class.

This function creates a string representation of a class by concatenating the module name and class name with a dot separator. The result can be used with the load_class() function to dynamically load the class.

Parameters:

cls (type) – The class object to generate a name for.

Returns:

A string in the format module_name.ClassName.

Return type:

str

Examples

Generate fully qualified names for classes:

from base.OXObject import OXObject

# Get the class name string
name = get_fully_qualified_name(OXObject)
print(name)  # Output: 'base.OXObject.OXObject'

# Works with built-in types too
list_name = get_fully_qualified_name(list)
print(list_name)  # Output: 'builtins.list'

See also

load_class(): Load a class from its fully qualified name.

utilities.load_class(fully_qualified_name: str) type[source]

Dynamically load a Python class from its fully qualified name.

This function loads a class by parsing the fully qualified name string, importing the module, and retrieving the class object from the module.

Parameters:

fully_qualified_name (str) – The fully qualified name of the class to load in the format module.ClassName.

Returns:

The loaded class object.

Return type:

type

Raises:

OXception – If the class cannot be loaded due to import errors or missing class names.

Examples

Load classes dynamically:

# Load a framework class
obj_class = load_class("base.OXObject.OXObject")
instance = obj_class()

# Roundtrip demonstration
from base.OXObject import OXObject
name = get_fully_qualified_name(OXObject)
loaded_class = load_class(name)
assert loaded_class is OXObject

See also

get_fully_qualified_name(): Generate class names for use with this function.

Examples

Basic Class Loading

from utilities import get_fully_qualified_name, load_class
from base.OXObject import OXObject

# Generate module.ClassName string
class_id = get_fully_qualified_name(OXObject)
print(class_id)  # Output: 'base.OXObject.OXObject'

# Dynamically load the class
loaded_class = load_class(class_id)
instance = loaded_class()

# Verify roundtrip integrity
assert loaded_class is OXObject

Integration with Serialization

from utilities import get_fully_qualified_name, load_class
from serialization import serialize_to_python_dict, deserialize_from_python_dict
from problem.OXProblem import OXLPProblem

# Create a problem
problem = OXLPProblem(name="Test Problem")

# Serialize to dictionary
problem_dict = serialize_to_python_dict(problem)
print(f"Class name in serialized data: {problem_dict['class_name']}")

# Deserialize from dictionary
restored_problem = deserialize_from_python_dict(problem_dict)
assert restored_problem.name == "Test Problem"

Error Handling

from utilities import load_class
from base.OXception import OXception

try:
    # Attempt to load a non-existent class
    bad_class = load_class("nonexistent.module.BadClass")
except OXception as e:
    print(f"Failed to load class: {e}")

See Also

  • base - Base classes that use the utilities module

  • serialization - Serialization system that relies on dynamic class loading

  • ../development/architecture - Framework architecture overview