Demo entry 6365846

Object Oriented Programming practice in Python

   

Submitted by anonymous on May 21, 2017 at 14:59
Language: Python. Code size: 3.5 kB.

class Employee(object):
    # the (object) makes this class a 'new-style' class used in python 2.7

    numOfEmps = 0
    raiseAmount = 1.04

    def __init__(self, first, last, pay):
        self.nameFirst = first  # slef.first is treated as a single variable that is tied to the any instance of the class.
        self.nameLast = last  # = first is the argument supplied to the class's __init__ method. The __init__ method initiantes the class.
        self.pay = pay
        self.email = first.lower() + '.' + last.lower() + '@mail.com'

        Employee.numOfEmps += 1  # initilised with Employee class, removes the ability for instances to override this vlaue

    def fullname(self):
        return '{} {}'.format(
            self.nameFirst, self.nameLast
        )  # equivalent to '%s %s' % (self.nameFirst, self.nameLast)

    def applyRaise(self):
        self.pay = int(
            self.pay * self.raiseAmount
        )  # initialising the raiseAmount class variable with self gives the
        # oppertunity to override the constant via the class instance

    @classmethod  # this is a decorator, makes the method take the class as the frst argument
    def setRaiseAmt(cls, amount):
        cls.raiseAmount = amount  # used to set class variables for both the class and all of its instnaces

    @classmethod
    def fromString(cls, emp_string):
        first, last, pay = emp_string.split('-')
        pay = int(pay)
        return cls(first, last, pay)  # passes into the __init__ mathoda

    #static methods do not pass any automatic arguments intot itself
    #used for things that have a logical connection with the class.
    @staticmethod
    def isWorkDay(day):
        if day.weekday() in [5, 6]:
            return False
        return True


class Developer(Employee):
    raiseAmount = 1.10

    def __init__(self, nameFirst, nameLast, pay, progLang):
        super(Developer, self).__init__(nameFirst, nameLast, pay)
        #super() calls the parent class
        self.progLang = progLang


class Manager(Employee):
    def __init__(
            self, nameFirst, nameLast, pay,
            employees=None):  # never pass mutable data types into functions
        super(Manager, self).__init__(nameFirst, nameLast, pay)
        if employees is None:
            self.employees = []
        else:
            self.employees = employees

    def addEmp(self, emp):
        if emp not in self.employees:
            self.employees.append(emp)

    def removeEmp(self, emp):
        if emp in self.employees:
            self.employees.remove(emp)

    def printEmps(self):
        for emp in self.employees:
            print '--> {}'.format(emp.fullname())


emp_1 = Employee('David', 'Tetruashvili', 1337)
emp_2 = Employee('Test', 'User', 4000)

Employee.setRaiseAmt(1.05)

# print Employee.raiseAmount
# print emp_1.raiseAmount
# print emp_2.raiseAmount

emp_3 = Employee.fromString('Robert-Baratheon-1000')

# print emp_3.email

# dev_1 = Developer.fromString('Dan-Avidan-50000')
#TODO make .fromsring work on more classes
dev_1 = Developer('Dav', 'Avidan', 70000, 'Java')
dev_2 = Developer('Arin', 'Hanson', 30000, 'Python')

# print dev_1.email

# print help(Developer)

# print dev_2.progLang

man_1 = Manager('Susi', 'Hanson', 50000, [dev_1, dev_2, emp_1, emp_2, emp_3])

issubclass(Developer, Employee)  #boolean tests
isinstance(dev_1, Developer)

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).