Mercurial > hg > tvii
comparison tvii/unique.py @ 50:4b20694b8a16
add module + test for uniqueness
| author | Jeff Hammel <k0scist@gmail.com> |
|---|---|
| date | Sun, 17 Sep 2017 14:28:36 -0700 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 49:111997fa04d1 | 50:4b20694b8a16 |
|---|---|
| 1 class NotUnique(Exception): | |
| 2 """exception for a set of items that are not unique""" | |
| 3 | |
| 4 msg_tmpl = "Not unique: {items}" | |
| 5 | |
| 6 def __init__(self, multiples): | |
| 7 self.multiples = multiples | |
| 8 message = self.msg_tmpl.format(items=', '.join([str(item) | |
| 9 for item in multiples])) | |
| 10 Exception.__init__(self, message) | |
| 11 | |
| 12 | |
| 13 def unique(items): | |
| 14 """asserts that a `items` is a set of one""" | |
| 15 | |
| 16 types = set(items) | |
| 17 unique = types.pop() | |
| 18 if types: | |
| 19 types.add(unique) | |
| 20 raise NotUnique(types) | |
| 21 return unique |
