duat.common

Common tools.

class duat.common.Call(fn, *args, **kwargs)[source]

Objectization of a call to be made. When an instance is called, such a call will be made.

__call__()[source]

Call self as a function.

__init__(fn, *args, **kwargs)[source]

Initialize self. See help(type(self)) for accurate signature.

class duat.common.MPCaller(num_threads=2)[source]

MultiProcessing Caller. Makes calls using multiple subprocesses.

processes

list of multiprocessing.Process – Processes managed by the instance.

__init__(num_threads=2)[source]

Initialize self. See help(type(self)) for accurate signature.

__repr__()[source]

Return repr(self).

abort(interrupt=False)[source]

Remove all queued calls and ask processes to stop.

Parameters:interrupt – If True, terminate all processes.
add_call(call)[source]

Add a call to the instance’s stack.

Parameters:call (Callable) – A function whose call method will be invoked by the processes. Consider using lambda functions or a Call instance.
spawn_threads(num_threads)[source]

Create the required number of processes and add them to the caller.

This does not remove previously created processes.

wait_calls(blocking=True, respawn=False)[source]

Ask all processes to consume the queue and stop after that.

Parameters:
  • blocking (bool) – Whether to block the call, waiting for processes termination.
  • respawn (bool) – If blocking is True, this indicates whether to respawn the threads after the calls finish. If blocking is not True this is ignored (no automatic respawn if non-blocking).
duat.common.ensure_dir_exists(path)[source]

Ensure a directory exists, creating it if needed.

Parameters:path (str) – The path to the directory.
Raises:OSError – An error occurred when creating the directory.
duat.common.ensure_executable(path, all_users=None)[source]

Ensure a file is executable.

Parameters:
  • path (str) – the path to the file.
  • all_users (bool) – whether it should be make executable for the user or for all users.
duat.common.get_dir_size(dir_path)[source]

Get the size of a directory in bytes.

duat.common.head(path, lines=10)[source]

Get the first lines of a file.

Parameters:
  • path (str) – Path to the file to read.
  • lines (int) – Number of lines to read.
Returns:

The lines found.

Return type:

list of str

duat.common.human_order_key(text)[source]

Key function to sort in human order.

duat.common.ifd(d, v1, v2, v3)[source]

Conditionally select a value depending on the given dimension number.

Parameters:
  • d (int) – Dimension number (1, 2, or 3).
  • v1 – The value if d = 1.
  • v2 – The value if d = 2.
  • v3 – The value if d = 3.
Returns:

v1, v2 or v3.

duat.common.tail(path, lines=1, _step=4098)[source]

Get the last lines of a file.

Parameters:
  • path (str) – Path to the file to read.
  • lines (int) – Number of lines to read.
  • _step (int) – Size of the step used in the search.
Returns:

The lines found.

Return type:

list of str