On Jun 28, 2019, at 12:09, Chris Angelico <ros...@gmail.com> wrote:
> 
> 1) For all the different types of object that can be read (integer,
> string, JSON blob, etc), have a function that will read one, stop when
> it's done, and report both the parsed object and the point where it
> stopped parsing.

For a string, what does it mean to “read one”? Does it just munch everything, 
or until the end of the line (whether \n or universal newlines), or until white 
space, or just one character? Whichever one you decide is right is probably 
trivial to implement (value, _, rest = arg.partition('\n')), but unless the 
goal is “exactly what C scanf does” (in which case I’m not sure we need a whole 
protocol-and-wrapper thing), there doesn’t seem to be a TOOWTDI answer here.

Meanwhile, the json module can already do this with the raw decode method 
(although you to have to construct a decoder instance, as it doesn’t have a 
convenience wrapper like loads), and so can lots of other things (even stuff 
like struct.unpack_from), but they mostly have a wide range of inconsistent 
APIs. Maybe just having a consistent “val, rest = parse_one(source_str, type)” 
function that calls a dunder protocol type.__parse_one__(source_str) or 
accesses a registry that each module can add to (and users can customize), or …?

Assuming you have that, then writing an unformat function where you specify the 
types by name is trivial, and just as extensible as format. That seems a lot 
more useful than a function which is like C scanf with a few differences and a 
few extensions (that aren’t the same extensions as, say, ObjC).

Although I suppose there’s no reason you couldn’t do both.

_______________________________________________
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/N73CUHXJQN2XSEC6Q3VDPPQ4FKGFMV63/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to