From 2a30c20a79cf4eee0d0b8efa0de39ed628882b54 Mon Sep 17 00:00:00 2001 From: wirawan Date: Thu, 10 Mar 2011 20:16:58 +0000 Subject: [PATCH] * Added dict_islice to support slicing while ignoring non-existing keys in the source dict. --- sugar.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/sugar.py b/sugar.py index 69412fb..75bf039 100644 --- a/sugar.py +++ b/sugar.py @@ -1,6 +1,6 @@ #!/usr/bin/ipython -pylab # -# $Id: sugar.py,v 1.8 2010-10-25 14:42:50 wirawan Exp $ +# $Id: sugar.py,v 1.9 2011-03-10 20:16:58 wirawan Exp $ # # Created: 20100121 # Wirawan Purwanto @@ -45,14 +45,28 @@ else: #print dir(globals()) def dict_slice(Dict, *keys): - """Returns a shallow copy of the subset of a given dict (or an otherwise - hashable object) with a given set of keys. + """Returns a shallow copy of the subset of a given dict (or a dict-like + object) with a given set of keys. + The return object is a dict. + No keys may be missing from Dict. Example: if d = {'abc': 12, 'def': 7, 'ghi': 32, 'jkl': 98 } then dict_slice(d, 'abc', 'ghi') will yield {'abc': 12, 'ghi': 32 } """ return dict([ (k, Dict[k]) for k in keys ]) +def dict_islice(Dict, *keys): + """Returns a shallow copy of the subset of a given dict (or an otherwise + hashable object) with a given set of keys. + The return object is a dict. + This is similar to dict_slice, except that missing keys in + Dict will be ignored. + """ + # This is fancy but we require Dict to have keys() function: + #return dict([ (k, Dict[k]) for k in (set(keys) & set(Dict.keys())) ]) + # use this one instead, which is milder requirement: + return dict([ (k, Dict[k]) for k in keys if (k in Dict) ]) + def dict_join(*dicts): """Join multiple dicts into one, updating duplicate keys from left-to-right manner.