Utility functions

Conversions

momotor.options.convert.convert_duration(t)

Convert a string containing a duration into a float. The duration is defined as “hh:mm:ss” (hours, minutes, seconds). The seconds can contain decimals.

>>> convert_duration('1')
1.0
>>> convert_duration('1.5')
1.5
>>> convert_duration('10')
10.0
>>> convert_duration('100')
100.0
>>> convert_duration('1:00')
60.0
>>> convert_duration('10:00')
600.0
>>> convert_duration('100:00')
6000.0
>>> convert_duration('1:00:00')
3600.0
>>> convert_duration('1:00:00')
3600.0
>>> convert_duration('10:00:00')
36000.0
>>> convert_duration('100:00:00')
360000.0
>>> convert_duration('123:45:67.89')
445567.89
>>> convert_duration(1)
1.0
>>> convert_duration(1.0)
1.0
Parameters:

t (Union[str, int, float, None]) –

Return type:

Optional[float]

Returns:

momotor.options.convert.convert_intlist(il: str, empty_values: bool = False) Tuple[int, ...]
momotor.options.convert.convert_intlist(il: str, empty_values: bool = True) Tuple[Optional[int], ...]

Convert a string containing a comma-separated sequence of integer values into a tuple of integers. Allows a range to be specified using “..” as separator, e.g. “100..109”. If :param:`allow_empty` is True, empty values in the list are allowed and returned as None

>>> convert_intlist("")
()
>>> convert_intlist("", empty_values=True)
(None,)
>>> convert_intlist("0")
(0,)
>>> convert_intlist("0,1")
(0, 1)
>>> convert_intlist("0,,1")
Traceback (most recent call last):
...
ValueError: '0,,1' is not a valid list of integers: empty values not allowed
>>> convert_intlist("0,,1", empty_values=True)
(0, None, 1)
>>> convert_intlist("0, ,1", empty_values=True)
(0, None, 1)
>>> convert_intlist("0,1,100..109")
(0, 1, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109)
>>> convert_intlist("0, 1, 100 .. 109")
(0, 1, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109)
>>> convert_intlist("0,", empty_values=True)
(0, None)
>>> convert_intlist("0,1,X")
Traceback (most recent call last):
...
ValueError: '0,1,X' is not a valid list of integers: invalid literal for int() with base 10: 'X'
>>> convert_intlist("0..")
Traceback (most recent call last):
...
ValueError: '0..' is not a valid list of integers: incomplete range 0..
>>> convert_intlist("0..", empty_values=True)
Traceback (most recent call last):
...
ValueError: '0..' is not a valid list of integers: incomplete range 0..
Parameters:
  • il (str) – string to convert

  • empty_values (bool) – allow empty values in the list. These will be converted to None

Return type:

Tuple[Optional[int], ...]

Returns:

tuple of integers

Raises:

ValueError – if any section of the list cannot be parsed as an integer

momotor.options.convert.convert_size(t)

Convert a size into an integer. If size ends with “i” or “ib”, the value is a binary (IEC) value, otherwise it is a decimal (SI) value. (See https://en.wikipedia.org/wiki/Binary_prefix)

Supported unit prefixes are:

Decimal

Binary

Value

SI

Value

IEC

1

(none)

1

(none)

1000

k

kilo

1024

Ki

kibi

10002

M

mega

10242

Mi

mebi

10003

G

giga

10243

Gi

gibi

10004

T

tera

10244

Ti

tebi

10005

P

peta

10245

Pi

pebi

10006

E

exa

10246

Ei

exbi

10007

Z

zetta

10247

Zi

zebi

10008

Y

yotta

10248

Yi

yobi

Case of unit is ignored, i.e. MiB == mib == MIB

>>> convert_size('16')
16
>>> convert_size('16B')
16
>>> convert_size('16 B')
16
>>> convert_size('16k')
16000
>>> convert_size('16kb')
16000
>>> convert_size('16ki')
16384
>>> convert_size('16kib')
16384
>>> convert_size('16MiB')
16777216
>>> convert_size('16gib')
17179869184
>>> convert_size('16TIB')
17592186044416
Parameters:

t (Union[str, int, None]) –

Return type:

Optional[int]

Returns:

File filters

momotor.options.filter_files.filter_files(files, class_name)

Filter a FilterableTuple of File objects on the name and class attributes.

The class_name argument contains the class and name to filter on, in the format <class>#<name>. The name part can contain wildcards, and is optional.

Parameters:
Return type:

FilterableTuple[File]

Returns:

momotor.options.filter_files.ifilter_files(files, class_name)

Return an iterable that filters a FilterableTuple of File objects on the name and class attributes.

The class_name argument contains the class and name to filter on, in the format <class>#<name>. The name part can contain wildcards, and is optional.

Parameters:
Return type:

Iterable[File]

Returns:

Splitting

momotor.options.split.multi_split(s, extra_sep=None, maxsplit=0)

Split a string on multiple separators.

Only splits on whitespace by default, but extra_sep can provide other separators to split the string on. If maxsplit is nonzero, at most maxsplit splits occur, and the remainder of the string is returned as the final element.

Parameters:
  • s (str) – string to process

  • extra_sep (Optional[Iterable]) – extra separators to split the string on

  • maxsplit (int) – maximum number of splits

Return type:

Iterable[str]

Returns:

>>> list(multi_split('testing'))
['testing']
>>> list(multi_split('testing one two three'))
['testing', 'one', 'two', 'three']
>>> list(multi_split('testing,one two three'))
['testing,one', 'two', 'three']
>>> list(multi_split('testing,one two three', ','))
['testing', 'one', 'two', 'three']
>>> list(multi_split('testing,one two three', ',', 1))
['testing', 'one two three']
>>> list(multi_split('testing,, one   two ,, three', ','))
['testing', 'one', 'two', 'three']
>>> list(multi_split('   testing,, one   two ,, three   ', ','))
['testing', 'one', 'two', 'three']