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
ifiterable
doesn’t contain at least one element;TypeError
ifiterable
isn’t iterable.
-
algebraixlib.util.miscellaneous.
get_variable
(variable_name: str, frames_up: int)[source]¶ Return the variable with name
variable_name
, fromframes_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='')[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 toprint_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.
-
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 calldata_functor
on it. If it is not a string, assume it is a file-like object (with a.write()
function) and calldata_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
(is_function: bool=True)[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.
-
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
isNone
. - skip – Set to
True
to 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_name
isNone
. - 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
-