-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataAccess.py
More file actions
39 lines (26 loc) · 1.05 KB
/
DataAccess.py
File metadata and controls
39 lines (26 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python3
""" Module to read and write json data from project Alperose. """
from json import load, dump
from logger import logging
from pprint import pformat
class DataAccess:
""" Class that provides read and write functions to access json data. """
def __init__(self, filename):
self._filename = filename
logging.info('Initialize DataAccess object for reading json file %s' %
filename)
def read(self):
""" Read from json file. """
logging.info('Read json data from %s' % self._filename)
with open(self._filename) as jf:
data = load(jf)
# Use pprint to format json data nicely
logging.info('Retrieve json data:')
logging.info(pformat(data))
return data
def write(self, data):
""" Write to json file. """
logging.info('Write data to json file %s' % self._filename)
logging.debug('Data to write: %s' % data)
with open(self._filename, 'w') as jf:
dump(data, jf, sort_keys=True, indent=4)