On Jun 25, 2019, at 08:44, James Lu <[email protected]> wrote:
>
> What if we had reverse format strings, i.e. reading formatted input?
>
> x = readf("{:.0%}", "50%")
> # => x == 0.50
> x = readf("{:.0}", "50")
> # => x == 0.50
>
> Readf takes the repr() of its second argument and “un-formats” the string.
What’s the algorithm for this?
There’s at least possibly an answer for things like C printf, because the
format specifier “%.1f” tells you the type—if you pass anything but a double to
printf with that specifier, it’s undefined behavior, so you can write a scanf
function that knows that “%.1f” has to “unprintf” into a double. But Python
format specifier “.1” tells you nothing about the type—it can be passed a
float, a str, an instance of some arbitrary user-defined type… so what type can
tell you how to “unformat” it?
(By the way, format(50.0, “.1”) gives you “5e+01”, not “50”. And the repr of
the string “50” is the string “‘50’”. Also, surely unformatting returns not a
single value, but as many values as there are format specifiers, right? But
these issues are trivial.)
It might be possible to come up with a solution to this. Maybe you could
explicitly specify the types in the “unformat” specifier, before the colon, and
it calls __unformat__ on that type, which returns the parsed value and the
remainder of the string?That could work for simple cases:
x, = unformat(“{float:.0f}”, “50”)
… or even:
x, op, y = unformat(“{float.0f} {str:.1} {float:.0f}”, “2 * 3”)
But what happens in this case:
x, op, y = unformat(“{float.0f} {str} {float:.0f}”, “2 * 3”)
There’s no way str.__unformat__(“”, “* 3”) can know whether to parse one
character or the whole string or anything in between. Unless you want some
horribly inefficient backtracking scheme, this has to be illegal. (And this is
exactly why people come up with more restricted languages to parse. If you know
the language is regular, you can write a regex to parse it, which actually can
disambiguate cases like this without exponential backtracking.)
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/ABQHFVX5DOK2C2HIYPLKGUTH2MI353AC/
Code of Conduct: http://python.org/psf/codeofconduct/