view tvii/unique.py @ 58:327fd6cb90ed

more links
author Jeff Hammel <k0scist@gmail.com>
date Sun, 08 Oct 2017 13:26:46 -0700
parents 4b20694b8a16
children
line wrap: on
line source

class NotUnique(Exception):
    """exception for a set of items that are not unique"""

    msg_tmpl = "Not unique: {items}"

    def __init__(self, multiples):
        self.multiples = multiples
        message = self.msg_tmpl.format(items=', '.join([str(item)
                                                        for item in multiples]))
        Exception.__init__(self, message)


def unique(items):
    """asserts that a `items` is a set of one"""

    types = set(items)
    unique = types.pop()
    if types:
        types.add(unique)
        raise NotUnique(types)
    return unique