API Reference
This page provides complete API documentation for all public classes and functions in StateTracker, automatically generated from source code docstrings.
Core Classes
State
The main class for creating and composing states.
- class statetracker.State(num_values=None, name=None, value=None, iter_order=None, *, _parents=None, _op=None)[source]
Bases:
objectA state that can be iterated and composed with other states.
- __init__(num_values=None, name=None, value=None, iter_order=None, *, _parents=None, _op=None)[source]
Create a state.
- property iter_order
Iteration order for this state.
- property num_values
Number of values this state can take (read-only).
- property id
Unique ID assigned by Manager (None if not registered).
- property name
Name of this counter.
- property value
Current value of this state.
- print_dag(style='clean')[source]
Print the ASCII tree visualization rooted at this state.
- Parameters:
style (
str) – Display style - ‘clean’ (default), ‘minimal’, or ‘repr’.
- get_states(include_inactive=True, named_only=True)[source]
Return dict of {name: value} for this state and all ancestors.
- Parameters:
- Return type:
- Returns:
Dictionary mapping state names to their current values, in reverse topological order (derived states first, parents last).
Manager
Context manager that must wrap all state operations.
- class statetracker.Manager[source]
Bases:
objectContext manager for handling State objects.
- get_ancestors(state, visited=None)[source]
Recursively collect all ancestor states and sync group peers.
Operation
Abstract base class for all state operations.
Exceptions
- exception statetracker.ConflictingValueAssignmentError[source]
Bases:
RuntimeErrorRaised when a state receives conflicting value assignments during propagation.
State Operation Functions
These functions create new states by combining or transforming existing states.
Product Operations
- statetracker.product(states, name=None)[source]
Create a State representing the cartesian product of the provided States.
- Parameters:
- Returns:
A State whose values index the cartesian product of the input states’ values.
- Return type:
State_type
- statetracker.ordered_product(states, name=None)[source]
Create a product State from the provided states, removing duplicates and automatically imposing an order based on state.iter_order and state.id.
This function recursively flattens nested product states before deduplication, which handles diamond patterns where the same state appears both as a direct parent and as an ancestor through another parent.
- Parameters:
- Returns:
A State representing the ordered, uniquified cartesian product of the input states.
- Return type:
State_type
Notes
Product is associative, so nested products are flattened:
ordered_product([A*B, C, D*A])becomesordered_product([A, B, C, D])Non-product operations (like stack, slice) are NOT flattened:
ordered_product([stack(A,B), C])keepsstack(A,B)as an atomic unit.
Stack (Disjoint Union)
Synchronize
- statetracker.synced_to(child_state, name=None, num_values=None)[source]
Create a new state synchronized with child_state.
- Parameters:
child_state (statetracker.state.State) – The state to synchronize with.
num_values (
Optional[Integral]) – Optional number of values for the new state. If not provided, uses child_state.num_values. Can be different from child_state.num_values - the group will track the max and states receive None when out of range.
- Return type:
statetracker.state.State
- Returns:
A new State synchronized with child_state.
Slice
- statetracker.slice(state, start=None, stop=None, step=None, name=None)[source]
Create a State whose values correspond to a slice of the parent State.
- Parameters:
- Returns:
A State whose values correspond to the specified slice of the parent State’s values.
- Return type:
State_type
Repeat
Shuffle
- statetracker.shuffle(state, seed=None, permutation=None, name=None)[source]
Create a State with the values of a parent State randomly shuffled.
- Parameters:
state (statetracker.state.State) – The State whose values will be shuffled.
seed (
Optional[Integral]) – Random seed to generate the shuffle permutation. Cannot be provided with ‘permutation’.permutation (
Optional[Sequence[Integral]]) – Explicit permutation of parent value indices, as a sequence of length parent.num_values. Cannot be provided with ‘seed’.name (
Optional[str]) – Name for the resulting shuffled State.
- Returns:
A State whose value order corresponds to a permutation of the parent’s values.
- Return type:
State_type
Sample
- statetracker.sample(state, num_values=None, sampled_states=None, seed=None, with_replacement=True, name=None)[source]
Create a State that samples values from the provided parent State.
- Parameters:
state (statetracker.state.State) – Parent State whose values will be sampled.
num_values (
Optional[Integral]) – Number of values to sample from the parent. Mutually exclusive with ‘sampled_states’.sampled_states (
Optional[Sequence[Integral]]) – Explicit sequence of parent value indices to use as samples. Mutually exclusive with ‘num_values’.seed (
Optional[Integral]) – Random seed for sampling. Only relevant if sampling with ‘num_values’ and not supplying ‘sampled_states’.with_replacement (
bool) – Whether to sample with replacement (True) or without replacement (False).name (
Optional[str]) – Name for the resulting sampled State.
- Returns:
A State whose values are a sampled subset of those of the parent State.
- Return type:
State_type
Split
- statetracker.split(state, split_spec, names=None)[source]
Split a State into multiple sub-States according to equal or proportional partitioning.
- Parameters:
state (statetracker.state.State) – The State object to be split.
split_spec (
Union[Integral,Sequence[Real]]) – If an integer N, split into N roughly equal parts. If a sequence of proportions, split according to these proportions (sequence length = number of parts).names (
Optional[Sequence[str]]) – Optional sequence of names for each resulting sub-State. If provided, must match the number of parts.
- Returns:
Tuple of new State objects corresponding to each partition of the original values.
- Return type:
Tuple[State_type, …]
Interleave
Operation Classes
These are the underlying operation classes used internally. Most users will use the convenience functions above instead of instantiating these directly.
ProductOp
StackOp
SyncOp
SliceOp
RepeatOp
ShuffleOp
SampleOp
- class statetracker.SampleOp(num_parent_values, num_values=None, sampled_states=None, seed=None, with_replacement=True)[source]
Bases:
OperationSample values from parent state.
- __init__(num_parent_values, num_values=None, sampled_states=None, seed=None, with_replacement=True)[source]