[issue44308] Raw Strings lack parody

2021-06-04 Thread STINNER Victor


STINNER Victor  added the comment:

I close the issue. Glad that you found a solution to your issue.

--
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44308] Raw Strings lack parody

2021-06-04 Thread Nicholas Willhite


Nicholas Willhite  added the comment:

Wow, thanks for all the helpful responses! I see how this was a classic PEBKAC 
issue on my end. 

Please closed this ticket at your convenience.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44308] Raw Strings lack parody

2021-06-04 Thread Mark Dickinson


Mark Dickinson  added the comment:

Ah, I think I see: you want a function that turns the string "foo\bar" into 
"foo\\bar". Even if this were a good idea, I don't think it's feasible to do it 
in a non-surprising way.

For example, given such a function f, what outputs would you expect for:

  (a) f("\012"), and
  (b) f("\n")?

(And yes, this is a trick question: "\012" and "\n" are the same string.)

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44308] Raw Strings lack parody

2021-06-04 Thread Mark Dickinson


Mark Dickinson  added the comment:

Sorry, I missed the definition of f in the last message. Trying again:

>>> def f(x): return x
... 
>>> f(r'foo\bar') == 'foo\\bar'
True

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44308] Raw Strings lack parody

2021-06-04 Thread Mark Dickinson


Mark Dickinson  added the comment:

> But there's no builtin function for r'foo\bar' that gives you 'foo\\bar'.

I'm confused about what's being requested here. r'foo\bar' and 'foo\\bar' are 
different source code representations of the exact same string (same type, same 
contents), so the identity function is such a function.

>>> f(r'foo\bar') == 'foo\\bar'
True

Nicholas: presumably you're after something more than the identity function. 
Can you clarify what the input and output types to your proposed function would 
be, and then give example input and output values?

--
nosy: +mark.dickinson

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44308] Raw Strings lack parody

2021-06-04 Thread STINNER Victor


STINNER Victor  added the comment:

You can use br"\n" to get 2 bytes: b"\\" and b"n".

IMO it's the best practice, to use raw strings for regular expressions.

Converting a regular string to a raw string sounds like a bad idea.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44308] Raw Strings lack parody

2021-06-03 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

Remember that backslash escapes are only a Python syntactic feature. If you 
read data from a file, or from the input() builtin, that contains a backslash, 
it remains a backslash:

>>> s = input()
a\b
>>> print(len(s), s == r'a\b')
3 True

Backslashes are only special in two cases: as source code, and when displaying 
a string (or bytes) using `repr`.

So if you get a regex from the user, say by reading it from a file, or from 
stdin, or from a text field in a GUI, etc. and that regex contains a backslash, 
your string will contain a backslash and you don't need anything special.

Does this solve your problem?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44308] Raw Strings lack parody

2021-06-03 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

I think you have missed something important here:

>>> data = b'foo\bar'
>>> len(data)
6
>>> print(data)
b'foo\x08ar'


If you want bytes including a backslash followed by a b, you need to use raw 
bytes rb'foo\bar' or escape the backslash.

Also Python 3.8 is in feature-freeze so the earliest this new feature could be 
added to the language is now 3.11.

--
nosy: +steven.daprano
type: behavior -> enhancement
versions: +Python 3.11 -Python 3.8

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44308] Raw Strings lack parody

2021-06-03 Thread Nicholas Willhite


New submission from Nicholas Willhite :

I'm really sure this isn't filed correctly. I'm a total noob to this process, 
so feel free to redirect me. :) 

Bytes can be defined as a function, or a prefixed series. You can prefix a 
series with "b" and get the expected data type. You can also use the builtin 
functions "bytes" to get the same structure:

  bytes('foo\bar', 'utf-8') == b'foo\bar'
  True

But there's no builtin function for r'foo\bar' that gives you 'foo\\bar'.

This would be really handy for applications that accept a regular expression. 
If that regex was part of the source code, I'd just r'foo\bar' to get the 
expected string. Being able to accept something like bytes and do:

  data = b'foo\bar'
  raw_string(data)
  'foo\\bar'

would be really useful for applications that accept a regex as input. 

Is there an obvious way to do this that I'm not seeing? Has my google-foo 
failed me? Feels like a function that should exist in the stdlib.

Again, really sure I'm not "doing this correctly." So please direct me! :) 

Appreciative,
-Nick Willhite

--
components: Unicode
messages: 395064
nosy: Nicholas Willhite, ezio.melotti, vstinner
priority: normal
severity: normal
status: open
title: Raw Strings lack parody
type: behavior
versions: Python 3.8

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com