So my idea for this program was to take data from a file and compiling it into a schedule type thing.... Comments? Again...for learning purposes, I'd love to see suggestions for content, efficiency, and techniques.
# Get data from schedules.txt
import string
f = open("schedules.txt","r")
schedule_rawdata = f.readlines()
f.close()
# Split data into list items
schedule_data = []
for line in schedule_rawdata:
schedule_data.append(string.split(line,","))
# strip newlines
for line in schedule_data:
del line[-1]
# Student Class
class Student(object):
'''Student'''
def __init__(self, name, age, grade):
self.name = name
self.age = age
self.grade = grade
self.schedule = {}
schedule_formatted = ""
self.address = ""
self.city = ""
self.state = ""
self.zip = ""
def __compile_schedule(self):
'''Used for print_schedule()'''
classes_temp = "" # Classes formatted for showing the schedule
period_list = ['1A','2A','3A','4A','5A','1B','2B','3B','4B','5B']
for index in range(5):
period = period_list[index]
classes_temp = classes_temp + "| %-3s | %-33s|\n" % (period,self.schedule['A'][index])
classes_temp = classes_temp + "|" + "-"*40 + "|\n"
for index in range(5):
period = period_list[index+5]
classes_temp = classes_temp + "| %-3s | %-33s|\n" % (period,self.schedule['B'][index])
classes_final = classes_temp[:-1]
return classes_final
def get_schedule(self):
if self.schedule['A']:
schedule_formatted = self.__compile_schedule()
self.schedule_formatted = schedule_formatted
else:
self.per1a = raw_input("Period 1A class: ")
self.per2a = raw_input("Period 2A class: ")
self.per3a = raw_input("Period 3A class: ")
self.per4a = raw_input("Period 4A class: ")
self.per5a = raw_input("Period 5A class: ")
self.per1b = raw_input("Period 1B class: ")
self.per2b = raw_input("Period 2B class: ")
self.per3b = raw_input("Period 3B class: ")
self.per4b = raw_input("Period 4B class: ")
self.per5b = raw_input("Period 5B class: ")
self.schedule = {'1a': self.per1a, '2a': self.per2a, '3a': self.per3a,'4a': self.per4a, '5a': self.per5a, '1b': self.per1b, '2b': self.per2b,'3b': self.per3b,'4b': self.per4b, '5b': self.per5b}
def print_schedule(self):
name = "Name: " + self.name
age = "Age: " + self.age
grade = "Grade: " + self.grade
address = "Address :"
city_state_zip = self.city + ", " + self.state + " " + self.zip
print \
'''
%-23s %s
%-23s %s
%-23s %s
==========================================
| Per | Class |
------------------------------------------
%s
==========================================
''' % (name,address,age,self.address,grade,city_state_zip,self.schedule_formatted)
def add_address(self):
try:
self.address = schedule_data[0][3]
except IndexError:
pass
try:
self.city = schedule_data[0][4]
except IndexError:
pass
try:
self.state = schedule_data[0][5]
except IndexError:
pass
try:
self.zip = schedule_data[0][6]
except IndexError:
pass
# Declare people from file
person1 = Student(schedule_data[0][0], schedule_data[0][1], schedule_data[0][2])
person1.schedule = {'A':schedule_data[1],'B':schedule_data[2]}
add_address(person1)
person1.get_schedule()
person1.print_schedule()
text file:
Mouche,18,12,1111 SW 1st PL,Dayton,OH,45390,
Underwater Basket Weaving,Concert Band,Lunch,Release,Release,
PE1,PE2,Team Sports,Lunch,Study Hall,
I haven't written it so that it accepts multiple users yet and detects how many people are in the data file. Suggestions for that?
As always, thanks for your time.