Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #93889

Mapping, with sequence as key, wildcard and subsequence matching

From Ben Finney <ben+python@benfinney.id.au>
Subject Mapping, with sequence as key, wildcard and subsequence matching
Date 2015-07-16 11:51 +1000
Newsgroups comp.lang.python
Message-ID <mailman.552.1437011478.3674.python-list@python.org> (permalink)

Show all headers | View raw


Howdy all,

What well-defined data type exists with the following properties:

* Mapping, key → value.

* Each key is a sequence (e.g. `tuple`) of items such as text strings.

* Items in a key may be the sentinel `ANY` value, which will match any
  value at that position.

* A key may specify that it will match *only* sequences of the same
  length.

* A key may specify that it will match sequences with arbitrarily many
  additional unspecified items.

If it matters: I'm using Python 2 to implement this.


The “match any item” type is simple enough; I'm aware of the
`unittest.mock.ANY` sentinel, and that meets my needs.

What I'm hoping to find is a general, existing, solution to the problem
of a mapping that uses keys which, in their value, encapsulate the fact
that the key does, or does not, match sequences of arbitrary length
longer than itself.

Example of what I'd like::

    from unittest import mock

    responses = SubsequenceMatchingType([
            ('foo', 'bar', 'baz'):
                "Simple three-argument command.",
            ('wibble', mock.ANY, 'wubble'):
                "Second argument ANY.",
            ('lorem', 'ipsum'):
                "Arbitrary trailing arguments.",
            ])

    responses[('foo', 'bar')]  # raises KeyError
    responses[('foo', 'bar', 'baz')]  # → "Simple three-argument command."
    responses[('foo', 'bar', 'baz', 'bork')]  # raises KeyError

    responses[('wibble', 'wobble')]  # raises KeyError
    responses[('wibble', 'wobble', 'wubble')]  # → "Second argument ANY."
    responses[('wibble', 'wobble', 'baz')]  # raises KeyError
    responses[('wibble', 'wobble', 'wubble', 'baz')]  # raises KeyError

    responses[('lorem',)]  # raises KeyError
    responses[('lorem', 'ipsum')]  # → "Arbitrary trailing arguments."
    responses[('lorem', 'ipsum', 'dolor', 'sit', 'amet')]  # → "Arbitrary trailing arguments."

So some kind of distinguishing feature needs to be added to those keys
which should match arbitrary trailing items: not ‘('foo', 'bar', 'baz')’
but ‘('lorem', 'ipsum')’.

The trouble I'm having is that the keys in the mapping, and the
candidate sequences, are simple tuples. Adding a sentinel item at the
end would make it a different sequence, and indistinguishable from
specifying an additional item at that position.

Is this a problem already solved (and implemented, and debugged!) in a
general form that I can use?

-- 
 \      “I am too firm in my consciousness of the marvelous to be ever |
  `\       fascinated by the mere supernatural …” —Joseph Conrad, _The |
_o__)                                                     Shadow-Line_ |
Ben Finney

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Mapping, with sequence as key, wildcard and subsequence matching Ben Finney <ben+python@benfinney.id.au> - 2015-07-16 11:51 +1000
  Re: Mapping, with sequence as key, wildcard and subsequence matching Steven D'Aprano <steve@pearwood.info> - 2015-07-16 14:01 +1000
    Re: Mapping, with sequence as key, wildcard and subsequence matching Ben Finney <ben+python@benfinney.id.au> - 2015-07-16 15:53 +1000
    Re: Mapping, with sequence as key, wildcard and subsequence matching Ethan Furman <ethan@stoneleaf.us> - 2015-07-15 23:31 -0700
    Re: Mapping, with sequence as key, wildcard and subsequence matching Ben Finney <ben+python@benfinney.id.au> - 2015-07-16 16:37 +1000
    Re: Mapping, with sequence as key, wildcard and subsequence matching Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-07-16 08:09 +0100
    Re: Mapping, with sequence as key, wildcard and subsequence matching Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-07-16 08:24 +0100
  Re: Mapping, with sequence as key, wildcard and subsequence matching Marko Rauhamaa <marko@pacujo.net> - 2015-07-16 09:53 +0300

csiph-web