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

Mikhail V mikhailwas at gmail.com
Sat Apr 22 22:06:34 EDT 2017


On 23 April 2017 at 02:33, Chris Angelico <rosuav at gmail.com> wrote:
> On Sun, Apr 23, 2017 at 10:19 AM, Mikhail V <mikhailwas at gmail.com> wrote:
>> On 23 April 2017 at 00:48, Chris Angelico <rosuav at gmail.com> wrote:
>>> On Sun, Apr 23, 2017 at 8:30 AM, Mikhail V <mikhailwas at gmail.com> wrote:
>>>> 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.
>>>
>>> Easy.
>>>
>>>>>> input()
>>> This string has "quotes" of 'various' «styles», and \backslashes\ too.
>>> 'This string has "quotes" of \'various\' «styles», and \\backslashes\\ too.'
>>>
>>> The repr of a string does pretty much everything you want. If you want
>>> a nice GUI, you can easily put one together that uses repr() to escape
>>> and ast.literal_eval() to unescape.
>>
>> I am sorry, could you elaborate what have you shown here?
>> So in Python console I can become escaped string, but what
>> commands do you use? I never use Python console actually :/
>
> You type "input()" at the Python console, then type the string you
> want. It will be echoed back in representation form, with everything
> correctly escaped.
>
>> And yes the idea is to have a nice GUI. And the idea is exactly opposite
>> to "everyone let's roll an own tool". Obviously I can spend day
>> or two and create such a tool, e.g. with PyQt.
>> But since the task is very common and quite unambiguos I think it is
>> a good reason for a standard official tool.
>
> Or you could spend two seconds firing up the Python REPL, which has
> all the tools you need right there :)
>

Don't know, all I see is "SyntaxError: invalid syntax" if I paste
there some text.
Try to paste e.g. this:
"ffmpeg -i "D:\VIDEO\exp\intro.mp4" -vf "crop=1280:720:0:40,
scale=640:360" -pix_fmt yuv420p  "D:\ART\0MASTER_UMST\yt_pico.mp4""

But are you joking, right? Even if it worked, how can this be convinient,
e.g. in console one cannot even select and copy paste easily.

Probably one can make a python script which takes clipboard contents
then place the conversion result back to clipboard.
Like:
- copy some text to clipboard
- run the script, which replace the clipboard contents with result
- paste text

I haven't tried that, but even this would be very inconvenient and
limited in comparison
to a GUI utility.


Mikhail



More information about the Python-list mailing list