ai2_kit.core.util module#

class ai2_kit.core.util.JoinTag[source]#

Bases: object

a tag to join strings in a list

classmethod from_yaml(constructor, node)[source]#
classmethod register(yaml: YAML)[source]#
classmethod to_yaml(dumper, data)[source]#
yaml_tag = '!join'#
class ai2_kit.core.util.LoadTextTag[source]#

Bases: object

a tag to read string from file

classmethod from_yaml(constructor, node)[source]#
classmethod register(yaml: YAML)[source]#
classmethod to_yaml(dumper, data)[source]#
yaml_tag = '!load_text'#
class ai2_kit.core.util.LoadYamlTag[source]#

Bases: object

a tag to read string from file

classmethod from_yaml(constructor, node)[source]#
classmethod register(yaml: YAML)[source]#
classmethod to_yaml(dumper, data)[source]#
yaml_tag = '!load_yaml'#
ai2_kit.core.util.default_mutable_field(obj)[source]#
ai2_kit.core.util.dict_nested_get(d: dict, keys: ~typing.List[str], default=<object object>)#

get value from nested dict

ai2_kit.core.util.dict_nested_set(d: dict, keys: List[str], value)#

set value to nested dict

ai2_kit.core.util.dict_remove_dot_keys(d)[source]#
ai2_kit.core.util.dump_json(obj, path: str)#
ai2_kit.core.util.dump_text(text: str, path: str, **kwargs)#
ai2_kit.core.util.ensure_dir(path: str)#
ai2_kit.core.util.expand_globs(patterns: Iterable[str], raise_invalid=False) List[str]#

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)#

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.flatten(l: List[List[T]]) List[T][source]#
ai2_kit.core.util.flush_stdio()#
ai2_kit.core.util.format_env_string(s: str) str[source]#
ai2_kit.core.util.get_yaml()[source]#
ai2_kit.core.util.limit(it, size=- 1)#

limit the size of an iterable

ai2_kit.core.util.list_even_sample(l, size)#
ai2_kit.core.util.list_random_sample(l, size, seed=None)#
ai2_kit.core.util.list_sample(l, size, method='even', **kwargs)#
ai2_kit.core.util.list_split(l: List[T], n: int) List[List[T]][source]#

split list into n chunks

ai2_kit.core.util.load_json(path: Union[Path, str], encoding: str = 'utf-8')[source]#
ai2_kit.core.util.load_text(path: Union[Path, str], encoding: str = 'utf-8')[source]#
ai2_kit.core.util.load_yaml_file(path: Union[Path, str])[source]#
ai2_kit.core.util.load_yaml_files(*paths: Tuple[Path], quiet: bool = False, purge_anonymous=True)[source]#
ai2_kit.core.util.merge_dict(lo: dict, ro: dict, path=None, ignore_none=True, quiet=False)#

Merge two dict, the left dict will be overridden. Note: list will be replaced instead of merged.

ai2_kit.core.util.nested_set(d: dict, keys: List[str], value)[source]#
ai2_kit.core.util.parse_path_list(path_list_str: Union[str, List[str]], to_abs: bool = False)[source]#

Parse path list of environment variable style string

ai2_kit.core.util.s_uuid()[source]#

short uuid

ai2_kit.core.util.short_hash(s: str) str[source]#

short hash string

ai2_kit.core.util.sort_unique_str_list(l: List[str]) List[str][source]#

remove duplicate str and sort

async ai2_kit.core.util.to_awaitable(value: T) T[source]#
ai2_kit.core.util.wait_for_change(widget, attribute)[source]#

Wait for attribute change of a Jupyter widget