From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-commits+bounces-627470-garchives=archives.gentoo.org@lists.gentoo.org>
Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80])
	by finch.gentoo.org (Postfix) with ESMTP id B9B85138202
	for <garchives@archives.gentoo.org>; Thu, 12 Sep 2013 16:36:50 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id F172DE0D55;
	Thu, 12 Sep 2013 16:36:43 +0000 (UTC)
Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183])
	(using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by pigeon.gentoo.org (Postfix) with ESMTPS id 5FD13E0D56
	for <gentoo-commits@lists.gentoo.org>; Thu, 12 Sep 2013 16:36:43 +0000 (UTC)
Received: from hornbill.gentoo.org (hornbill.gentoo.org [94.100.119.163])
	(using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id 668FF33EC48
	for <gentoo-commits@lists.gentoo.org>; Thu, 12 Sep 2013 16:36:42 +0000 (UTC)
Received: from localhost.localdomain (localhost [127.0.0.1])
	by hornbill.gentoo.org (Postfix) with ESMTP id 1C50AE546A
	for <gentoo-commits@lists.gentoo.org>; Thu, 12 Sep 2013 16:36:40 +0000 (UTC)
From: "André Erdmann" <dywi@mailerd.de>
To: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: 8bit
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "André Erdmann" <dywi@mailerd.de>
Message-ID: <1379003415.2cc79dc0280b25cfabdd8c5166a35f61ff17935c.dywi@gentoo>
Subject: [gentoo-commits] proj/R_overlay:master commit in: roverlay/util/
X-VCS-Repository: proj/R_overlay
X-VCS-Files: roverlay/util/dictwalk.py
X-VCS-Directories: roverlay/util/
X-VCS-Committer: dywi
X-VCS-Committer-Name: André Erdmann
X-VCS-Revision: 2cc79dc0280b25cfabdd8c5166a35f61ff17935c
X-VCS-Branch: master
Date: Thu, 12 Sep 2013 16:36:40 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
X-Archives-Salt: 5d7bebe2-9615-4a2b-9313-cad94bcbd461
X-Archives-Hash: 1e46c92cf3bab5787aff7b4cd55c7fea

commit:     2cc79dc0280b25cfabdd8c5166a35f61ff17935c
Author:     André Erdmann <dywi <AT> mailerd <DOT> de>
AuthorDate: Thu Sep 12 16:30:15 2013 +0000
Commit:     André Erdmann <dywi <AT> mailerd <DOT> de>
CommitDate: Thu Sep 12 16:30:15 2013 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=2cc79dc0

roverlay/util/dictwalk: dictmerge()

Convert an iterable of iterables with non-unique keys into a dict with one list
per key.

E.g.
[ ( "a", 1, 2 ), ( "a", 2, 3 ), ( "b", 1, 2 ) ]
-> dict( "a": [(1,2),(2,3)], "b": [(1,2)] )

---
 roverlay/util/dictwalk.py | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/roverlay/util/dictwalk.py b/roverlay/util/dictwalk.py
index 52a8c61..0813d11 100644
--- a/roverlay/util/dictwalk.py
+++ b/roverlay/util/dictwalk.py
@@ -7,6 +7,27 @@
 import roverlay.util.namespace
 import roverlay.util.objects
 
+def dictmerge_inplace ( d, iterable, get_key=None, get_value=None ):
+   keyfunc = ( lambda x: x[0]  ) if get_key   is None else get_key
+   valfunc = ( lambda x: x[1:] ) if get_value is None else get_value
+
+   for item in iterable:
+      key = keyfunc ( item )
+      val = valfunc ( item )
+      entry = d.get ( key )
+      if entry is None:
+         d[key] = [ val ]
+      else:
+         #assert isinstance ( entry, list )
+         entry.append ( val )
+# --- end of dictmerge_inplace (...) ---
+
+def dictmerge ( iterable, dict_cls=dict, get_key=None, get_value=None ):
+   ret = dict_cls()
+   dictmerge_inplace ( ret, iterable, get_key=get_key, get_value=get_value )
+   return ret
+# --- end of dictmerge (...) ---
+
 
 def dictwalk_create_parent_v ( root, path, dict_create=None, cautious=True ):
    """Creates a dict tree structure using keys the given path. The last path