From 1eaeb3e68cd74bbd0dd7692556513b1fd483d1bb Mon Sep 17 00:00:00 2001 From: Wirawan Purwanto Date: Thu, 29 May 2014 13:55:16 -0400 Subject: [PATCH] * Added function: dict_defval and dict_defvals. --- sugar.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/sugar.py b/sugar.py index a9c5e62..cc5ee4e 100644 --- a/sugar.py +++ b/sugar.py @@ -86,6 +86,17 @@ def dict_join(*dicts): rslt.update(d) return rslt +def dict_defval(p, key, val): + """For a dict-like object, sets a default value for the given key, + if that has not been defined in the object.""" + if key not in p: p[key] = val + +def dict_defvals(p, q): + """For a dict-like object, sets multiple default values for the given keys + (q is an input dict containing the defaults).""" + for qk in q: + dict_defval(p, qk, q[qk]) + def list_join(*L): r = [] for i in L: