0% found this document useful (0 votes)
10 views

Lecture 1

Uploaded by

EmjayVibes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lecture 1

Uploaded by

EmjayVibes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Lecture 1

1. Object Oriented Software design (Object Oriented Programming,


OOP)
2. Implementation and Testing
3. Team Software specification and management
4. Cross-platform tools and GUI development

Project: Student management system in Python


Problem Statement 1: Write a program employing the concept of OOP and its
pillars to build a simple GUI Student Management System using Python (or any
other programming language of your choice!) which can perform the following
operations:
1. Accept new student registration
2. Display student data when required
3. Search for student records
4. Delete student record
5. Update student record

Problem Statement 2: Write a program employing the concept of OOP and its
pillars to build a simple GUI Staff Management System using Python (or any
other programming language of your choice!) which can perform the following
operations:
1.Accept new staff registration
2. Display staff data when required
3.Search for staff records
4.Delete staff record
5.Update student record

Implementation is entirely up to you! What is required of you is to give an


update on the methods below:

Method 1: Accept – This method takes details from the user like name,
matric- number, subjects offered, and other relevant student details.

Example:

# Method to enter new student details


def accept(self, Name, matno,….. ):
# Creates a new class constructor
# and pass the details
sd = Student(Name, Matno,…….)
# list containing objects of student class
ls.append(sd)

Method 2: Display – This method displays the details of every student.

Example:

# Function to display student details


def display(self, sd):
print("Name : ", sd.name)
print("Matno : ", sd.matno)
print(" ", )
print(" ", )
Print…….

Method 3: Search – This method searches for a particular student from


the list of students. This method will ask the user for example on name or
mat number and then search according to name or mat number

Example:

Search Function
def search(self, a,b):
for i in range(ls.__len__()):
# iterate through the list containing
# student object and checks through
# name or mat no of each object
if(ls[i].matno == ….):
# returns the object with matching
# roll number
return i
Method 4: Delete – This method deletes the record of a particular student
or staff with a matching identifier that is defined.

Example:

# Delete Function
def delete(self, …..):
# Calls the search function
# created above
i = obj.search(….)
del ls[i]

Method 5: Update – This method updates the roll number of the student.
This method will ask for the old roll number and new roll number. It will
replace the old roll number with a new roll number.

Example:

# Update Function
def update(self, …, ….):
# calling the search function
# of student class
i = obj.search(…)
ls[i].matno = No

Introduction to Object Oriented Software Design

Procedural vs OOP
Until now, you have probably been coding in so-called procedural style: your code was a
sequence of steps to be carried out. This is common when doing data analysis: you download
the data, process, and visualize it.
Procedural thinking is natural. You get up, have breakfast, go to work. This sequential
viewpoint works great if you are trying to plan your day. But if you are a city planner, you have
to think about thousands of people with their own routines. Trying to map out a sequence of
actions for each individual would quickly get unsustainable. Instead, you are likely to start
thinking about patterns of behaviors. Same thing with code. The more data it uses, the more
functionality it has, the harder it is to think about as just a sequence of steps.
Instead, we view it as a collection of objects, and patterns of their interactions - like users
interacting with elements of an interface. This point of view becomes invaluable when
designing frameworks, like application program interfaces or graphical user interfaces, or
building tools like pandas DataFrames! It will help you organize your code better, making it
more reusable and maintainable.

• Abstraction: It is a property by which we hide unnecessary details


from the user. For example, if a user wants to order food, he/she
should only have to give order details and get the order
acknowledgment, they do not need to know about how the order
is processed internally or what mechanisms we are using.
Abstraction does the work of simplifying the interface and the user
only has to think about the input and output and not about the
process.

• Encapsulation: Encapsulation is the process of binding the data


with the code so that the data is not available externally or to
unauthorized personnel. This is done by restricting access using
access modifiers.

• Inheritance: Inheritance is the property by which a class is able to


access the fields and methods of another class. The class inheriting
these fields is called the subclass and the class whose fields are
inherited is called the superclass. The sub-class can have
additional fields and methods than those of the superclass. The
purpose of inheritance is to increase code reusability and make the
code more readable.

• Polymorphism: Polymorphism is the ability to have different


methods with the same name but perform different tasks. It can
be achieved through method overriding or method overloading.
Method overriding is a subclass having a different
implementation of the method than the parent class while method
overloading is a function having different parameter types or
number of parameters.

Objects as Data Structures


