Skip to content

Instantly share code, notes, and snippets.

@p7k
Created May 23, 2018 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save p7k/d82db86e5b58aa0d39f66bc7154cc05b to your computer and use it in GitHub Desktop.
Save p7k/d82db86e5b58aa0d39f66bc7154cc05b to your computer and use it in GitHub Desktop.
xor gate in voluptuous
def xor(fields, group_name):
"""Creates an Exclusive OR (XOR) logical gate voluptuous schema.
see https://github.com/alecthomas/voluptuous/issues/126
:param fields: fields comprising the exclusive group.
:type fields: Dict[str, Schema]
:param group_name: exclusive group identifier.
:type group_name: str
:returns: XOR gated voluptuous schema.
:rtype: Schema
"""
return vl.All(
vl.Any(*(vl.Schema({vl.Required(name): schema}, extra=vl.ALLOW_EXTRA) for name, schema in fields.iteritems())),
vl.Schema({vl.Exclusive(name, group_name): schema for name, schema in fields.iteritems()}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment