Reputation: 60014
I need to create a map from twitter status IDs to their author ID. Obviously, each status has exactly one author.
I expected Python collections to have something like uniqdict
class for which d[key] = value
will raise an exception if the key
already has a value different from value
:
class uniqdict(dict):
def __setitem__(self,key,value):
try:
old = super(uniqdict,self).__getitem__(key)
if old != value:
raise ValueError(self.__class__.__name__,key,old,value)
except KeyError:
super(uniqdict,self).__setitem__(key,value)
Upvotes: 8
Views: 179
Reputation: 203
This looks like a duplicate of Write-once dictionary?
Anyway, I think write-once dictionary is the name you're looking for
Upvotes: 1
Reputation: 9739
I had a similar requirement recently, where I needed to merge two dicts but it was an error if they had any keys in common.
I don't think there is a standard name, so let me propose one: a no-overwrite dictionary.
Or perhaps single-update keys dictionary, but that's a bit ugly.
(The simpler alternative single-update dictionary seems too misleading.)
Upvotes: 0