Created
May 23, 2018 04:56
-
-
Save p7k/d82db86e5b58aa0d39f66bc7154cc05b to your computer and use it in GitHub Desktop.
xor gate in voluptuous
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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