Safely formats a string with the given keyword arguments. If a keyword is not found in the string, it will be ignored. Args: input_str (str): The string to be formatted. **kwargs: The keyword arguments to be used for formatting. Returns: str: The formatted strin
(input_str: str, **kwargs)
| 9 | |
| 10 | |
| 11 | def safe_format(input_str: str, **kwargs) -> str: |
| 12 | """Safely formats a string with the given keyword arguments. If a keyword |
| 13 | is not found in the string, it will be ignored. |
| 14 | |
| 15 | Args: |
| 16 | input_str (str): The string to be formatted. |
| 17 | **kwargs: The keyword arguments to be used for formatting. |
| 18 | |
| 19 | Returns: |
| 20 | str: The formatted string. |
| 21 | """ |
| 22 | # import re |
| 23 | # segs = [input_str] |
| 24 | # for k, v in kwargs.items(): |
| 25 | # regex = re.compile(f'(?<={{{k}}})(?={{{k}}})|({{{k}}})') |
| 26 | # segs = [regex.split(seg) for seg in segs] |
| 27 | # segs = sum(segs, []) |
| 28 | # replace_dict = {f'{{{k}}}': str(v) for k, v in kwargs.items()} |
| 29 | # segs = [replace_dict.get(seg, seg) for seg in segs] |
| 30 | # output_str = ''.join(segs) |
| 31 | # return output_str |
| 32 | |
| 33 | for k, v in kwargs.items(): |
| 34 | input_str = input_str.replace(f'{{{k}}}', str(v)) |
| 35 | return input_str |
| 36 | |
| 37 | |
| 38 | def get_prompt_hash(dataset_cfg: Union[ConfigDict, List[ConfigDict]]) -> str: |
no test coverage detected