neuralib.util.utils

neuralib.util.utils.uglob(directory, pattern, is_dir=False)[source]

Use glob pattern to find the unique file in the directory.

Parameters:
  • directory (str | Path | bytes | BinaryIO | BufferedIOBase | BufferedReader) – Directory

  • pattern (str) – Glob pattern

  • is_dir (bool) – Is the pattern point to a directory?

Returns:

The unique path

Raises:
  • FileNotFoundError – the unique path is not existed.

  • NotADirectoryErrordirectory is not a directory

  • RuntimeError – more than one path are found.

Return type:

Path

neuralib.util.utils.filter_matched(pattern, strings)[source]

Filter a list of string element that match the pattern.

Parameters:
  • pattern (str) – Regular expression

  • strings (list[str]) – List of strings to find the pattern

Returns:

Return type:

list[str]

neuralib.util.utils.joinn(sep, *part)[source]

join non-None str with sep.

Parameters:
  • sep (str)

  • part (str | None)

Return type:

str

neuralib.util.utils.ensure_dir(p, verbose=True)[source]

Ensure the path is a directory. Create if it is not exist.

Parameters:
  • p (str | Path | bytes | BinaryIO | BufferedIOBase | BufferedReader) – path to be checked

  • verbose (bool) – print verbose message

Return type:

Path

neuralib.util.utils.keys_with_value(dy: dict[KT, Any | Collection[VT]], value: VT, to_item: Literal[False] = False) list[KT][source]
neuralib.util.utils.keys_with_value(dy: dict[KT, VT | Collection[VT]], value: VT, to_item: Literal[True] = True) KT

Get keys from a dict that are associated with the value.

Supports value types: str, int, float (with tolerance), and any collection types.

Parameters:
  • dy – The value to match against the dictionary values

  • value – The value to match against the dictionary value

  • to_item – List of item to item if unique

Returns:

A list of keys whose values match the provided value

neuralib.util.utils.cls_hasattr(cls, attr)[source]

Check if attributes in class

Parameters:
  • cls (type) – The class to check for the attribute.

  • attr (str) – The name of the attribute to look for within the class and its hierarchy.

Returns:

True if the class or any of its parent classes has the specified attribute, False otherwise.

Return type:

bool