[Python-ideas] Match statement brainstorm
Paul Moore
p.f.moore at gmail.com
Thu May 26 13:11:21 EDT 2016
On 26 May 2016 at 17:21, Sven R. Kunze <srkunze at mail.de> wrote:
> I think "test one thing against multiple conditions" is a quite abstract thing to do.
>
> I would like to see some real-world use-case for this kind of syntax
Today, I was parsing a job output file. For each line in the file, I
tested it against various patterns/conditions to see what type of line
it was. Depending on the type of line, I did something different. I
used a chain of if/elif statements, because that's what Python
currently (3.5) provides, but it would have been an obvious case for a
match/switch statement. If the important thing here is *structural*
matching then what I actually did was call split() on the line to get
a list of elements, then wanted some to be constants, and I picked
data out of others. Something like
match line_parts:
case _, 'Job', 'Start', start_time: do_something1(start_time)
case _, 'Job', 'End', end_time: do_something2(end_time)
case view, 'size', 'is', kb, 'KB': do_something3(view, kb)
Is that the type of example you had in mind?
Paul
More information about the Python-ideas
mailing list