Code Review Exercises
Before beginning, make sure you have reviewed directory structure with your Team Leader
class CalculateBonus(object): # class names: a CamelCased short name that # says what it does """'Static' variables defined at the class level http://stackoverflow.com/a/9056994/946957 (class scoped vars) BONUS: they give us default values for all instance variables In PHP we call these properties """ event_revenue_1 = 3 event_revenue_2 = 5 student_name = "The unamed student" bonus_percent = 0.20 #total_revenue = 0 # functions inside a class are methods def sum_revenues(self): total_revenue = self.event_revenue_1 + self.event_revenue_2 # Money Format: http://stackoverflow.com/a/21208495/946957 return '${:,.2f}'.format(total_revenue) def sum_then_multiply(self): # Proper Line Continuation: http://stackoverflow.com/a/53180 bonus_earned = (self.event_revenue_1 + self.event_revenue_2) * \ self.bonus_percent return '${:,.2f}'.format((bonus_earned)) class FormatBonusToPercent(CalculateBonus): #Pythonic inheritance def decimal_to_percent(self): return ( str(self.bonus_percent*100) + "%" ) def main(): # These are 3 objects student_1 = CalculateBonus() student_1.student_name = "Prithi Malhotra" student_1.event_revenue_1 = 2 student_1.event_revenue_2 = 3 student_1.bonus_percent = .25 student_2 = FormatBonusToPercent() student_2.student_name = "Abe Oluengsi" student_2.event_revenue_1 = 7 student_2.event_revenue_2 = 10 student_2.bonus_percent = .5 student_3 = FormatBonusToPercent() student_3.student_name = "Suchi Nishikawa" """The % operator (modulo): %s is a placeholder for data that will be inserted into the string. Also known as String Formatting Syntax, Formatters, Formatter variables, etc """ print ("%s's total revenues were %s with a %s bonus at a rate of %s" % ( student_1.student_name, student_1.sum_revenues(), student_1.sum_then_multiply(), student_1.bonus_percent )) print ("%s's total revenues were %s with a %s bonus, at a rate of %s" % ( student_2.student_name, student_2.sum_revenues(), student_2.sum_then_multiply(), student_2.decimal_to_percent() )) print ("%s's total revenues were %s with a %s bonus, at a rate of %s" % ( student_3.student_name, student_3.sum_revenues(), student_3.sum_then_multiply(), student_3.decimal_to_percent() )) """ 1. Test whether your script is being run directly or being imported by something else by testing if it's the "main fubction" 2. Makes sure the server only runs if the script is executed directly from the Python interpreter and not used as an imported module. 3. If your code is being imported into another module, the various function and class definitions will be imported, but the main() code won't get run """ if __name__ == '__main__': main() #Can call this anything, but must match name of the principal function!
""" Learn more about importing here: https://docs.python.org/3.5/tutorial/modules.html """ from bonus import CalculateBonus, FormatBonusToPercent #import classes into namespace employee_1 = FormatBonusToPercent #CalculateBonus employee_1.student_name = "Barry Obama" CalculateBonus.bonus_percent = .8 #or employee_1.bonus_percent = .8 print ("%s's total revenues were %s with a %s bonus at a rate of %s. \ \n%s's bonus in percent is actually: %s" % ( employee_1.student_name, employee_1.sum_revenues(employee_1), employee_1.sum_then_multiply(employee_1), employee_1.bonus_percent, employee_1.student_name, employee_1.decimal_to_percent(employee_1) ))