Utility modules and functions

Package Contents

maestral.utils.natural_size(num: float, unit: str = 'B', sep: bool = True)str[source]

Convert number to a human readable string with decimal prefix.

  • num (float) – Value in given unit.

  • unit – Unit suffix.

  • sep – Whether to separate unit and value with a space.


Human readable string with decimal prefixes.

maestral.utils.chunks(lst: List, n: int, consume: bool = False)Iterator[List][source]

Partitions an iterable into chunks of length n.

  • lst – Iterable to partition.

  • n – Chunk size.

  • consume – If True, the list will be consumed (emptied) during the iteration. This can be used to free memory in case of large lists.


Iterator over chunks.

maestral.utils.clamp(n: _N, minn: _N, maxn: _N)_N[source]

Clamps a number between a minimum and maximum value.

  • n – Original value.

  • minn – Minimum allowed value.

  • maxn – Maximum allowed value.


Clamped value.

maestral.utils.get_newer_version(version: str, releases: Iterable[str])Optional[str][source]

Checks a given release version against a version list of releases to see if an update is available. Only offers newer versions if they are not a prerelease.

  • version – The current version.

  • releases – A list of valid cleaned releases.


The version string of the latest release if a newer release is available.

maestral.utils.removeprefix(string: str, prefix: str)str[source]

Removes the given prefix from a string. Only the first instance of the prefix is removed. The original string is returned if it does not start with the given prefix.

This follows the Python 3.9 implementation of str.removeprefix.

  • string – Original string.

  • prefix – Prefix to remove.


String without prefix.

maestral.utils.sanitize_string(string: str)str[source]

Converts a string provided by file system APIs, which may contain surrogate escapes for bytes with unknown encoding, to a string which can always be displayed or printed. This is done by replacing invalid characters with “�”.


string – Original string.


Sanitised path where all surrogate escapes have been replaced with “�”.