Hacker News new | ask | show | jobs
by b_tterc_p 2737 days ago
One thing I do frequently is create an ordered dict where the keys are either regex patterns or lambda functions that return true or false; and the values are some sort of label (let’s say some sort of document type). Then I’ll create a function that will take that ordered dict plus some data, and return the first label that the data conforms to (e.g. regex pattern matches some text input)

This is a handy way of making classification rules for a many stepped process, where certain rules have priority over others. The alternative would be a lengthy set of ifs and elifs which is both ugly and not fun to edit on the fly.