utils¶
General utilities for UXsim.
- exception uxsim.utils.LoggingWarning[source]¶
This warns that when vehicle_logging_timestep_interval is not 1 but called vehicle logging-related functions.
- uxsim.utils.catch_exceptions_and_warn(warning_msg='')[source]¶
A decorator that catches exceptions in the decorated function and raises a warning with the specified message.
- Parameters:
warning_msg (str, optional) – A custom message to be displayed along with the exception details. Default is an empty string.
- Returns:
A decorator that wraps the decorated function to catch exceptions and raise warnings.
- Return type:
decorator
Notes
When an exception occurs in the decorated function, the decorator will raise a warning that includes: - The name of the function. - A custom message, if provided. - The type of the exception. - The exception message. - The filename and line number where the decorated function was called.
This decorator is for inessential functions where random exceptions are expected (especially due to file I/O or something), but you want to avoid crashing the entire program and instead receive a warning. Mainly written by ChatGPT.
- uxsim.utils.display_image_in_notebook(image_path)[source]¶
Display an image in Jupyter Notebook.
- Parameters:
image_path (str) – The path to the image file to display.
- uxsim.utils.get_font_for_matplotlib(fname=None)[source]¶
Get a multi-language (currently Japanese only) font for matplotlib. TODO: check if it works on different OS. It is only checked on Japanese Windows. TODO: explore if it can be extended for other languages.
- Returns:
The name of a Japanese font that is available on the system. If no Japanese font is found, “monospace” is returned.
- Return type:
str
- uxsim.utils.lange(l)[source]¶
Super lazy function for abbreviating range(len(l))
- Parameters:
l (list)
- uxsim.utils.print_columns(*lists)[source]¶
Convinient function to check contents of 1d lists. For debug.
- uxsim.utils.printtry(*args, **kwargs)[source]¶
Print messages safely, evaluating any callable arguments and catching exceptions. Mainly written by GPT-4
- Parameters:
*args (any) – The contents to print wrapped by lambda. E.g., lambda: (x, y, z)
**kwargs (any) – Arbitrary keyword arguments. These are passed directly to the built-in print function.
Examples
>>> printtry(lambda: (mylist[100], mylist[1000], mylist[10000])) >>> printtry(lambda: (mylist[100], mylist[1000], mylist[10000]), sep=";", end="!")