Commit 99a118e7 authored by LammLukas's avatar LammLukas

Unattached write_json and data2dict in exportable class

parent cf6980ef
......@@ -8,6 +8,7 @@ from src.Exceptions import *
from src.Exportable import Exportable
from src.Room import Room
from src.Participant import Participant
import re
class Exam(Exportable):
......@@ -28,7 +29,8 @@ class Exam(Exportable):
def save_exam(self, file):
"""Save current exam to .json file"""
data = self.data2dict()
self.write_json(file, data)
def load_exam(self, file):
"""Load exam from .json file"""
......@@ -84,6 +86,14 @@ class Exam(Exportable):
" Create rooms for exam"
room_str = data[1][indices['rooms']]
room_str = re.split(", <br>", room_str)
for index in range(len(room_str)):
room_str[index] = re.sub('\(.*$', '', room_str[index])
def export_data_RWTHOnline(self):
"""Export data to .csv file for upload to RWTHOnline
......@@ -116,7 +126,7 @@ class Exam(Exportable):
matriculation + ' does not exist in list'
'from RWTHOnline!')
write_csv('./test/rwthExportTest.csv', file_data)
write_csv('./testdata/rwthExportTest.csv', file_data)
def export_rating_list(self, file):
......@@ -13,8 +13,7 @@ class Exportable(object):
def data2dict(self, export_list=[]):
"""Store attributes in dictionary for .json export
"""Store attributes in dictionary for .json export"""
data = {}
for attr_name in self.get_attributes():
attribute = getattr(self, attr_name)
......@@ -32,12 +31,6 @@ class Exportable(object):
data[attr_name] = attribute
return data
def write_json(self, file, export_list=[]):
"""Write object data to .json file"""
data = self.data2dict(export_list)
with open(file, 'w') as outfile:
json.dump(data, outfile, indent=4)
def print_object_info(self):
"""Print information on object (including its attributes) to console"""
print('Object of type: ' + str(type(self)))
......@@ -67,4 +60,10 @@ class Exportable(object):
"""Read object data from .json file"""
with open(file) as infile:
data = json.load(infile)
return data
\ No newline at end of file
return data
def write_json(file, data):
"""Write object data to .json file"""
with open(file, 'w+') as outfile:
json.dump(data, outfile, indent=4)
\ No newline at end of file
......@@ -8,13 +8,14 @@ from src.Exportable import Exportable
class Room(Exportable):
"""Class for objects of type Room"""
def __init__(self, template=None, name=None, num_seats=None, supervisor_in_charge=None,
extra_supervisors=None, assigned_participants=None):
def __init__(self, template=None, name=None, internal_id=None, num_seats=None,
supervisor_in_charge=None, extra_supervisors=None, assigned_participants=None):
"""Constructor of Room object"""
if template is not None:
self.Name = name
self.Internal_ID = internal_id
self.NumSeats = num_seats
if not hasattr(self, 'SupervisorInCharge'):
self.SupervisorInCharge = supervisor_in_charge
......@@ -23,17 +24,15 @@ class Room(Exportable):
if not hasattr(self, 'AssignedParticipants'):
self.AssignedParticipants = assigned_participants
def save_template(self, file):
def save_template(self, file, append=False):
"""Save template for room"""
tmp = Room(name=self.Name, num_seats=str(self.NumSeats))
data = self.data2dict(export_list=['Name', 'Internal_ID', 'NumSeats'])
self.write_json(file, data)
def load_template(self, file):
"""Load template for room"""
data = self.read_json(file)
self.Name = data['Name']
self.NumSeats = data['NumSeats']
self.SupervisorInCharge = ""
self.ExtraSupervisors = []
self.AssignedParticipants = []
#self.Internal_ID = data['Internal_ID']
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment