Projects‎ > ‎

Project 4

Object-Oriented Polling Data Programs

Due Wednesday, December 5th at 11:59pm
Due Monday, December 10th at 11:59pm

For this project you are going to rewrite your Project 3 code using Classes and Objects.

You will create three classes:

class ElectionPolls(object):
    ...

class State(object):
    ...

class Poll(object):
    ...

The ElectionPolls class will contain a set of State objects, and each State object will contain one or more Poll objects.

You can still use dictionaries and lists within the objects, but now, for example, the ElectionPolls object will have a dictionary, in which each key is a state and each value is a State object (rather than a list).  The State object will consist of a list of Poll objects.

You ElectionPolls class will look like this:

class ElectionPolls(object):

    def __init__(self, name):
        # Initialization here
        # Create dictionary
        self.name = name

    def load_poll_data(self, filename):
        """Load poll data into a polls dictionary from filename."""
        ...

    def load_result_data(self, filename):
        """Load the election results data into a a results dictionary from filename."""
        ...

    def states_by_electoral_votes(self):
        """Return a list of State objects sorted by number of electoral votes."""
        ....

    def find_dem_states(self):
        """Return a list of State objects that have a dem majority."""
        ...

    def find_gop_states(self):
        """Return a list of State objects that have a gop majority."""
        ...

    def find_tie_states(self):
        """Return a list of States object that have a tie between dem and gop."""
        ...

    def find_closest_polls(self):
        """For each state find the poll that most closely matchs the final popular vote.  Return a list of tuple (State, Poll)."""
        ....

    def find_best_poll(self):
        """Find the poll that resulted in the most accurate prediction of the 2012 presidential elections."""
        ....

    def __str__(self):
        """Return a string version of the ElectionPolls object."""
        ...


Your State class will look like this:

class State(object):

    def __init__(self, name, electoral_votes):
        """Initialize a State object with values.  Initialize poll list."""
        ...

    def get_name(self):
        """Return the name of the state object."""
        ...

    def get_electoral_votes(self):
        """Return the number of electoral votes for the state object."""
        ...

    def add_poll(self, poll_name, date, dem, gop):
        """Create a new Poll object and add to State poll list."""
        ...

    def get_polls(self):
        """Return a list of poll objects for this State object."""
        ...

    def __str__(self):
        """Return a string version of the State object."""
        ...

Your Poll class will look like this:

class Poll(object):

    def __init__(self, name, date, dem, gop):
         """Initialize a Poll object."""

    def get_name(self):
        """Return the name of the poll object."""
        ...

    def get_date(self):
        """Return the date of the poll object."""
        ...

    def get_dem(self):
        """Return the dem percent for the poll object."""
        ...

    def get_gop(self):
        """Return the gop percent for the poll object."""
        ...

    def is_dem(self):
        return self.dem > self.gop

    def __str__(self):
        """Return a string version of the Poll object."""
        ...


You should create a subdirectory called "polldata" and create an empty file called:

__init__.py

You should put each class above into a separate file inside the polldata directory:

election_polls.py
state.py
poll.py


Once you have these classes implemented, you should be able to write all of your Project 3 programs like this:

import polldata.election_polls
import polldata.state
import polldata.poll

ep = polldata.election_polls.ElectionPolls("2012 Election")
ep.load_poll_data("pres_polls.csv")
print ep.states_by_electoral_votes()

Comments