Skip to content

Commit

Permalink
feat: add allowed_keys to dict validator (#894)
Browse files Browse the repository at this point in the history
* feat: add allowed_keys to dict validator

* fix: remove f-strings and replace d by value
  • Loading branch information
nulinspiratie authored and WilliamHPNielsen committed Dec 4, 2017
1 parent da1b0a4 commit 68f6b83
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions qcodes/utils/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,14 +545,29 @@ class Dict(Validator):
"""
Validator for dictionaries
"""
def __init__(self):
# exists only to overwrite parent class
pass
def __init__(self, allowed_keys=None):
"""
Validator for dictionary keys
Args:
allowed_keys (List): if set, all keys must be in allowed_keys
"""
self.allowed_keys = allowed_keys

def validate(self, value, context=''):
if not isinstance(value, dict):
raise TypeError(
'{} is not a dictionary; {}'.format(repr(value), context))

if self.allowed_keys is not None:
forbidden_keys = [key for key in value if key not in self.allowed_keys]
if forbidden_keys:
raise SyntaxError('Dictionary keys {} are not in allowed keys '
'{}'.format(forbidden_keys,
self.allowed_keys))


def __repr__(self):
return '<Dict>'
if self.allowed_keys is None:
return '<Dict>'
else:
return '<Dict {}>'.format(self.allowed_keys)

0 comments on commit 68f6b83

Please sign in to comment.