Mapping, with sequence as key, wildcard and subsequence matching

Ben Finney ben+python at benfinney.id.au
Thu Jul 16 07:53:21 CEST 2015


Steven D'Aprano <steve at pearwood.info> writes:

> Sounds like a regular expression. Remember that computer science
> theoretical regular expressions don't just match strings, they can
> apply to any sequence of primitive values.

Good insight, thank you.

> In your case, you only need two special tokens, match-one and
> match-one-or-more (which may be like r'.' and r'.*').

It actually needs “one” and “zero-or-more”. Which does match your
analogous regex ‘.’ and ‘.*’ patterns.

> Otherwise, you can create your own sequence type and simple regex
> engine (you only need two meta-items, [ONE and ZERO-OR-MORE]) to
> perform equality tests.

It may have to come to that. That's not such a problem, but:

> You can't use a dict for the mapping, not unless you're smarter than
> me, due to the requirement to hash the keys.

Dang. It's the mapping that I really need to solve, I think. A mapping
that has a custom “does this candidate match any existing key” and
“return the value for this key” to defer to the matching behaviour
described above.

Are those the ‘__contains__’, ‘__getitem__’ methods? What actually is
the API of a mapping type, that would need to be customised for this
application?

-- 
 \       “If we listen only to those who are like us, we will squander |
  `\   the great opportunity before us: To live together peacefully in |
_o__)            a world of unresolved differences.” —David Weinberger |
Ben Finney



More information about the Python-list mailing list