Base Services

The base services module defines the fundamental service abstractions used throughout the Hiten framework.

Persistence Service

_PersistenceServiceBase()

Mixin offering a uniform persistence API around plain callables. Handles save, load, and load-in-place operations.

class hiten.algorithms.types.services.base._PersistenceServiceBase(*, save_fn, load_fn, load_inplace_fn=None)[source]

Bases: ABC

Mixin offering a uniform persistence API around plain callables.

Parameters:
  • save_fn (Callable[..., Any]) – The function to save the object.

  • load_fn (Callable[..., Any]) – The function to load the object.

  • load_inplace_fn (Optional[Callable[..., Any]] = None) – The function to load the object in place.

save(target, filepath, **kwargs)[source]

Save the object to a file.

Parameters:
  • target (Any) – The object to save.

  • filepath (Any) – The path to the file to save the object to.

Return type:

Any

load(filepath, **kwargs)[source]

Load the object from a file.

Parameters:

filepath (Any) – The path to the file to load the object from.

Return type:

Any

load_inplace(target, filepath, **kwargs)[source]

Load the object from a file in place.

Parameters:
  • target (Any) – The object to load the object into.

  • filepath (Any) – The path to the file to load the object from.

Return type:

Any

Dynamics Service

_DynamicsServiceBase()

Mixin offering a uniform dynamics API around plain callables. Provides caching and domain-specific computations.

class hiten.algorithms.types.services.base._DynamicsServiceBase(domain_obj)[source]

Bases: ABC

Mixin offering a uniform dynamics API around plain callables.

Parameters:

domain_obj (Any) – The domain object.

cache

The cache service.

Type:

_CacheServiceBase

property domain_obj: Any

The domain object.

get_or_create(key, factory)[source]

Get or create a cache value.

Parameters:
  • key (Any) – The cache key.

  • factory (Callable[[], CacheValueT]) – The factory function to create the cache value.

Return type:

CacheValueT

make_key(*args)[source]

Create a cache key with the current function name as the first element.

Parameters:

*args (Any) – Additional arguments to include in the cache key.

Return type:

tuple[Any, …]

__getitem__(key)[source]

Get a cache value.

Parameters:

key (Any) – The cache key.

Return type:

CacheValueT

__setitem__(key, value)[source]

Set a cache value.

Parameters:
  • key (Any) – The cache key.

  • value (CacheValueT) – The cache value.

Return type:

CacheValueT

reset(key=None)[source]

Reset the cache.

Parameters:

key (Any, optional) – The cache key to reset. If None, the entire cache is cleared.

Return type:

None

__getstate__()[source]

Get state for pickling.

Preserves computed properties and cache that are expensive to recompute. Excludes only the domain_obj to avoid circular references.

__setstate__(state)[source]

Set state after unpickling.

Restores computed properties and cache, reinitializes only domain_obj.

Cache Service

_CacheServiceBase()

Helper providing lazy caching for dynamics-oriented adapters. Manages cache operations and key generation.

class hiten.algorithms.types.services.base._CacheServiceBase[source]

Bases: Generic[CacheValueT]

Helper providing lazy caching for dynamics-oriented adapters.

Parameters:

_cache (Dict[Any, CacheValueT]) – The cache dictionary.

get_or_create(key, factory)[source]

Get or create a cache value.

Parameters:
  • key (Any) – The cache key.

  • factory (Callable[[], CacheValueT]) – The factory function to create the cache value.

Return type:

CacheValueT

get(key)[source]

Get a cache value.

Parameters:

key (Any) – The cache key.

Return type:

CacheValueT

set(key, value)[source]

Set a cache value.

Parameters:
  • key (Any) – The cache key.

  • value (CacheValueT) – The cache value.

Return type:

CacheValueT

reset(key=None)[source]

Reset the cache.

Parameters:

key (Any, optional) – The cache key to reset. If None, the entire cache is cleared.

Return type:

None

make_key(*args)[source]

Create a cache key with the current function name as the first element.

This helps avoid cache key collisions between different methods. Automatically handles unhashable types by converting them to hashable equivalents.

Parameters:

*args (Any) – Additional arguments to include in the cache key.

Returns:

Cache key with function name as first element.

Return type:

tuple

Service Bundle

_ServiceBundleBase()

Lightweight helper for service bundles offering ergonomic helpers. Coordinates multiple services for domain objects.

class hiten.algorithms.types.services.base._ServiceBundleBase(domain_obj)[source]

Bases: ABC

Lightweight helper for service bundles offering ergonomic helpers.

Parameters:

domain_obj (Any) – The domain object.

property domain_obj: Any

The domain object.

__getitem__(service_name)[source]

Get a service by name.

This method allows dynamic access to any service in the service bundle.

Parameters:

service_name (str) – The name of the service to retrieve (e.g., ‘correction’, ‘continuation’)

Returns:

The requested service instance

Return type:

Any

Raises:

AttributeError – If the requested service is not available

abstractmethod classmethod default(domain_obj)[source]

Create a default service bundle.

Parameters:

domain_obj (Any) – The domain object.

Return type:

_ServiceBundleBase

abstractmethod classmethod with_shared_dynamics(domain_obj, dynamics)[source]

Create a service bundle with a shared dynamics service.

Parameters:
Return type:

_ServiceBundleBase