The fundamental concepts of OOP are objects and classes. An object is a data structure
incorporating information about state and behavior. For example, an object representing a
customer can have a certain phone number and email associated with them, and behaviors like
placeOrder or cancelOrder.
An object representing a button on a website can have a label, and can triggerEvent when
pressed. The distinctive feature of OOP is that state and behavior are bundled together: instead
of thinking of customer data separately from customer actions, we think of them as one unit
representing a customer. This is called encapsulation, and it's one of the core tenets of object-
oriented programming.
The real strength of OOP comes from utilizing classes. Classes are like blueprints for objects.
They describe the possible states and behaviors that every object of a certain type could have.
For example, if you say "every customer will have a phone number and an email, and will be
able to place and cancel orders", you just defined a class! This way, you can talk about
customers in a unified way.
Classes as blueprints

Then a specific Customer object is just a realization of this class with particular state values

Objects in Python

In Python, everything is an object. Numbers, strings, DataFrames, even functions are objects.
In particular, everything you deal with in Python has a class, a blueprint associated with it
under the hood. The existence of these unified interfaces, is why you can use, for example, any
DataFrame in the same way. You can call type() on any Python object to find out its class. For
example, the class of a numpy array is actually called ndarray (for n-dimensional array).

Attributes and methods

Classes incorporate information about state and behavior. State information in Python is
contained in attributes, and behavior information -- in methods. Take a numpy array: you've
already been using some of its methods and attributes! For example, every numpy array has an
attribute "shape" that you can access by specifying the name of the array, then dot, and shape.
It also has methods, like max and reshape, which are also accessible via dot.
Object = attributes + methods

Attributes (or states) in Python objects are represented by variables -- like numbers, or strings,
or tuples, in the case of the numpy array shape. Methods, or behaviors, are represented by
functions. Both are accessible from an object using the dot syntax. You can list all the attributes
and methods that an object has by calling dir() on it. For example here, we see that a numpy
array has methods like trace and transpose.

Example:

We can define a class (Employee), and created an object of that class called mystery
What class does the mystery object have? Type(mystery)
So the mystery object is an Employee! You can further explore it in the console to find out what
attributes it has.

• Print the mystery employee's name attribute.


• Print the employee's salary.
• Give a particular employee a raise of $2500 by using a suitable method
(use help() again if you need to!).
• Print the salary again.

# Print the mystery employee's name


print(mystery.name)

# Print the mystery employee's salary


print(mystery.salary)

# Give the mystery employee a raise of $2500


mystery.give_raise(2500)
# Print the salary again
print(mystery.salary)

The solution works as follows:


1. print(mystery.name): This line of code prints the name of the mystery employee, which
is Natasha.
2. print(mystery.salary): This line of code prints the current salary of Natasha.
3. mystery.give_raise(2500): This line of code uses the give_raise method to increase
Natasha's salary by $2500. In object-oriented programming, a method is a function that is
associated with an object. In this case, the object is mystery (which represents Natasha),
and the method is give_raise. The number 2500 is an argument passed to the method,
indicating the amount of the raise.
4. print(mystery.salary): Finally, this line of code prints Natasha's salary again, which
should now be $2500 higher than the original amount.
This solution assumes that the mystery object and the give_raise method have been properly
defined elsewhere in the code. The give_raise method should be designed to increase
the salary attribute of the mystery object by the amount passed as an argument.

Help(mystery)
Help on Employee in module __main__ object:

class Employee(builtins.object)
| Employee(name, email=None, salary=None, rank=5)
|
| Class representing a company employee.
|
| Attributes
| ----------
| name : str
| Employee's name
| email : str, default None
| Employee's email
| salary : float, default None
| Employee's salary
| rank : int, default 5
| The rank of the employee in the company hierarchy (1 -- CEO, 2
-- direct reports of CEO, 3 -- direct reports of direct reports of CEO
etc). Cannot be None if the employee is current.
|
| Methods defined here:
|
| __init__(self, name, email=None, salary=None, rank=5)
| Create an Employee object
|
| give_raise(self, amount)
| Raise employee's salary by a certain `amount`. Can only be used
with current employees.
|
| Example usage:
| # emp is an Employee object
| emp.give_raise(1000)
|
| promote(self)
| Promote an employee to the next level of the company hierarchy.
Decreases the rank of the employee by 1. Can only be used on current
employeed who are not at the top of the hierarchy.
|
| Example usage:
| # emp is an Employee object
| emp.promote()
|
| terminate(self)
| Terminate the employee. Sets salary and rank to None..
|
| Example usage:
| # emp is an Employee object
| emp.terminate()
|
| -------------------------------------------------------------------
---
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)

A basic class

