1 class LookupDictionary(dict):
3 a dictionary which can lookup value by key, or keys by value
6 def __init__(self
, items
=[]):
7 """items can be a list of pair_lists or a dictionary"""
8 dict.__init
__(self
, items
)
10 def get_keys_for_value(self
, value
, fail_value
=None):
11 """find the key(s) as a list given a value"""
12 list_result
= [item
[0] for item
in self
.items() if item
[1] == value
]
13 if len(list_result
) > 0:
17 def get_first_key_for_value(self
, value
, fail_value
=None):
18 """return the first key of this dictionary given the value"""
19 list_result
= [item
[0] for item
in self
.items() if item
[1] == value
]
20 if len(list_result
) > 0:
24 def get_value(self
, key
, fail_value
=None):
25 """find the value given a key"""
31 class Enum(LookupDictionary
):
32 def __init__(self
, initial_value
=0, items
=[]):
33 """items can be a list of pair_lists or a dictionary"""
34 LookupDictionary
.__init
__(self
, items
)
35 self
.value
= initial_value
37 def set_value(self
, v
):
38 v_typename
= typeof(v
).__name
__
39 if v_typename
== "str":
47 def get_enum_value(self
):
50 def get_enum_name(self
):
54 s
= self
.get_first_key_for_value(self
.value
, None)
56 s
= "%#8.8x" % self
.value