wystia.utils.parse package

Submodules

wystia.utils.parse.file module

wystia.utils.parse.file.resolve_contents(file_path: str | None = None, contents: str | None = None, raise_=True) str[source]

Resolves file contents, given two optional parameters.

Parameters:
  • file_path – An optional path to the file to read.

  • contents – The optional contents of a file.

  • raise – Whether to raise an error if the file contents are empty.

Returns:

The resolved file contents.

Raises:

ContentIsEmpty – If both file_path and contents are empty, and the raise_ flag is enabled.

wystia.utils.parse.srt module

wystia.utils.parse.srt.get_srt_duration(srt_contents: str, default_end_seconds=0.0) float[source]

Gets the total duration (based on end timestamp) of an SRT file

wystia.utils.parse.srt.total_ms(ts: str) int[source]

Converts a timestamp containing hours, minutes, seconds, and milliseconds (for example, in the “HH:mm:ss,SSS” format) to an integer value representing the total milliseconds.

For example, a string like “1:20:32,5” will be returned as 4832005

Supports parsing the following input formats:
  • (H)H:mm:ss,SSS

  • (H)H:mm:ss.SSS

  • (H)H:mm:ss:SSS

A modified version of the following (great) solution: https://stackoverflow.com/a/57610198

wystia.utils.parse.srt.total_seconds(ts: str) str[source]

Converts a timestamp containing hours, minutes, seconds, and milliseconds (for example, in the “HH:mm:ss,SSS” format) to a string representing the total seconds, along with the millisecond part.

For example, a string like “1:20:32,5” will be returned as “4832.005”

Supports parsing the following input formats:
  • (H)H:mm:ss,SSS

  • (H)H:mm:ss.SSS

  • (H)H:mm:ss:SSS

A modified version of the following (great) solution: https://stackoverflow.com/a/57610198

wystia.utils.parse.types module

wystia.utils.parse.types.as_bool(o: str | bool, default=False)[source]

Return o if already a boolean, otherwise return the boolean value for a string. If o is None or an empty string, return default instead.

wystia.utils.parse.types.as_datetime(o: str | Number | datetime, default=None, raise_=True)[source]

Return o if already a datetime object, otherwise convert the object to a datetime object using the below logic.

  • str: convert date strings (in ISO format) via the built-in fromisoformat method.

  • Number (int or float): Convert a numeric timestamp via the

    built-in fromtimestamp` method.

If o is None or false-y, return default instead.

Otherwise, if we’re unable to convert the value of o to a datetime as expected, raise an error if the raise_ parameter is true.

wystia.utils.parse.types.as_int(o: str | int, default=0, raise_=True)[source]

Return o if already a int, otherwise return the int value for a string. If o is None or an empty string, return default instead.

If o cannot be converted to an int, raise an error if raise_ is true, other return default instead.

wystia.utils.parse.types.as_list(o: str | list[str], sep=',')[source]

Return o if already a list. If o is None or an empty string, return an empty list. Otherwise, split the string on sep and return the list result.

wystia.utils.parse.types.as_str(o: str | None, default='', raise_=True)[source]

Return o if already a str, otherwise return the string value for o. If o is None or an empty string, return default instead.

If o cannot be converted to an str, raise an error if raise_ is true, other return default instead.

Module contents