diff --git a/src/py/mat3ra/utils/array.py b/src/py/mat3ra/utils/array.py index 2766154..339c98a 100644 --- a/src/py/mat3ra/utils/array.py +++ b/src/py/mat3ra/utils/array.py @@ -1,4 +1,4 @@ -from typing import Any, List, Optional, Union, Sequence +from typing import Any, List, Optional, Sequence, Union import numpy as np @@ -18,6 +18,7 @@ def filter_by_slice_or_index_or_indices( def convert_to_array_if_not(array_or_item: Union[List, Any]): return array_or_item if isinstance(array_or_item, list) else [array_or_item] + def jaccard_similarity_for_strings(array_a: Sequence[str], array_b: Sequence[str]) -> float: """ Compute the Jaccard similarity coefficient between two sequences of elements. diff --git a/src/py/mat3ra/utils/extra/jinja.py b/src/py/mat3ra/utils/extra/jinja.py index 186c94b..207e561 100644 --- a/src/py/mat3ra/utils/extra/jinja.py +++ b/src/py/mat3ra/utils/extra/jinja.py @@ -1,6 +1,6 @@ import os -from jinja2 import Environment, FileSystemLoader +from jinja2 import Environment, FileSystemLoader, TemplateError def render_template_file(template_file_path: str, **kwargs): @@ -33,3 +33,22 @@ def render_template_string(template_string: str, **kwargs): env = Environment() template = env.from_string(template_string) return template.render(**kwargs) + + +def render_jinja_with_error_handling(template_string: str, **kwargs): + """ + Renders a given template string with error handling. + + Args: + template_string (str): template string + kwargs: variables passed to the template + + Returns: + str + """ + try: + return render_template_string(template_string, **kwargs) + except TemplateError as e: + return ( + f"Error rendering template: {str(e)}\nTemplate content:\n{template_string}\nTemplate variables:\n{kwargs}" + )