# -*- coding: iso-8859-1 -*-
-# Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
+# Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
#
# Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
# CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
-# version 2.1 of the License.
+# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
#
+## \defgroup diclookup diclookup
+# \{
+# \details Smart dictionary with key/value lookup
+# \}
+
__author__="gboulant"
__date__ ="$21 mai 2010 18:00:23$"
# using named functions or a class
# tested with Python25 by Ene Uran 01/19/2008
+## return the key of dictionary dic given the value
+# \ingroup diclookup
def find_key(dic, val):
"""return the key of dictionary dic given the value"""
return [k for k, v in dic.iteritems() if v == val][0]
+## return the value of dictionary dic given the key
+# \ingroup diclookup
def find_value(dic, key):
"""return the value of dictionary dic given the key"""
return dic[key]
+## a dictionary which can lookup value by key, or keys by value
+# \ingroup diclookup
class Lookup(dict):
"""
a dictionary which can lookup value by key, or keys by value
"""
- def __init__(self, items=[]):
+ ## items can be a list of pair_lists or a dictionary
+ def __init__(self, items=None):
"""items can be a list of pair_lists or a dictionary"""
+ if items is None:
+ items = []
dict.__init__(self, items)
+ ## find the key(s) as a list given a value
def get_keys(self, value):
"""find the key(s) as a list given a value"""
return [item[0] for item in self.items() if item[1] == value]
+ ## find the key associated to the given a value. If several keys exist,
+ # only the first is given. To get the whole list, use get_keys instead.
def get_key(self, value):
"""
find the key associated to the given a value. If several keys exist,
return None
return list[0]
+ ## find the value given a key
def get_value(self, key):
"""find the value given a key"""
return self[key]