def _check_sets(result, expected, msg, path, type_): """Compare two sets. This is used to check dictionary keys and sets. Parameters ---------- result : set expected : set msg : str path : tuple type : str The type of an element. For dict we use ``'key'`` and for set we use ``'element'``. """ if result != expected: if result > expected: diff = result - expected msg = 'extra %s in result: %r' % (s(type_, diff), diff) elif result < expected: diff = expected - result msg = 'result is missing %s: %r' % (s(type_, diff), diff) else: in_result = result - expected in_expected = expected - result msg = '%s only in result: %s\n%s only in expected: %s' % ( s(type_, in_result), in_result, s(type_, in_expected), in_expected, ) raise AssertionError( '%ss do not match\n%s%s' % ( type_, _fmt_msg(msg), _fmt_path(path), ), )
def _canonical_key(cls, args, kwargs): extra_dims = cls.extra_dims dimensions_set = set(extra_dims) if not set(kwargs) <= dimensions_set: extra = sorted(set(kwargs) - dimensions_set) raise TypeError( '%s does not have the following %s: %s\n' 'Valid dimensions are: %s' % ( cls.__name__, s('dimension', extra), ', '.join(extra), ', '.join(extra_dims), ), ) if len(args) > len(extra_dims): raise TypeError( '%s has %d extra %s but %d %s given' % ( cls.__name__, len(extra_dims), s('dimension', extra_dims), len(args), plural('was', 'were', args), ), ) missing = object() coords = OrderedDict(zip(extra_dims, repeat(missing))) to_add = dict(zip(extra_dims, args)) coords.update(to_add) added = set(to_add) for key, value in kwargs.items(): if key in added: raise TypeError( '%s got multiple values for dimension %r' % ( cls.__name__, coords, ), ) coords[key] = value added.add(key) missing = {k for k, v in coords.items() if v is missing} if missing: missing = sorted(missing) raise TypeError( 'no coordinate provided to %s for the following %s: %s' % ( cls.__name__, s('dimension', missing), ', '.join(missing), ), ) # validate that all of the provided values exist along their given # dimensions for key, value in coords.items(): if value not in cls.extra_dims[key]: raise ValueError( '%r is not a value along the %s dimension of %s' % ( value, key, cls.__name__, ), ) return coords, tuple(coords.items())