Object Oriented Programming, Pt. 2

In our first lecture on Object Oriented Programming we introduced the concept of classes to create specialized data structures with functions specific to the class, known as methods. In this lecture, we continue to build upon the fundamentals and use classes to store in data from a CSV file. Our class contains several attributes that allow us to easily access information stored in our file.

To begin, let’s look at the data which we want to organize. Here is one line from our CSV file.

“Josephine”,”Darakjy”,”Chanay, Jeffrey A Esq”,”4 B Blue Ridge Blvd”,”Brighton”,”Livingston”,”MI”,48116,”810-292-9388″,”810-374-9840″,”josephine_darakjy@darakjy.org”,”http://www.chanayjeffreyaesq.com”

Each line of the file contains a first name, last name, company, address, city, county, state, 
zip, primary phone number, secondary phone number, email, and web address. These will be the attributes to our Record class. We can create a Record class and give it these attributes like so.

class Record:

def __init__(self, firstname='', lastname='', company='', \
address='', city='', county='', state='', zip='', phone1='', \
phone2='', email='', web=''):
"""
Initializes the Record Class with 12 arguments
"""

self.firstname = firstname
self.lastname = lastname
self.company = company
self.address = address
self.city = city
self.county = county
self.state = state
self.zip = zip
self.phone1 = phone1
self.phone2 = phone2
self.email = email
self.web = web

We saw in our first lecture on object oriented programming that the value of objects cannot be printed without first describing to Python should be printed when we call the printfunction. We can do so by overloading the __str__()operator. Here will will print the entire contents of the Record.

  def __str__(self):
"""
Prints out the entire Record object
"""

s = self.firstname + " " + self.lastname + "\n" + \
self.company + "\n" + \
self.address + "\n" + \
self.county + "\n" + \
self.city + ", " + self.state + ", " + self.zip + "\n" +\
"Phone 1: " + self.phone1 + "\n" +\
"Phone 2: " + self.phone2 + "\n" +\
"Email: " +self.email + "\n" +\
"web: " +self.web

return s

In some instances, we want to print an abbreviated or compact version of a Record, for instance when it appears in a list. In this case, we want to overload the __repr__()operator. For our case, we will only print the first and last name as a shorter representation of the Record object.

  def __repr__(self):
"""
Shorter representation of the Record object
"""
return self.lastname + ', ' + self.firstname

Lastly, we will want to sort all of the records once we import them. We will store our records in a list. Lists have a built-in method for sorting; however, we can’t quite use this method yet. The reason we are unable to do so is because the sorting method has no understanding of how to compare two Record objects. We can tell Python what it means to compare two records by overloading the greater-than and less-than operators. Both methods are shown below.

  def __lt__(self, rec):
"""
Overloads the less than operator to compare Record objects. Records
are compared using the last name attribute. If the records have the same
last name, the first name is not evaluated.
"""
return self.lastname < rec.lastname

def __gt__(self, rec):
"""
Overloads the greater than operator to compare Record objects. Records
are compared using the last name attribute. If the records have the same
last name, the first name is not evaluated.
"""
return self.lastname > rec.lastname

Now, we can use the built in sort operator. Putting this all together, we can read in a records from a CSV file, store it in a Record object, and sort the list containing all of the records. The complete code is shown below. Press run to see the code operate.

css.php