Last active
November 17, 2016 10:52
-
-
Save cpelley/6979e2f2032b3c3a58b417359bd16d10 to your computer and use it in GitHub Desktop.
Json loader
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
| import json | |
| class JLoader(object): | |
| def __init__(required=None): | |
| self._required = required | |
| def _check_arguments(self, keys): | |
| keys = set(keys) | |
| if required: | |
| diff = set([req in keys for req in required]) - keys | |
| if diff: | |
| raise KeyError('Keys: {} missing from file'.format(diff)) | |
| def load(self, filename): | |
| with open(filename, 'r') as fh: | |
| dic = json.load(fh) | |
| self._check_arguments(dic.keys()) | |
| return dic |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment