ai2_kit.core.util module#
- class ai2_kit.core.util.JoinTag[source]#
Bases:
object
a tag to join strings in a list
- yaml_tag = '!join'#
- class ai2_kit.core.util.LoadTextTag[source]#
Bases:
object
a tag to read string from file
- yaml_tag = '!load_text'#
- class ai2_kit.core.util.LoadYamlTag[source]#
Bases:
object
a tag to read string from file
- yaml_tag = '!load_yaml'#
- ai2_kit.core.util.cmd_with_checkpoint(cmd: str, checkpoint: str, ignore_error: bool = False)[source]#
Add checkpoint to a shell command. If a checkpoint file exists, the command will be skipped, otherwise the command will be executed and a checkpoint file will be created if the command is executed successfully.
- Parameters:
cmd – shell command
checkpoint – checkpoint file name
ignore_error – if True, will not raise error if the command failed
- ai2_kit.core.util.dict_nested_get(d: dict, keys: ~typing.List[str], default=<object object>)[source]#
get value from nested dict
- ai2_kit.core.util.dict_nested_set(d: dict, keys: List[str], value)[source]#
set value to nested dict
- ai2_kit.core.util.expand_globs(patterns: Iterable[str], raise_invalid=False) List[str] [source]#
Expand glob patterns in paths
- Parameters:
patterns – list of paths or glob patterns
raise_invalid – if True, will raise error if no file found for a glob pattern
- Returns:
list of expanded paths
- ai2_kit.core.util.flat_evenly(list_of_lists)[source]#
flat a list of lists and ensure the output result distributed evenly >>> flat_evenly([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) [1, 4, 7, 2, 5, 8, 3, 6, 9] Ref: https://stackoverflow.com/questions/76751171/how-to-flat-a-list-of-lists-and-ensure-the-output-result-distributed-evenly-in-p
- ai2_kit.core.util.list_sample(l, size: int, method: Literal['even', 'random', 'truncate'] = 'even', **kwargs)[source]#
sample list
- Parameters:
size – size of sample, if size is larger than data size, return all data
method – method to sample, can be ‘even’, ‘random’, ‘truncate’, default is ‘even’
seed – seed for random sample, only used when method is ‘random’
Note that by default the seed is length of input list, if you want to generate different sample each time, you should set random seed manually
- ai2_kit.core.util.load_yaml_files(*paths: Path | str, quiet: bool = False, purge_anonymous=True)[source]#
- ai2_kit.core.util.merge_dict(lo: dict, ro: dict, path=None, ignore_none=True, quiet=False)[source]#
Merge two dict, the left dict will be overridden. Note: list will be replaced instead of merged.
- ai2_kit.core.util.parse_path_list(path_list_str: str | List[str], to_abs: bool = False)[source]#
Parse path list of environment variable style string
- ai2_kit.core.util.slice_from_str(index: str)[source]#
get slice object from a string expression, for example: “1:10” -> slice(1, 10), “:10” -> slice(None, 10), etc