Skip to content

Commit

Permalink
dump method add
Browse files Browse the repository at this point in the history
  • Loading branch information
josuebrunel committed Jun 7, 2015
1 parent 90ecf7e commit 5918067
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions myql/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,44 @@
from xml.etree import cElementTree as tree

def pretty_json(data):
"""Return a pretty formatted json
"""
data = json.loads(data.decode('utf-8'))
return json.dumps(data, indent=4, sort_keys=True)

def pretty_xml(data):
"""Return a pretty formated xml
"""
parsed_string = minidom.parseString(data.decode('utf-8'))
return parsed_string.toprettyxml(indent='\t', encoding='utf-8')

def prettyfy(response, format='json'):

"""A wrapper for pretty_json and pretty_xml
"""
if format=='json':
return pretty_json(response.content)
else :
return pretty_xml(response.content)

def json_write_data(json_data, filename):
"""Write data in json file
"""
with open(filename, 'w') as fp:
json.dump(json_data, fp, indent=4, sort_keys=True, ensure_ascii=False)
return True
return False

def json_get_data(filename):
"""Get data from json file
"""
with open(filename, 'r') as fp:
json_data = json.load(fp)
return json_data


def dump(response):
"""Print a pretty formatted response content
"""
try:
print(pretty_json(response.content))
except:
prrint(pretty_xml(response.content))

0 comments on commit 5918067

Please sign in to comment.