String escaping utility for Python (was: Rawest raw string literals)

Mikhail V mikhailwas at gmail.com
Sat Apr 22 18:30:41 EDT 2017


On 20 April 2017 at 23:54, MRAB <python at mrabarnett.plus.com> wrote:
> On 2017-04-20 22:03, Mikhail V wrote:
>>
>> On 20 April 2017 at 22:43, Random832 <random832 at fastmail.com> wrote:
>>> [snip]
>>>
>>> The best solution I can think of is to have a text editor designed to
>>> parse a string literal, spawn a nested editor with the unescaped
>>> contents of that string literal, and then re-escape it back to place in
>>> the code. If we had that, then we wouldn't even need raw strings.
>>
>>
>> Yes exactly, it would be cool to have such a satellite app
>> which can escape and unescape strings according to rules.
>> And which can also convert unicode literals to their ascii
>> analogues and back on the fly, this would very useful
>> for programming.
>> Probably it is a good idea to even include such thing
>> in Python package. So it would be a small standalone app
>> running parallel with text editor making it to copy paste strings.
>>
> I'm sure it's possible in, say, Emacs.
>
> The editor that I use (EditPad Pro) can call external tools, so I could:
>
> 1. Select the string literal (easy when it is syntax-aware, so I can select
> all of the literal with 2 keypresses).
>
> 2. Call the external tool (1 keypress), to open, say, a simple tkinter app.
>
> 3. Edit the unescaped text (unescape with ast.literal_eval, re-escape with
> 'ascii').
>
> 4. Close the external tool, and the selection is replaced.

I have done a quick google search and could not find
such utility for Python.

I am very interested in having such utility.
And I think it would be fair that such utility
should be made by the Python team so that
all syntax nuances will be correctly implemented.

The purpose is simple: reduce manual work to escape special
characters in string literals (and escape non-ASCII characters).

Simple usage scenario:
- I have a long command-line string in some text editor.
- Copy this string and paste into the utility edit box
- In the second edit box same string with escaped characters
  appears (i.e tab becomes \t, etc)
- Further, if I edit the text in the second edit box,
  an unescaped string appears in the first box.

Possible toggle options, e.g. :
- 'asciify' non-ascii characters

It could be not only useful to eliminate boilerplate typing,
but also a great way to learn string rules for Python learners.


PS:
Also I remember now about the python-ideas thread
on entering unicode characters with decimals instead of
hex values. It was met somewhat negatively but then it turned out
that in recent Python version it can be done with f-strings.
E.g. a string :

s="абв"
one can write as:
s = f"{1072:c}{1073:c}{1074:c}"
instead of traditional hex:
"\u0430\u0431\u0432"

It was told however this is not normal usage.
Still I find it very helpful, so if this is correct syntax, I'd
personally find such a conversion option also very useful.


Mikhail


More information about the Python-list mailing list