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: StopIterationifiterabledoesn’t contain at least one element;TypeErrorifiterableisn’t iterable.
-
algebraixlib.util.miscellaneous.get_variable(variable_name: str, frames_up: int)[source]¶ Return the variable with name
variable_name, fromframes_upframes 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_upframes up.
-
algebraixlib.util.miscellaneous.open_webpage_from_html_str(html: str)[source]¶ Open the HTML content string
htmlin 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_nameand 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_upcan be left at its default of 0.) - skip – Set to
Trueto skip printing the variable. Use to control a number of calls toprint_varwith a single variable. - short – Set to
Trueto print a short version (only name and len if applicable). - max_length – The maximum length of the string to be printed. Set to
Noneif 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_pathis a string, open a file and calldata_functoron it. If it is not a string, assume it is a file-like object (with a.write()function) and calldata_functoron 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:
objectTime 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.
-
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_nameisNone. - skip – Set to
Trueto skip this lap. Defaults toFalse. - short – Print a short lap result without the full content of
variable_name. - max_length – Maximal length of print. Defaults to 10000 characters.
- variable_name – The name of the variable to be printed. Defaults to
-
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_nameisNone. - short – Print a short lap result without the full content of
variable_name. - max_length – Maximal length of print. Defaults to 10000 characters.
- variable_name – The name of the variable to be printed. Defaults to
-
-
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.