I have a dictionary filled with key-object pairs. I want to make the dictionary immutable and I thought the best/easiest way is to cast it to a frozenset but frozenset(dict)
and also tuple(dict)
only stores the keys.
Using frozenset(dict.items())
I seem to get a frozenset with the key-object pairs but I don't know how to retrieve the values/keys.
I have the following code which works, as long as "__obfuscators" is a dictionary
def obfuscate_value(self, key, value):
obfuscator = self.__obfuscators.get(key)
if obfuscator is not None:
return obfuscator.obfuscate_value(value)
else:
return value
I tried this in an attempt to get it working with the frozen set:
def obfuscate_value(self, key, value):
try:
obfuscator = self.__obfuscators[key]
except:
return value
return obfuscator.obfuscate_value(value)
but this gives that frozenset does not have \__getitem__
and self.__obfuscators.__getattribute__(key)
always says it does not have the attribute (because I assume this searches for a function named key)
Is there a better way to make the dictionary immutable or how can I retrieve the object depending on the key?
Edit:
I ended up casting the dict to a tuple using tuple(obfuscator.items())
and then wrote my own find value function:
def find_obfuscator(self, key):
for item in self.__obfuscators:
x, y = item
if self.case_insensitive:
if x.lower() == key.lower():
return y
else:
if x == key:
return y
I would like to thank everyone for their efforts and input.