Reputation: 3011
I am trying to define a namespace-like object that can have significance parameters. This is useful so that I can implement a hashing function for the object that will only hash members tagged as significant. I would like the API to be as close to the namespace style as possible, but as far as I can think there is no easy way to do something like
n = MyNamespace()
n.a = (1, True)
And have 1
be stored in __dict__
while True
would be stored in some other dictionary that maintains the knowledge that 1
is significant.
I can think of a way of doing it with a setter, like so, but that is a bit clunky and non-pythonic and requires a ugly paired tuple way of direct instantiation.
class SignificantNamespace(object):
def __init__(self, *args):
self.significant = {}
for name, val, significance in args:
setattr(self, name, val)
self.significant[name] = significance
def set(self, name, val, significant=True):
setattr(self, name, val)
self.significant[name] = significant
What do you guys think? Is there a better/cleaner way to do this?
Upvotes: 1
Views: 97
Reputation: 21183
If I understand you correctly you could do something like this:
class MyNamespace(object):
def __hash__(self):
"""Only hash dict key pairs that are significant"""
d_keys = sorted(self.__dict__.keys())
to_hash = []
for k in d_keys:
v = self.__dict__[k]
if v[1]:
to_hash.append((k, v[0]))
return hash(frozenset(to_hash))
def get_signifcant_dict(self):
return {k: v[0] for k, v in self.__dict__.items() if v[1]}
Usage example:
>>> my = MyNamespace()
>>> my.a = (1, True)
>>> my.__hash__()
-2550060783245333914
>>> my.b = (2, False)
>>> my.__hash__()
-2550060783245333914 # hash didn't change since `b` is not signifcant
>>> my.get_signifcant_dict()
{'a': 1}
Upvotes: 1