Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/py/mat3ra/utils/array.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, List, Optional, Union, Sequence
from typing import Any, List, Optional, Sequence, Union

import numpy as np

Expand All @@ -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.
Expand Down
21 changes: 20 additions & 1 deletion src/py/mat3ra/utils/extra/jinja.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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}"
)
Loading