Last active
March 16, 2018 14:37
-
-
Save Avlyssna/6163eb17cc4deb0944909f2e028192dd to your computer and use it in GitHub Desktop.
This file contains 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
# Standard library imports | |
import logging | |
import json | |
def load_document(string): | |
document = None | |
try: | |
document = json.loads(string) | |
except: | |
logging.warning('Invalid JSON string supplied!') | |
if document: | |
if isinstance(document, dict): | |
meta = document.get('meta') | |
if not meta or isinstance(meta, dict): | |
return document | |
else: | |
logging.warning('Invalid "meta" supplied!') | |
else: | |
logging.warning('Invalid document supplied!') | |
class Document: | |
def __repr__(self): | |
return json.dumps(self.__dict__) | |
class DataDocument(Document): | |
def __init__(self, data, meta={}): | |
self.data = data | |
if meta: | |
self.meta = meta | |
@classmethod | |
def from_string(this, string): | |
document = load_document(string) | |
if document: | |
data = document.get('data') | |
if isinstance(data, dict) or isinstance(data, list): | |
return this(data, document.get('meta')) | |
else: | |
logging.warning('Invalid "data" supplied!') | |
class ErrorDocument(Document): | |
def __init__(self, errors, meta={}): | |
self.errors = errors | |
if meta: | |
self.meta = meta | |
@classmethod | |
def from_string(this, string): | |
document = load_document(string) | |
if document: | |
errors = document.get('errors') | |
if isinstance(errors, dict) or isinstance(errors, list): | |
return this(errors, document.get('meta')) | |
else: | |
logging.warning('Invalid "errors" supplied!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment