On Fri, Sep 18, 2020 at 12:16 PM Wes Turner <wes.turner@gmail.com> wrote:
This functionality MUST be accessible with a function or a method. (?)
As already mentioned, building up a pattern string and then using that on the LHS cannot work:
pattern = "{a:d} {b:d}" pattern += " {c:s}" pattern = "12 34 ef"
This is the way Parse works; you call parse() and it returns a Result:
r = parse("The {} who say {}", "The knights who say Ni!") print(r) <Result ('knights', 'Ni!') {}> print(r.fixed) ('knights', 'Ni!')
https://github.com/r1chardj0n3s/parse#result-and-match-objects :
...
Similar functionality in the standard library could return e.g. Union[tuple, namedtuple] and expect users to call namedtuple.asdict() when there are template field names specified; but the parse.Result object does support .spans ("A dictionary mapping the names and fixed position indices matched to a 2-tuple slice range of where the match occurred in the input.").
And I for one do like how the parse.Result object provides pretty much all the information that gets... created?... when a parsing action happens: the parsed value, the name supplied in the template string associated with the value (if any), and also the position in the template string. If any of those were missing in a std lib version, it would feel sort of hobbled, at least to me. So perhaps neither dictionaries nor tuples are a good parse() method result. --- Ricky. "I've never met a Kentucky man who wasn't either thinking about going home or actually going home." - Happy Chandler