To start a new class definition, all you need is a class statement, followed by the name
of the class, followed by a colon. Everything in the indented block after will be
considered a part of the class. You can create an "empty" class -- like a blank template
-- by including the pass statement after the class declaration. Even though this class
is empty, we can already create objects of the class by specifying the name of the
class, followed by parentheses. Here, c1 and c2 are two different objects of the empty
class Customer. We want to create objects that actually store data and operate on it -
- in other words have attributes and methods.
Add methods to a class

Defining a method is is simple. Methods are functions, so the definition of a method


looks just like a regular Python function, with one exception: the special self
argument that every method will have as the first argument, possibly followed by
other arguments. We'll get back to self in a minute, first let's see how this works.
Here we defined a method "identify" for the Customer class that takes self and a
name as a parameter and prints "I am Customer" plus name when called. We create
a new customer object, call the method by using object-dot-method syntax and pass
the desired name, and get the output. Note that name was the second parameter in
the method definition, but it is the first parameter when the method is called. The
mysterious self is not needed the method call.

What is self?

So what was that self? Classes are templates. Objects of a class don't yet exist when
a class is being defined, but we often need a way to refer to the data of a particular
object within class definition. That is the purpose of self - it's a stand-in for the future
object. That's why every method should have the self argument -- so we could use it
to access attributes and call other methods from within the class definition even when
no objects were created yet. Python will handle self when the method is called from
an object using the dot syntax. In fact, using object-dot-method is equivalent to passing
that object as an argument. That's why we don't specify it explicitly when calling the
method from an existing object.
We need attributes

By the principles of OOP, the data describing the state of the object should be
bundled into the object. For example, customer name should be an attribute of a
customer object, instead of a parameter passed to a method. In Python attributes --
like variables -- are created by assignment, meaning an attribute manifests into
existence only when a value is assigned to it.

Add an attribute to class

Here is a method set_name with arguments self (every method should have a self
argument) and new_name. To create an attribute of the Customer class called
"name", all we need to do is to assign something to self-dot-name. Remember, self
is a stand-in for object, so self-dot-attribute should remind you of the object-dot-
attribute syntax. Here, we set the name attribute to the new_name parameter of the
function. When we create a customer, it does not yet have a name attribute. But after
the set_name method was called, the name attribute is created, and we can access
it through dot-name.
Using attributes in class definition

Equipped with the name attribute, now we can improve our identification method!
Instead of passing name as a parameter, we will use the data already stored in the
name attribute of the customer class. We remove the name parameter from the
identify method, and replace it with self-dot-name in the printout, which, via self, will
pull the name attribute from the object that called the method. Now the identify
function will only use the data that is encapsulated in the object, instead of using
whatever we passed to it.

class Employee:

def set_name(self, new_name):


self.name = new_name

# Add set_salary() method


def set_salary(self, new_salary):
self.salary = new_salary

# Create an object emp of class Employee


emp = Employee()

# Use set_name to set the name of emp to 'Korel Rossi'


emp.set_name('Korel Rossi')

# Set the salary of emp to 50000


emp.set_salary(50000)

Using attributes in class definition


In the previous exercise, you defined an Employee class with two attributes and two methods
setting those attributes. This kind of method, aptly called a setter method, is far from the only
possible kind. Methods are functions, so anything you can do with a function, you can also do
with a method. For example, you can use methods to print, return values, make plots, and raise
exceptions, as long as it makes sense as the behavior of the objects described by the class
(an Employee probably wouldn't have a pivot_table() method).

In this exercise, you'll go beyond the setter methods and learn how to use existing class
attributes to define new methods. The Employee class and the emp object from the previous
exercise are in your script pane.

class Employee:
def set_name(self, new_name):
self.name = new_name

def set_salary(self, new_salary):


self.salary = new_salary

# Add a give_raise() method with raise amount as a paramet


er
def give_raise(self, amount):
self.salary = self.salary + amount

emp = Employee()
emp.set_name('Korel Rossi')
emp.set_salary(50000)

print(emp.salary)
emp.give_raise(1500)
print(emp.salary)
class Employee:
def set_name(self, new_name):
self.name = new_name

def set_salary(self, new_salary):


self.salary = new_salary

def give_raise(self, amount):


self.salary = self.salary + amount

# Add monthly_salary method that returns 1/12th of salary


attribute
def monthly_salary(self):
return self.salary / 12

emp = Employee()
emp.set_name('Korel Rossi')
emp.set_salary(50000)

# Get monthly salary of emp and assign to mon_sal


mon_sal = emp.monthly_salary()

# Print mon_sal

Good luck!

You might also like