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)
| 3957 | |
| 3958 | |
| 3959 | def _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 | |
| 3981 | def _format_py_obj(obj, indent=0, mode="", cache=None, prefix=""): |