Skip to content

Instantly share code, notes, and snippets.

@Quidge
Last active May 15, 2018 14:21
Show Gist options
  • Save Quidge/bd6e84147a6bd2880bc3d70b3482ee66 to your computer and use it in GitHub Desktop.
Save Quidge/bd6e84147a6bd2880bc3d70b3482ee66 to your computer and use it in GitHub Desktop.
class ErrorOnDuplicateSet(object):
"""
This class behaves just like a set type collection, but will error if
a duplicate is added to the collection.
"""
__emulates__ = set
def __init__(self):
self.data = set()
@collection.appender
def append(self, item):
try:
assert item not in data
except:
raise
else:
self.data.add(item)
def remove(self, item):
self.data.remove(item)
def __iter__(self):
return iter(self.data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment