I have posted homework 9 here.
It is due this coming Sunday at 11:59 PM.
Let's look at the answers to Quiz 7
>>> from time_2 import Time >>> t1 = Time("14:35:12") >>> t1.seconds 52512 t1.format_time() '2:35:12 PM'
>>> t1.seconds = 1000000000 >>> t1.format_time() '277765:46:40 PM'
ti = = Time("09:45:00")
print(t1)
>>> from time_3 import Time >>> t = Time("09:30:00") >>> print(t) <time_3.Time object at 0x1013e8358>
def __str__(self): hours = self.seconds // (60 * 60) remainder = self.seconds % (60 * 60) minutes = remainder // 60 seconds = remainder % 60 return str(hours) + " hours " + str(minutes)+ " minutes " + str(seconds) + " seconds
def __init__(self, manufacturer, model, serial_number, processor, ram, hostname, disk_size): self.__manufacturer = manufacturer self.__model = model self.__serial_number = serial_number self.__processor = processor self.__ram = ram self.__hostname = hostname self.__disk_size = disk_size
def get_manufacturer(self): return self.__manufacturer def get_model(self): return self.__model def get_serial_number(self): return self.__serial_number def get_processor(self): return self.__processor def get_ram(self): return self.__ram def get_hostname(self): return self.__hostname def get_disk_size(self): return self.__disk_size
def set_ram(self, ram): self.__ram = ram def set_hostname(self, hostname): self.__hostname = hostname def set_disk_size(self, disk_size): self.__disk_size = disk_siz
isinstance
isinstance
function takes two arguments
True
if the object is an instance of the class
>>> isinstance("14:35:12", str) True
if not isinstance(time_string, str): raise TypeError("Time constructor expects a string argument")
# returns true if the string has the form HH:MM:SS def has_valid_format(self,s): pattern = re.compile(r"\d\d:\d\d:\d\d") match = pattern.match(s) return match
True
None
False
in an if
statement # returns true if the numbers for hours, minutes and seconds # are within the proper range def date_numbers_in_range(self, hours, minutes, seconds): if not 0 <= hours <= 23: return False if not 0 <= minutes <= 59: return False if not 0 <= seconds <= 59: return False return True
# expects a string of the form HH:MM:SS # where hours are expressed in numbers from 0 to 23 def __init__(self, time_string): if not isinstance(time_string, str): raise TypeError("Time constructor expects a string argument") if not self.has_valid_format(time_string): raise ValueError("String must have the format HH:MM:SS") hours, minutes, seconds = tuple(time_string.split(":")) hours = int(hours) minutes = int(minutes) seconds = int(seconds) if not self.date_numbers_in_range(hours, minutes, seconds): raise ValueError("One of the time values is not in the proper range") self.__seconds = hours * 60 * 60 + minutes * 60 + seconds
def __init__(self, id, last_name, first_name): self.__id = id self.__last_name = last_name self.__first_name = first_name self.__preferred_name = "" self.__phonetic_name = "" self.__email = "" self.__unix_username = ""
def get_id(self): return self.__id def get_last_name(self): return self.__last_name def get_first_name(self): return self.__first_name def get_preferred_name(self): return self.__preferred_name def get_phonetic_name(self): return self.__phonetic_name def get_email(self): return self.__email def get_unix_username(self): return self.__unix_username
def set_preferred_name(self, preferred_name): self.__preferred_name = preferred_name def set_phonetic_name(self, phonetic_name): self.__phonetic_name = phonetic_name def set_email(self, email): self.__email = email def set_unix_username(self, unix_username): self.__unix_username = unix_username
def __str__(self): return self.__first_name + " " + self.__last_name + ", " + self.__id
>>> from student import Student >>> student = Student("12345", "Smith", "William") >>> print(student) William Smith, 12345
def get_full_name(self): if self.__preferred_name: return self.__preferred_name + " " + self.__last_name else: return self.__first_name + " " + self.__last_name
>>> student.get_full_name() 'William Smith' >>> student.set_preferred_name("Bill") >>> student.get_full_name() 'Bill Smith'
def get_sort_name(self): if self.__preferred_name: return self.__last_name + ", " + self.__preferred_name else: return self.__last_name + ", " + self.__first_name
>>> student.get_sort_name() 'Smith, Bill'
STUDENT_ID,LAST_NAME,FIRST_NAME
from student import Student students = {} data_filename = input("Data filename: ") data_file = open(data_filename, "r")
for line in data_file: line = line.rstrip() id, last_name, first_name = line.split(",") student = Student(id, last_name, first_name) students[id] = student
for id in students: student = students[id] name = student.get_first_name() + " " + student.get_last_name() print(name)
import pickle
pickle_file = open(PICKLE_FILENAME, "wb")
pickle.dump(students, pickle_file)
pickle_file = open(PICKLE_FILENAME, "rb")
students = pickle.load(pickle_file)
try to open the pickle file
if the file exists
create the students dictionary from the pickle file
else:
create an empty dictionary
open the text file with student data
loop through the text file
get the id, first name and last name
if the id is not in the students dictionary:
create a student object
add the new object to the dictionary
PICKLE_FILENAME = "students.dat" try: pickle_file = open(PICKLE_FILENAME, "rb") except: students = {} else: students = pickle.load(pickle_file) pickle_file.close() data_filename = input("Data filename: ") try: data_file = open(data_filename, "r") except: print("Cannot open", data_file) sys.exit() for line in data_file: line = line.strip() id, last_name, first_name = line.split(",") if id not in students: student = Student(id, last_name, first_name) students[id] = student data_file.close()
def __init__(self, course_id, semester, section_no): self.__course_id = course_id self.__semester = semester self.__section_no = section_no self.__student_ids = set()
STUDENT_ID,LAST_NAME,FIRST_NAME
# reads a text file that has the format # STUDENT_ID,LAST_NAME,FIRST_NAME # adds these students to __student_ids def add_students(self, data_filename): try: data_file = open(data_filename, "r") except: print("Could not open", data_file) sys.exit() for line in data_file: line = line.strip() id, last_name, first_name = line.split(",") self.__student_ids.add(id) data_file.close()
# retrieves the pickled students dictionary # and uses it to create a new dictionary # of student object for students enrolled in this section def get_enrolled_students(self): enrolled_students = {} try: pickle_file = open(PICKLE_FILENAME, "rb") except: print("Could not open", PICKLE_FILENAME) sys.exit() students = pickle.load(pickle_file) pickle_file.close() for id in self.__student_ids: enrolled_students[id] = students[id] return enrolled_students
from section import Section section = Section("it117", "s17", 1) print(section) section.add_students("students.txt") enrolled_students = section.get_enrolled_students() for id in enrolled_students: print(enrolled_students[id])
it117 s17 section 1 Smith John, 34524 Adams Jane, 45235
from student import Student import pickle PICKLE_FILENAME = "students.dat" class Section: ...