The data module
Utilities for randomising spawn templates and other structured data dicts.
Overview
data_choose_value_from_template takes a template dict where each value is either a list (one element chosen at random) or a dict of parallel lists (a single random index is picked and applied consistently across all inner lists). This keeps related attributes — for example a name and a ship type — coherent when randomly selecting.
Quick example
template = {
"name": ["Raider", "Marauder", "Corsair"],
"ship": ["cruiser", "frigate", "fighter"],
}
chosen = data_choose_value_from_template(template)
log(f"Spawning {chosen['name']} in a {chosen['ship']}")
from sbs_utils.procedural.data import data_choose_value_from_template
template = {
"name": ["Raider", "Marauder", "Corsair"],
"ship": ["cruiser", "frigate", "fighter"],
}
chosen = data_choose_value_from_template(template)
# chosen == {"name": "Marauder", "ship": "frigate"} (consistently paired)
Parallel arrays (consistent index)
When a key maps to a dict of parallel lists, the same random index is used for all inner lists:
template = {
"unit": {
"name": ["Alpha", "Beta", "Gamma"],
"ship": ["cruiser", "frigate", "scout"],
"side": ["tsn", "tur", "skaraan"],
}
}
chosen = data_choose_value_from_template(template)
# e.g. chosen["unit"] == {"name": "Beta", "ship": "frigate", "side": "tur"}
# All three values came from index 1.
API
data_choose_value_from_template(data)
Randomly resolve a data template dict, selecting one value per key.
Each key in data may have a list value (random element chosen) or a
dict of parallel arrays (a random index is chosen across all arrays so the
result stays consistent).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
Template dict mapping keys to lists or dicts of parallel lists. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
A new dict with the same keys and randomly resolved values. |