Instructions
Objective
To complete a Python assignment, you need to write a program that creates a file IO system in Python. This program will allow you to interact with files, both for reading and writing data. File input and output operations are essential in programming as they enable you to store and retrieve information from external sources. By completing this Python assignment, you will gain a deeper understanding of how to handle files, manage data, and effectively utilize file IO in your coding projects.
Requirements and Specifications
Source Code
# **APCO 1P93: Applied Programming (for Data Science)**
### Winter, 2022
### Instructor: Yifeng (Ethan) Li
### Department of Computer Science, Brock University
### Email:
### TA: Tristan Navikevicius:
---
## **Assignment 4**
## **Due Date: 10:00pm, Sunday, March 20th, 2022 **
### **Plagiarism = Severe Consequence **
### **Place your work in a zipped folder named _Firstname_Lastname_StudentNumber_ for your submission. **
## **Question 1** (8 points)
The focus of this assignment is to practice file IO through designing a prototype of a seminar registration system. You will need to define two classes: Seminar and Participant. The skeleton of them are provided below. To be specific, you will need to do the following tasks.
* Complete the missing parts of the `__init__` method to (1) read the data from `abstract.txt` and assign it to `self.abstract` as a string; (2) read the data from `bio.txt` and assign it to `self.bio` as a string, and (3) create a text file named `participants.txt` and add header information to it where the header include four fileds: "name", "department", "position", and "email", seperated by tab (i.e., `\t`). **(1.5 marks)**
* Complete the `save_to_text` method to append the information of a participant to the text file as new line. **(1 mark)**
* Complete the `show_participants_in_text` method to read all the data in the text file and print it on the screen. **(0.5 mark)**
* Complete the `backup_participants` method to save the list of participants, i.e. `self.participants`, to a pickle file. **(1 mark)**
* Complete the `load_participants` method to load the saved data in the pickle file and assign to `self.participants`. **(1 mark)**
* Create an instance of the Seminar class, named `s1` with title being `'Using Mitacs Funding to Support Research and Innovation Collaborations'`, speaker name `'Greg MacNeill'`, abstract given in the attached text file `abstract.txt`, bio given in `bio.txt`, time being `'12:00pm-1:00pm EST, Monday, March 10th, 2022'`. **(0.5 mark)**
* After defining the two classes required above, create an instance of the Participant class, named `p1`, for a participant with name "Chris Howden". You can make up information for other attributes of the object. Add this participant object to the list of participants (i.e., `self.participants`), and add this participant's data into the text file. **(0.5 mark)**
* Create an instance of the Participant class, named `p2`, for a participant with name "Carol Off". You can make up information for other attributes of the object. Add this participant object to the list of participants (i.e., `self.participants`), and add this participant's data into the text file. Through calling defined methods above, print out all the participants' information in the text on the screen, and print out all the participants' information in `self.participants` on the screen. **(0.5 mark)**
* After registering the two participants above, save the list of participant objects to a pickle file. **(0.5 mark)**
* Create an third instance of the Participant class, named `p3`, for a participant with name "Marcia Young". You can make up information for other attributes of the object. Add this participant object to the list of participants (i.e., `self.participants`), and add this participant's data into the text file. Through calling defined methods above, print out all the participants' information in the text on the screen, and print out all the participants' information in `self.participants` on the screen. **(0.5 mark)**
* Finally, through calling defined methods above, load the data from the saved pickle file back to `self.participants` and print them on the screen. **(0.5 marks)**
import pickle
# define your Seminar class here
class Seminar:
def __init__(self, title='', abstract='', speaker='', bio='',
time = '', location='Zoom', filename = './participants.txt'):
self.title = title
# read abstract data from abstract.txt and assign to self.abstract as a string (0.5 mark)
with open('abstract.txt', 'r') as f:
self.abstract = f.read()
self.speaker = speaker
# read bio data from bio.txt and assign to self.bio as a string (0.5 mark)
with open('bio.txt', 'r') as f:
self.bio = f.read()
self.time = time
self.location = location
self.participants = []
self.filename = filename
# create a text file with header: name\tdepartment\tposition\temail (0.5 mark)
fout = open(self.filename, 'w+')
self.fout = fout
header = 'name\tdepartment\tposition\temail\n'
self.fout.write(header)
print(f"Created a text file named {self.filename} with the following header")
print(header)
self.fout.close()
def add_participant(self, par):
"""
Add par, an object of the Participant class, to list self.participants.
INPUTS:
par: object of the Participant class.
"""
self.participants.append(par)
print("Registered a new participant to the list.")
self.save_to_text(par)
def save_to_text(self, par):
"""
Append information in an object of Participant to the text file as a new line.
"""
# 1 mark
self.fout = open(self.filename, 'a')
info = '{0}\t{1}\t{2}\t{3}\n'.format(par.name, par.department, par.position, par.email)
self.fout.write(info)
self.fout.close()
print(f"Added the following info to text file {self.filename}:")
print(info)
def show_participants_in_text(self):
"""
Read the text file of participant data and print it on the screen.
"""
print('Info in the text file:')
# 0.5 mark
with open(self.filename, 'r') as f:
for line in f.readlines():
print(line)
def show_participants_in_list(self):
"""
Print the list of participants on the screen.
"""
print('Info in self.participants:')
header = 'name\tdepartment\tposition\temail\n'
print(header)
for par in self.participants:
info = '{0}\t{1}\t{2}\t{3}\n'.format(par.name, par.department, par.position, par.email)
print(info)
def backup_participants(self, filename = './participants_backup.pkl'):
"""
Save the list of participants, i.e., self.participants, to a pickle file with the given name.
"""
# 1 mark
with open(filename, 'w+') as f:
f.write('mame\tdepartment\tposition\temail\n')
for par in self.participants:
info = '{0}\t{1}\t{2}\t{3}\n'.format(par.name, par.department, par.position, par.email)
f.write(info)
def load_participants(self, filename = './participants_backup.pkl'):
"""
Load the saved list of participants from the specified pickle file and assign it to self.participants.
"""
# 1 mark
# Load participants
# Clear current participants
self.participants.clear()
with open(filename, 'r') as f:
lines = f.readlines()
# Skip header
lines.pop(0)
for line in lines:
# Split
line = line.strip()
row = line.split('\t')
name = row[0]
department = row[1]
position = row[2]
email = row[3]
par = Participant(name, department, position, email)
# Add to list
self.participants.append(par)
class Participant():
def __init__(self, name, department, position, email):
self.name = name
self.department = department
self.position = position
self.email = email
# create an instance, named s1, of the Seminar class (0.5 mark)
s1 = Seminar()
# create an instance, named p1, of the Participant class for Chris Howden (0.5 mark)
p1 = Participant("Chris Howden", "Computer Science", "Student", "choughton@brocku.ca")
s1.add_participant(p1)
# create an instance, named p2, of the Participant class, and related operations (0.5 mark)
p2 = Participant("Carol Off", "Biological Sciences", "Faculty", "coff@brocku.ca")
s1.add_participant(p2)
s1.show_participants_in_text()
s1.show_participants_in_list()
# back up the list of participant objects into a pickle file (0.5 mark)
s1.backup_participants()
# create an instance, named p3, of the Participant class, and do related operations (0.5 mark)
p3 = Participant("Marcia Young", "Health Science", "Staff", "myoung@brocku.ca")
s1.add_participant(p3)
s1.show_participants_in_text()
s1.show_participants_in_list()
# load the saved list of participants from the pickle file and show the participants in the list. (0.5 mark)
s1.load_participants()
s1.show_participants_in_list()
Similar Samples
Browse our extensive selection of programming homework samples covering Java, Python, C++, and more. Each sample showcases our expertise in tackling diverse coding challenges effectively. Whether you need assistance with algorithms, data structures, or software development projects, our samples demonstrate our commitment to delivering high-quality solutions tailored to your academic needs.
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python