Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:" ': 0.03; 'subject:test': 0.05; 'dictionary': 0.07; 'builtin': 0.09; 'exceptions': 0.09; 'suggestions,': 0.09; 'value:': 0.09; '>>>': 0.12; 'solutions,': 0.12; 'def': 0.13; 'advance': 0.14; '"""test': 0.16; 'passed.': 0.16; 'possibly': 0.16; 'have:': 0.19; 'maybe': 0.21; 'code': 0.22; 'unsure': 0.23; 'received:209.85.161.46': 0.26; 'received:mail-fx0-f46.google.com': 0.26; 'message- id:@mail.gmail.com': 0.28; 'testing': 0.28; 'thanks': 0.29; 'missed': 0.29; 'received:209.85.161': 0.29; '(the': 0.30; 'seem': 0.30; 'this.': 0.30; 'comparison': 0.31; 'key,': 0.31; 'all,': 0.31; 'to:addr:python-list': 0.32; 'reference': 0.34; 'some': 0.37; 'case': 0.37; 'received:209.85': 0.37; 'received:google.com': 0.38; 'ways': 0.38; 'anything': 0.38; 'ok,': 0.39; 'comments': 0.39; 'to:addr:python.org': 0.39; 'received:209': 0.39; 'i.e.': 0.40; 'header:Received:5': 0.40; 'simple': 0.60; 'unique': 0.63; 'worth': 0.64; 'special': 0.66; 'idiomatic': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:date:message-id:subject:from:to :content-type; bh=xELQQOV7UpWRyrHzjVd8HGSbIt4zVBZJ8hWHNI2O8qI=; b=He19AdfngLoZ8xOXdZSjjceUVGWTPUai5jFdTR87B5tcbirHeUlOLTJuKpmBucraGk oBdHsNGuKGZbwLwo1Ul3u9npUS0tjuqExxiofDHeOAl5TLYGMOq7bpjZklZ3MBgWzetx S7qgWz7NQVuwTJOfrgUekVvzV+L4tY5gVthLc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=glEIzFzYLFQdX+ffIv8B/rwuE8uajmPtuaZJFGgb1AsSYtcSUUnhnTqzRsvH0i3hgT axNAWe4/NTXaTYt4RO83Jzxk9WiNrM2bnZmxS4EGDKB23ZyA3OZr9ODET4LrYByXhBeh gqO3lxX+UtB3U0fD3/zUcwBnDzyyDmnTcXCo4= MIME-Version: 1.0 Date: Fri, 22 Apr 2011 15:55:23 +0200 Subject: suggestions, comments on an "is_subdict" test From: Vlastimil Brom To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 26 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1303480526 news.xs4all.nl 81481 [::ffff:82.94.164.166]:40526 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.stben.net!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3869 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