pythia.utils.ext#

Python extensions and decorators.

pythia.utils.ext.get_arch() str[source]#

Return system arch.

Returns:

platform, like uname machine.

pythia.utils.ext.grouped(iterable: Iterable[T], size=2) Iterable[Tuple[T, ...]][source]#

Iterate by groups.

Parameters:
  • iterable – container for the data to group by.

  • size – size of the groups

Returns:

zip containing tuples of values.

Example

>>> [*grouped(range(10),3)]
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]
pythia.utils.ext.import_from_path(name: str, path: str | Path) ModuleType[source]#

Import a module from a filepath.

Parameters:
  • name – the name to use for the module when importing.

  • path – path to the python file to import as a module.

Returns:

The imported module.

Raises:

ImportError – unable to get spec from location

See also

importlib.util.spec_from_file_location

pythia.utils.ext.import_from_str(module: str, *, name=None, suffix: str = '') module[source]#

Import python file as a module or from its path.

Parameters:
  • module – name of the python file to import.

  • name__name__ to use when import thing the module. If not set, defaults to the file’s stem.

  • suffix – file suffix. If set, load name as a file. Otherwise,

Returns:

The improted module.

pythia.utils.ext.not_empty(path: Path) Path[source]#

Raise if receives an empty value.

Parameters:

path – The file to check for emptiness.

Returns:

The decorated function.

Raises:

EOFError – The received file is empty.

pythia.utils.ext.not_none(value: Optional[Any])[source]#

Raise if receives None.

Parameters:

value – The value which should not be none.

Returns:

The received value.

Raises:

ValueError – The received value was None.

pythia.utils.ext.remove_prefix(input_string: str, prefix: str) str[source]#

Remove leading substring.

Parameters:
  • input_string – Input string to look for the suffix.

  • prefix – The substring to match at the start of the input string.

Returns:

If the string ends with the suffix string and that suffix

is not empty, return string[:-len(suffix)]. Otherwise, return a copy of the original string

Backport of stdlib@3.9

pythia.utils.ext.remove_suffix(input_string: str, suffix: str) str[source]#

Remove trailing substring.

Parameters:
  • input_string – Input string to look for the suffix.

  • suffix – The substring to match at the end of the input string.

Returns:

If the string ends with the suffix string and that suffix

is not empty, return string[:-len(suffix)]. Otherwise, return a copy of the original string

Backport of stdlib@3.9