-
Notifications
You must be signed in to change notification settings - Fork 0
/
elves.py
46 lines (36 loc) · 1.47 KB
/
elves.py
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
40
41
42
43
44
45
46
# Christmas is coming. Elves are busy producing decorations.
# Each one has a to produce a different amount of items (quota).
# Load data from `elves.json` and create a report `elves.txt`.
# An example report is displayed in `elves_sample.txt`
# Your code here
import json
class Elves:
def __init__(self, config):
self.report_file = 'elves.txt'
self.data = None
self.load(config['file_name'])
def load(self, filename):
try:
with open(filename, 'r') as json_file:
self.data = json.load(json_file)
except IOError as io_error:
print 'Error loading file. {0}'.format(io_error)
except ValueError as val_error:
print 'File decoding error. {0}'.format(val_error)
def write_report(self):
if self.data is None:
return
data_report = []
self.format_elves(data_report)
try:
with open(self.report_file, 'w+') as report:
report.write('\n'.join(data_report))
except IOError as io_error:
print('Data could not be written to file. {error}'.format(error=io_error))
else:
print 'Report was successfully saved to "{file_name}" file'.format(file_name=self.report_file)
def format_elves(self, data_report):
for elf in self.data['elves']:
data_report.append(
'=========\nElf: {elf[name]} {elf[surname]}\nQuota: {elf[quota]}'.format(elf=elf)
)