algebraixlib.util.miscellaneous module

Miscellaneous utility functions and classes.

algebraixlib.util.miscellaneous.get_full_class_name(obj: object) → str[source]

Get the fully qualified name of a class.

Parameters:obj – An object.
Returns:The class name of obj, fully qualified with package and module name(s).
algebraixlib.util.miscellaneous.get_hash(*args) → int[source]

Create a hash of the arguments.

Parameters:args – Any number of arguments.
Returns:A hash of args. The hash is an integer with the width of hashes on the system.
algebraixlib.util.miscellaneous.get_single_iter_elem(iterable)[source]

Get the single element of iterable.

Parameters:iterable – An iterable that is expected to have a single element.
Returns:The single element of iterable. (Assert if there isn’t exactly one element.)
Raise:StopIteration if iterable doesn’t contain at least one element; TypeError if iterable isn’t iterable.
algebraixlib.util.miscellaneous.get_variable(variable_name: str, frames_up: int)[source]

Return the variable with name variable_name, from frames_up frames upwards.

Parameters:
  • variable_name – The name of the variable to retrieve and return.
  • frames_up – The number of call stack frames up (relative to the caller of this function).
Returns:

The variable with the name variable_name, frames_up frames up.

algebraixlib.util.miscellaneous.open_webpage_from_html_str(html: str)[source]

Open the HTML content string html in the system default browser.

algebraixlib.util.miscellaneous.print_var(variable_name, frames_up: int = 0, skip: bool = False, short: bool = False, max_length: int = 10000, append: str = '', indent=None, indent_first=None)[source]

Print a variable, given its name variable_name and its location in the call stack.

Parameters:
  • variable_name – The name of the variable to print.
  • frames_up – The number of call stacks up (relative to the caller of this function) where the variable is located. (If the caller wants to print a local variable, frames_up can be left at its default of 0.)
  • skip – Set to True to skip printing the variable. Use to control a number of calls to print_var with a single variable.
  • short – Set to True to print a short version (only name and len if applicable).
  • max_length – The maximum length of the string to be printed. Set to None if you always want everything.
  • append – A string that is appended to the generated string.
  • indent – Indent all lines with this string. Default is no indent.
  • indent_first – Indent first line with this string. Default is the value of indent.
algebraixlib.util.miscellaneous.write_to_file_or_path(file_or_path, data_functor)[source]

If file_or_path is a string, open a file and call data_functor on it. If it is not a string, assume it is a file-like object (with a .write() function) and call data_functor on it.

Parameters:
  • file_or_path – A string or a file-like object (with a .write() function).
  • data_functor – A function-like object with one argument that is the writer.
class algebraixlib.util.miscellaneous.FunctionTimer[source]

Bases: object

Time a function (and parts of it), with provisions for call hierarchies of functions.

Example code:

from algebraixlib.util.miscellaneous import FunctionTimer

def foo():
    skip_laps = False  # Set to True to skip lap prints.
    timer1 = FunctionTimer()

    var1 = 'something' # Do some work.
    timer1.lap('var1', skip=skip_laps)

    var2 = 'something else' # Do some more work.
    timer1.lap('var2', skip=skip_laps)

    result = 'laboriously calculated' # Do still more work.
    timer1.end('result')

    return result

timer = FunctionTimer(is_function=False)

foo()
timer.lap(desc='after first call')

foo()
timer.lap(desc='after second call')

Store time and caller’s function name, increase indent, print caller’s arguments.

print(*texts)[source]
lap(variable_name: str = None, desc: str = None, skip: bool = False, short: bool = False, max_length: int = 10000)[source]

Print the currently elapsed time and optionally a variable value, properly indented.

Parameters:
  • variable_name – The name of the variable to be printed. Defaults to None.
  • desc – A description for this lap. Only used if variable_name is None.
  • skip – Set to True to skip this lap. Defaults to False.
  • short – Print a short lap result without the full content of variable_name.
  • max_length – Maximal length of print. Defaults to 10000 characters.
end(variable_name: str = None, desc: str = None, short: bool = False, max_length: int = 10000)[source]

Decrease indent, print the elapsed time and optionally a variable value.

Parameters:
  • variable_name – The name of the variable to be printed. Defaults to None.
  • desc – A description for this lap. Only used if variable_name is None.
  • short – Print a short lap result without the full content of variable_name.
  • max_length – Maximal length of print. Defaults to 10000 characters.
get_elapsed_time()[source]

Return the time elapsed since construction in seconds (see __init__()).

static get_indent(additional: int = 0) → str[source]

Return the current indent as string of spaces. :param additional: (Optional) Additional levels of indentation. Defaults to 0.

algebraixlib.util.miscellaneous.core(string: str, begin_len: int = 0, end_len: int = 0) → str[source]

Remove characters in the middle of a string by specifying the length of the beginning of the string and the length of the ending of the string. Ellipses are inserted in place of the core of the string that is being removed.

Parameters:
  • string – The string to process.
  • begin_len – The length, in characters, of the beginning part of the string.
  • end_len – The length, in characters, of the ending part of the string.
class algebraixlib.util.miscellaneous.PerformanceTimer(quiet: bool = False)[source]

Bases: object

end(msg='')[source]
get_elapsed_time()[source]
get_start_time
get_end_time