
On Wed, 30 Mar 2022 at 15:11, Stephen J. Turnbull <stephenjturnbull@gmail.com> wrote:
Chris Angelico writes:
[fruit] = sscanf(sample, "%*sfruit:%s\n")
I'm warming to this idea. It does hit the sweet spot of doing exactly what you want -- except when it can't do what you want at all. :-) It's concise and quite powerful, applicable to many common use cases.
It fits nicely between "x,sep,y = str.partition(...)" and a regular expression.
I do have one windowframe of the bikeshed to paint: this is Python, so maybe just "scanf" is a fine name?
Sure, whether it's scanf or sscanf doesn't really matter to me. And - I had to look this up - the converse is referred to in the docs as "printf-style formatting", not sprintf. So that's an argument in favour of "scanf".
The first argument can be any iterable of characters, and if an iterator it would leave the iteration pointer where it left off (eg, beginning of next line in 'sample' above).
Hmm, I'm not really a fan. To be efficient, scanf will need to be able to use core string searching functionality - str.index() is faster than simply iterating over a string and comparing character by character. I don't think, for instance, that json.load() promises anything about where it leaves an iterable; in fact, I believe it simply reads everything into a string and then parses that. It would be worth supporting both byte strings and text strings, though, for the same reason that they both support printf formatting. ChrisA