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

Mikhail V mikhailwas at gmail.com
Sun Apr 23 12:02:57 EDT 2017


On 23 April 2017 at 05:03, MRAB <python at mrabarnett.plus.com> wrote:
> On 2017-04-22 23:30, Mikhail V wrote:
>>
>> 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.
>>
> Here's a very simple tkinter GUI app. It only goes one way (plain to escaped
> (asciified)), but it shows what's possible with very little code.
>
>
> #! python3.6
> # -*- coding: utf-8 -*-
> import tkinter as tk
>
> class App(tk.Tk):
>     def __init__(self):
>         tk.Tk.__init__(self)
>         self.title('Escaper')
>
>         tk.Label(self, text='Plain string').pack()
>
>         self.plain_box = tk.Text(self)
>         self.plain_box.pack()
>         self.plain_box.focus()
>
>         tk.Label(self, text='Escaped string').pack()
>
>         self.escaped_box = tk.Text(self)
>         self.escaped_box.pack()
>
>         self.after(100, self.on_tick)
>
>     def on_tick(self):
>         plain_string = self.plain_box.get('1.0', 'end')[ : -1]
>
>         escaped_string = ascii(plain_string)
>
>         self.escaped_box.delete('1.0', 'end')
>         self.escaped_box.insert('1.0', escaped_string)
>
>         self.after(100, self.on_tick)
>
> App().mainloop()
>

Thank you for sharing! Works fine on Win 7.
So that is what I mean, even such simple app is
uncomparably better that any workarounds and already
covers most cases.
Unfortunately I am not proficient in tkinter so I hope
that will gain some support from other folks and then
could be pinned somewhere and be accessible for
everyone.


Mikhail


More information about the Python-list mailing list