mypythontools.misc package

Module with miscellaneous functions that do not fit into other subpackage but are not big enough have it’s own subpackage.

class mypythontools.misc.TimeTable[source]

Bases: object

Class that create printable table with spent time on various phases that runs sequentionally.

Add entry when current phase end (not when it starts).

Example

>>> import time
...
>>> time_table = TimeTable()
>>> time.sleep(0.01)
>>> time_table.add_entry("First phase")
>>> time.sleep(0.02)
>>> time_table.add_entry("Second phase")
>>> time_table.add_entry("Third phase")
>>> time_table.finish_table()
...
>>> print(time_table.time_table)
+--------------+--------------+
|     Time     |  Phase name  |
+==============+==============+
| First phase  |    0...
add_entry(phase_name: str) None[source]

Add new line to the Time table.

finish_table(table_format: None | dict = None) None[source]

Create time table.

Parameters

table_format (None | dict, optional) – Dict of format settings used in tabulate. If None, default DEFAULT_TABLE_FORMAT is used. Defaults to None.

mypythontools.misc.delete_files(paths: PathLike | Iterable[PathLike], on_error: Literal['pass', 'raise'] = 'pass')[source]

Delete file, folder or Sequence of files or folders.

Folder can contain files, it will be also recursively deleted. You can choose behavior on error (because of permissions for example).

Parameters
  • paths (PathLike | Iterable[PathLike]) – List or tuple of paths to be deleted. Can be files as well as folders.

  • on_error (Literal["pass", "raise"], optional) – Depends whether you want to pass or raise an error. Error can occur when for example file is opened or if has no necessary permissions. Defaults to “pass”.

mypythontools.misc.print_progress(name: str, verbose: bool = True)[source]

Print current step of some process.

Divide it with newlines so it’s more readable.

Parameters
  • name (str) – Name current step.

  • verbose (bool) – It is possible to turn off logging of progress with one parameter config value. Defaults to True.

mypythontools.misc.watchdog(timeout: int | float, function: Callable, *args, **kwargs) Any[source]

Time-limited execution for python function. TimeoutError raised if not finished during defined time.

Parameters
  • timeout (int | float) – Max time execution in seconds.

  • function (Callable) – Function that will be evaluated.

  • *args – Args for the function.

  • *kwargs – Kwargs for the function.

Raises
  • TimeoutError – If defined time runs out.

  • RuntimeError – If function call with defined params fails.

Returns

Depends on used function.

Return type

Any

Examples

>>> import time
>>> def sleep(sec):
...     for _ in range(sec):
...         time.sleep(1)
>>> watchdog(1, sleep, 0)
>>> watchdog(1, sleep, 10)
Traceback (most recent call last):
TimeoutError: ...