Skip to content

Instantly share code, notes, and snippets.

@cpelley
Last active November 17, 2016 10:52
Show Gist options
  • Select an option

  • Save cpelley/6979e2f2032b3c3a58b417359bd16d10 to your computer and use it in GitHub Desktop.

Select an option

Save cpelley/6979e2f2032b3c3a58b417359bd16d10 to your computer and use it in GitHub Desktop.
Json loader
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