schola.core.utils.IdManager
Class Definition
class schola.core.utils.IdManager(ids)
Bases: object
A class to manage the mapping between nested and flattened ids.
Parameters
ids
Type: List[List[int]]
A nested list of lists of ids to manage, index in the list is first id, second id is stored in the second list.
Attributes
ids
Type: List[List[int]]
The nested list of lists of ids to manage.
Properties
id_list
Type: List[Tuple[int, int]]
(property)
List of nested ids, for lookups from flattened id to nested ids.
Returns: List of nested ids.
Return type: List[Tuple[int, int]]
id_map
Type: List[Dict[int, int]]
(property)
List of dictionaries mapping nested ids to flattened ids.
Returns: List of dictionaries mapping nested ids to flattened ids.
Return type: List[Dict[int, int]]
num_ids
Type: int
(property)
The number of ids managed by the IdManager.
Returns: The number of ids.
Return type: int
Methods
__init__
__init__(ids)
Parameters:
- ids (
List[List[int]]
)
flatten_id_dict
flatten_id_dict(nested_id_dict, default=None)
Flatten a dictionary of nested ids into a list of values.
Parameters:
- nested_id_dict (
Dict[int, Dict[int, T]]
) – The dictionary to flatten - default (
Optional[T]
, optional) – The default value to use if a key is not found, by default None
Returns: A flattened list of the values found in the dictionary.
Return type: List[T]
get_flattened_id
get_flattened_id(first_id, second_id)
Get the flattened id from a nested id.
Parameters:
- first_id (
int
) – The first id - second_id (
int
) – The second id
Returns: The flattened id.
Return type: int
get_nested_id
get_nested_id(flat_id)
Get the nested id from a flattened id.
Parameters:
- flat_id (
int
) – The flattened id to convert
Returns: The nested id.
Return type: Tuple[int, int]
nest_id_list
nest_id_list(id_list, default=None)
Nest a list of values, indexed by flattened id, into a dictionary of nested ids.
Parameters:
- id_list (
List[T]
) – The list of values to convert into a nested dictionary - default (
Optional[T]
, optional) – The default value to use if a key is not found, by default None
Returns: A nested dictionary of the values in id_list or default if values are missing.
Return type: Dict[int, Dict[int, T]]
partial_get
partial_get(first_id)
Get the second ids for a given first id.
Parameters:
- first_id (
int
) – The first id to get the second ids for
Returns: The second ids for the given first id.
Return type: List[int]