Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'value,': 0.03; 'raises': 0.07; 'strings.': 0.07; 'trailing': 0.07; 'unittest': 0.07; 'implemented,': 0.09; 'length.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'specifying': 0.09; 'python': 0.10; 'itself.': 0.11; 'argument': 0.15; 'value.': 0.15; '*only*': 0.16; 'arbitrarily': 0.16; 'keyerror': 0.16; 'mapping,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sentinel': 0.16; 'sequence,': 0.16; 'subject:Mapping': 0.16; 'subject:key': 0.16; 'which,': 0.16; 'exists': 0.18; 'skip:` 10': 0.18; 'all,': 0.20; 'not,': 0.22; 'keys': 0.22; 'import': 0.24; 'feature': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; '(e.g.': 0.27; 'sequence': 0.27; 'specify': 0.27; 'this.': 0.28; 'does,': 0.29; 'use?': 0.29; "i'm": 0.30; 'position.': 0.30; "i'd": 0.31; 'candidate': 0.31; 'implement': 0.32; 'problem': 0.33; 'items.': 0.33; 'trouble': 0.35; 'text': 0.35; 'mapping': 0.35; 'item': 0.35; 'but': 0.36; 'too': 0.36; 'should': 0.36; '(and': 0.36; 'to:addr:python-list': 0.36; 'received:org': 0.37; 'end': 0.39; 'data': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'some': 0.40; 'firm': 0.60; 'ever': 0.60; 'different': 0.63; 'hoping': 0.77; '_o__)': 0.84; 'received:125': 0.84; 'responses': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Mapping, with sequence as key, wildcard and subsequence matching Date: Thu, 16 Jul 2015 11:51:07 +1000 Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) Cancel-Lock: sha1:2Eu7jn//GipNylyQKBvd+CSDTOQ= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 72 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1437011478 news.xs4all.nl 2833 [2001:888:2000:d::a6]:37196 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:93889 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