expyriment.misc.MediaTime

class expyriment.misc.MediaTime(time)

A class representing time as used in the context of media playback.

Internally represents time in seconds as a float value (inherits from built-in float type), but can deal with multiple formats when creating and comparing time.

Parameters:
timeint, float, tuple, list or str
the time to represent; can be any of the following formats:
  • float: seconds

  • tuple: (minutes, seconds) or (hours, minutes, seconds)

  • list: [minutes, seconds] or [hours, minutes, seconds]

  • str: ‘hh:mm:ss’, ‘hh:mm:ss.sss’, or ‘mm:ss.sss’

Examples

>>> MediaTime(15.4)                # seconds
>>> MediaTime((1, 21.5))           # (min, sec)
>>> MediaTime((1, 1, 2))           # (hr, min, sec)
>>> MediaTime('01:01:33.5')        # (hr:min:sec)
>>> MediaTime('01:01:33.045')
>>> MediaTime('01:01:33,5')        # comma works too
>>> MediaTime(3600) == '01:00:00'  # True
__init__(*args, **kwargs)
as_integer_ratio(/)

Return a pair of integers, whose ratio is exactly equal to the original float.

The ratio is in lowest terms and has a positive denominator. Raise OverflowError on infinities and a ValueError on NaNs.

>>> (10.0).as_integer_ratio()
(10, 1)
>>> (0.0).as_integer_ratio()
(0, 1)
>>> (-.25).as_integer_ratio()
(-1, 4)
conjugate(/)

Return self, the complex conjugate of any float.

static convert_to_seconds(time)

Try to convert time specified in common media formats into seconds.

Parameters:
timeint, float, tuple, list or str
the time to convert; can be any of the following formats:
  • float: seconds

  • tuple: (minutes, seconds) or (hours, minutes, seconds)

  • list: [minutes, seconds] or [hours, minutes, seconds]

  • str: ‘hhrmm:ss’, ‘hh:mm:ss.sss’, or ‘mm:ss.sss’

Returns:
float

The media time in seconds.

Examples

>>> convert_to_seconds(15.4)          # seconds
15.4
>>> convert_to_seconds((1, 21.5))     # (min, sec)
81.5
>>> convert_to_seconds((1, 1, 2))     # (hr, min, sec)
3662
>>> convert_to_seconds('01:01:33.5')  # (hr:min:sec)
3693.5
>>> convert_to_seconds('01:01:33.045')
3693.045
>>> convert_to_seconds('01:01:33,5')  # comma works too
3693.5
classmethod fromhex(string, /)

Create a floating-point number from a hexadecimal string.

>>> float.fromhex('0x1.ffffp10')
2047.984375
>>> float.fromhex('-0x1p-1074')
-5e-324
hex(/)

Return a hexadecimal representation of a floating-point number.

>>> (-0.1).hex()
'-0x1.999999999999ap-4'
>>> 3.14159.hex()
'0x1.921f9f01b866ep+1'
imag

the imaginary part of a complex number

is_integer(/)

Return True if the float is an integer.

real

the real part of a complex number