Friday, March 21, 2014

Class Plan (03/21/2014)

We Are Back!  =)

Today's Exercise [DATA]

Create a recreation index for Arlington using three location types: Parks, Supermarkets, BK_JackBox_KFC_Mcd_TB_Wendys.
  • Code the classes in one script and the procedures in another.
  • The code will predefine the location types scores, but the user will assign the weights.
  • Incorporate error handling in the procedural script.
  • The class script will incorporate inheritance for the location types. We will implement a parent class for locations and children classes for each of the three location types.




New Material for Today:
  • Error Handling with Try/Except
  • String tokens with %s
  • Intro to Object Oriented Programming (OOP)

Error Handling
try:
     Some Operation
except:
     If the try fails, do this instead of an error message
Example
def pick_num():

     i = 0

     while i == 0:

          try:

               user_num = int(raw_input("Please specify your number.\n >> "))

               if user_num > 0:

                    return user_num

          except:

               print "Hey now! Whole interger numbers only, yeah?\n"

print "Good job! You picked %s." % pick_num()


String Tokens
Use % (modulo) and then s (signifying string) to represent strings in text. Should be familiar to you C programmers.

Example
first_class = "python"
second_class = "GIS"
print "My favorite class is %s" % first_class
print "My two favorite classes are %s and %s" % (first_class, second_class)

Object Oriented Programming
Definitions
  • OOP: Separates objects from procedures and places the object as the more important.
  • Class: Defines an object.
  • Methods: Functions within a class
  • Inheritance: A new class based around a parent class and inheriting all parent methods

Example #1
## In this example notice the class and the method.
## Notice the (self) in the print_fave method. This refers to the instance of the object. In this case, the instance is my_fave.
class fave_classes:
     def print_fave(self):
          print "Of course, my favorite class is Python Programming for GIS!"

## Notice below the instance to the class must first be established and then the specific method called
my_fave = fave_classes()
my_fave.print_fave()

Example #2
## Notice variables passed to the method come after the self designation
class fave_classes:
     def print_fave(self, fave):
          print "Of course, my favorite class is %s" % fave
## Notice variables are not passed to the class, but to the specific method
my_fave = fave_classes()
my_fave.print_fave("Python Programming")

Example #3
## This displays a method calling another method in the same class. As shown, the instance (self in example below) must be specified.
class fave_classes:

     def print_fave(self, fave):

          print "Of course, my favorite class is %s" % fave

     def receive_fave(self, user_fave):

          self.print_fave(user_fave)

my_fave = fave_classes()

my_fave.receive_fave("Python Programming")


Example #4
## This displays a method from one class calling a method from another class. As shown, the class with () must precede the method.
class first_class:
     def print_fave(self, fave):
          print "Of course, my favorite class is %s" % fave
         
class second_class:
     def receive_fave(self, user_fave):
          first_class().print_fave(user_fave)

my_fave = second_class()

my_fave.receive_fave("Python Programming")

Example #5
## This demonstrates inheritance. The instance is set to fave_class , but you can see we can also use methods from the parent class, all_classes.
class all_classes:
     def print_statement(self):
          print "I have a lot of classes on my schedule."
         
class fave_class(all_classes):
     def most_fave(self, user_fave):
          print "My favorite, however, is %s" % user_fave

my_fave = fave_class()
my_fave.print_statement()
my_fave.most_fave("Python Programming!")

No comments:

Post a Comment