MCPcopy
hub / github.com/huggingface/transformers / _quote_string

Function _quote_string

src/transformers/testing_utils.py:3959–3978  ·  view source on GitHub ↗

Given a string `s`, return a python literal expression that give `s` when it is used in a python source code. For example, if `s` is the string `abc`, the return value is `"abc"`. We choice double quotes over single quote despite `str(s)` would give `'abc'` instead of `"abc"`.

(s)

Source from the content-addressed store, hash-verified

3957
3958
3959def _quote_string(s):
3960 """Given a string `s`, return a python literal expression that give `s` when it is used in a python source code.
3961
3962 For example, if `s` is the string `abc`, the return value is `"abc"`.
3963
3964 We choice double quotes over single quote despite `str(s)` would give `'abc'` instead of `"abc"`.
3965 """
3966 has_single_quote = "'" in s
3967 has_double_quote = '"' in s
3968
3969 if has_single_quote and has_double_quote:
3970 # replace any double quote by the raw string r'\"'.
3971 s = s.replace('"', r"\"")
3972 return f'"{s}"'
3973 elif has_single_quote:
3974 return f'"{s}"'
3975 elif has_double_quote:
3976 return f"'{s}'"
3977 else:
3978 return f'"{s}"'
3979
3980
3981def _format_py_obj(obj, indent=0, mode="", cache=None, prefix=""):

Callers 1

_format_py_objFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected