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
Are there any questions before I begin?
>>> 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'
t1 = Time("09:45:00")
print(t1)
>>> 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(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(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 = 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
@DECORATOR_NAME
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
@property
@property def manufacturer(self): return self._manufacturer @property def model(self): return self._model @property def serial_number(self): return self._serial_number @property def processor(self): return self._processor @property def ram(self): return self._ram @property def hostname(self): return self._hostname @property def disk_size(self): return self._disk_size
>>> c1 = Computer("Apple", "Mac Studio", "FD5C640XZH", "Apple M2 Max", "32 GB", "Jupiter", "2 TB") >>> c1.manufacturer 'Apple' >>> c1.model 'Mac Studio' >>> c1.serial_number 'FD5C640XZH' >>> c1.processor 'Apple M2 Max' >>> c1.ram '32 GB' >>> c1.hostname 'Jupiter' >>> c1.disk_size '2 TB'
@ATTRIBUTE_NAME.setter
@ram.setter def ram(self, new_ram): self._ram = new_ram @hostname.setter def hostname(self, new_hostname): self._hostname = new_hostname @disk_size.setter def disk_size(self, new_disk_size): self._disk_size = new_disk_size self._facing = facing
>>> c1.ram '32 GB' >>> c1.ram = "64 GB" >>> c1.ram '64 GB' >>> c1.hostname 'Jupiter' >>> c1.hostname = "Jupiter II" >>> c1.hostname 'Jupiter II' >>> c1.disk_size '2 TB' >>> c1.disk_size = "3 TB" >>> c1.disk_size '3 TB'
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 = ""
@property def id(self): return self._id @property def last_name(self): return self._last_name @property def first_name(self): return self._first_name @property def preferred_name(self): return self._preferred_name @property def phonetic_name(self): return self._phonetic_name @property def email(self): return self._email @property def unix_username(self): return self._unix_username
@preferred_name.setter def preferred_name(self, preferred_name): self._preferred_name = preferred_name @phonetic_name.setter def phonetic_name(self, phonetic_name): self._phonetic_name = phonetic_name @email.setter def email(self, email): self._email = email @unix_username.setter def unix_username(self, unix_username): self._unix_username = unix_username
def __str__(self): return self._first_name + " " + self._last_name + ", " + self._id
>>> student = Student("12345", "Smith", "William") >>> print(student) William Smith, 12345
def full_name(self): if self._preferred_name: return self._preferred_name + " " + self._last_name else: return self._first_name + " " + self._last_name
>>> student.full_name() 'William Smith' >>> student._preferred_name("Bill") >>> student.get_full_name() 'Bill Smith'
def sort_name(self): if self._preferred_name: return self._last_name + ", " + self._preferred_name else: return self._last_name + ", " + self._first_name
>>> student.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.strip() 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 objects 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: ...