Please change the subject line when you change the topic of
conversation. Or even better, since this isn't strongly related to
what you're replying to, and you quoted no text whatsoever - just make
it a brand new post. Thanks!

On Thu, Jun 27, 2019 at 5:06 AM James Lu <jam...@gmail.com> 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 you're talking about sounds like a scanf sort of thing. In C,
printf and scanf are approximate counterparts, as are sprintf and
sscanf, which work directly with strings (compare json.load and
json.loads, or json.dump and json.dumps). The way sscanf works is that
every marker clearly defines the type of value it can parse:

%d - one integer, decimal
%f - floating-point value in decimal
%s - string
etc, etc

Trying to form a parallel to the way Python's .format() method works
is a little tricky, because format() starts with the object it's
formatting, and then says "hey, format yourself, kay?". So it may be
best to abandon that and instead stick with the well-known sscanf
notation.

The main advantage of sscanf over a regular expression is that it
performs a single left-to-right pass over the format string and the
target string simultaneously, with no backtracking. (This is also its
main DISadvantage compared to a regular expression.) A tiny amount of
look-ahead in the format string is the sole exception (for instance,
format string "%s$%d" would collect a string up until it finds a
dollar sign, which would otherwise have to be written "%[^$]$%d").
There is significant value in having an extremely simple parsing tool
available; the question is, is it worth complicating matters with yet
another way to parse strings? (We still have fewer ways to parse than
ways to format strings. I think.)

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/IZLZAS33HISBT3XGTFUXJRMLT6YPZCJH/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to