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


Groups > comp.lang.python > #3869

suggestions, comments on an "is_subdict" test

Date 2011-04-22 15:55 +0200
Subject suggestions, comments on an "is_subdict" test
From Vlastimil Brom <vlastimil.brom@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.747.1303480525.9059.python-list@python.org> (permalink)

Show all headers | View raw


Hi all,
I'd like to ask for comments or advice on a simple code for testing a
"subdict", i.e. check whether all items of a given dictionary are
present in a reference dictionary.
Sofar I have:

def is_subdict(test_dct, base_dct):
    """Test whether all the items of test_dct are present in base_dct."""
    unique_obj = object()
    for key, value in test_dct.items():
        if not base_dct.get(key, unique_obj) == value:
            return False
    return True

I'd like to ask for possibly more idiomatic solutions, or more obvious
ways to do this. Did I maybe missed some builtin possibility?
I am unsure whether the check  against an unique object() or the
negated comparison are usual.?
(The builtin exceptions are ok, in case anything not dict-like is
passed. A cornercase like >>> is_subdict({}, 4)
>>> True
doesen't seem to be worth a special check just now.)

Thanks in advance for the suggestions,
  regards,
      vbr

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


Thread

suggestions, comments on an "is_subdict" test Vlastimil Brom <vlastimil.brom@gmail.com> - 2011-04-22 15:55 +0200
  Re: suggestions, comments on an "is_subdict" test Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2011-04-22 16:57 +0200
    Re: suggestions, comments on an "is_subdict" test MRAB <python@mrabarnett.plus.com> - 2011-04-22 16:18 +0100
      Re: suggestions, comments on an "is_subdict" test Raymond Hettinger <python@rcn.com> - 2011-04-23 00:23 -0700
    Re: suggestions, comments on an "is_subdict" test Paul Rubin <no.email@nospam.invalid> - 2011-04-23 00:26 -0700

csiph-web