qstack.tools

Utility functions and classes for Q-stack.

Provides decorators, argument parsers, and helper functions for command-line tools.

class qstack.tools.Cursor(action='slicer', inc=<function Cursor.<lambda>>, i0=0)[source]

Bases: object

Cursor class to manage dynamic indexing.

Parameters:
  • action (str) – Type of indexing action (‘slicer’ or ‘ranger’).

  • (callable (inc) – int->int): Function to determine increment size. Defaults to identity function.

  • i0 (int) – Initial index position. Defaults to 0.

i[source]

Current index position.

Type:

int

i_prev[source]

Previous index position.

Type:

int

cur (range or slice

Current range or slice.

inc[source]

Increment function.

Type:

callable int->int

add(di)[source]

Advances the cursor by increment and returns current range/slice.

__call__(di=None)[source]

Advances the cursor if di is not None, returns current range/slice.

add(di)[source]

Advance the cursor and return the current range or slice.

Parameters:

di – Element to determine increment size.

Returns:

Current range or slice after advancing.

class qstack.tools.FlexParser(prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=<class 'argparse.HelpFormatter'>, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True, exit_on_error=True)[source]

Bases: ArgumentParser

Argument parser that allows removing arguments.

Parameters:

**kwargs – Arguments passed to ArgumentParser.

remove_argument(arg)[source]

Remove an argument from the parser.

Utility method for customizing parsers by removing unwanted arguments from the pre-configured set. Useful when deriving specialized parsers.

Parameters:

arg (str) – Option destination name.

Output:

Modifies parser in place.

qstack.tools.correct_num_threads()[source]

Set MKL and OpenBLAS thread counts based on SLURM environment.

If running under SLURM, sets MKL_NUM_THREADS and OPENBLAS_NUM_THREADS to match SLURM_CPUS_PER_TASK.

qstack.tools.slice_generator(iterable, inc=<function <lambda>>, i0=0)[source]

Generate slices for elements in an iterable based on increments.

Parameters:
  • iterable (iterable) – Iterable of elements to generate slices for.

  • (callable (inc) – int->int): Function that computes increment size for each element. Defaults to identity function.

  • i0 (int) – Initial starting index. Defaults to 0.

Yields:

tuple – (element, slice) pairs for each element in the iterable.

qstack.tools.unix_time_decorator(func)[source]

Measure and print execution time statistics for a function.

Measures real, user, and system time for the decorated function. Thanks to https://gist.github.com/turicas/5278558

Parameters:

func (callable) – Function to be decorated.

Returns:

Wrapped function that prints timing information.

Return type:

callable

qstack.tools.unix_time_decorator_with_tvalues(func)[source]

Measure execution time statistics and return them along with function result.

Measures real, user, and system time for the decorated function and returns timing dict. Thanks to https://gist.github.com/turicas/5278558

Parameters:

func (callable) – Function to be decorated.

Returns:

Wrapped function that returns (timing_dict, result).

Return type:

callable