schola.core.utils.IdManager
class schola.core.utils.IdManager(ids)
: Bases: object
A class to manage the mapping between nested and flattened ids.
Parameters: : ids (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.
ids : The nested list of lists of ids to manage.
Type: : List[List[int]]
Methods
__init__ (ids) | |
flatten_id_dict (nested_id_dict[, default]) | Flatten a dictionary of nested ids into a list of values. |
get_flattened_id (first_id, second_id) | Get the flattened id from a nested id. |
get_nested_id (flat_id) | Get the nested id from a flattened id. |
nest_id_list (id_list[, default]) | Nest a list of values, indexed by flattened id, into a dictionary of nested ids. |
partial_get (first_id) | Get the second ids for a given first id. |
Attributes
id_list | List of nested ids, for lookups from flattened id to nested ids. |
id_map | List of dictionaries mapping nested ids to flattened ids. |
num_ids | The number of ids managed by the IdManager. |
__init__(ids) : Parameters: : ids (List[List[int]**])
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(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(flat_id) : Get the nested id from a flattened id.
Parameters: : flat_id (int) – The flattened id to convert.
Returns: : The nested id.
property id_list*: List[Tuple[int, int]]* : List of nested ids, for lookups from flattened id to nested ids.
Returns: : List of nested ids.
Return type: : List[Tuple[int, int]]
property id_map*: List[Dict[int, int]]* : 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]]
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]]
property num_ids*: int* : The number of ids managed by the IdManager.
Returns: : The number of ids.
Return type: : int
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]