Title: Modular Programming
1Modular Programming
- HI5100 Data Structures for BioInformatics
- Lesson 8
2OBJECTIVES
- In this lesson you will learn
- What is a module?
- Why are programs developed using modules?
3Modern Programming
- Complex environments
- Most systems are built using an object-oriented
(OO) approach - Before OO programming there was modular
programming
4Modules
- Code modules organize large amounts of code into
logical collections of parts that are designed to
interact in specific ways - They help programmers reuse code instead of
rewriting functionality into every program - Similar in concept to subroutines
- A named section of code designed to complete a
specific task - Subroutine is called by name from the main
program to complete its specific processing task
5Structured Programming
- Before there was modular programming there was
structured programming - Eliminate spaghetti code
- Eliminate goto transfer of control
- Main body of code is placed in a subroutine
called main - Code to complete specific tasks is placed in the
program as subroutines with names - e.g., compute_interest
- Inside main the primary task is handled by
sequential calls to subroutines with specific
functionality
6Subroutines
- All modern programming languages provide the
capability of creating and using subroutines - Synonyms
- Macros
- Functions
- Lets you write a piece of code that performs a
specific task - Can be called frequently from the main program as
needed
7Advantages of Subroutines
- Code is written once then reused within the
program - Main program is more bug-free from the beginning
- Debugging is easier
- Can be used by other programs as well
8Encapsulation
- As long as the inputs and outputs to a subroutine
remain the same, you can change the internal
workings of the subroutine all you need to in
order to make improvements
9Object-Oriented Programming
- Depends on the principle of encapsulation
- And reuse
- And some other principles
- Abstraction
- Polymorphism
- inheritance
10Modular Programming
- Depends on the principle of encapsulation
- And reuse
- And some other principles
- Abstraction
11Modular Programming
- Part of an evolutionary progression for program
design
Spaghetti Code
Programming
Structured Programming
Modular Programming
Object-Oriented Programming
12Abstraction
- Writing code that is reusable in lots of
situations - As opposed to code that is useful in only a
narrowly defined situation
13Append AAAAAAA
appendAAAAAAA.py def appendAAAAAAA() DNAseq
raw_input(Enter the DNA sequence
to which you want to append AAAAAAA) newDNA
DNAseq AAAAAAA print newDNA
- This program, named appendAAAAAAA.py, gets a DNA
sequence as input and appends the sequence
AAAAAAA to it
14Append TTTTT
appendTTTTT.py def appendTTTTT() DNAseq
raw_input(Enter the DNA sequence to
which you want to append TTTTT) newDNA
DNAseq TTTTTT print newDNA
- This program, named appendTTTTT.py, gets a DNA
sequence as input and appends the sequence TTTTT
to it
15Append Nucleic Acids
append7.py def append7() DNAseq
raw_input(Enter the DNA sequence to
which you want to append n nucleic
acids of the same type) NAcid
raw_input(Enter the nucleic acid you want to
append by providing its single letter
represenation (A, C, G or T)) howMany
input(Enter the number of nucleic acids of that
type you want to append) newDNA DNAseq
NAcidhowMany print newDNA
16Abstraction
- The task defined as append n nucleic acids of
the same type - Usable in several situations
- Is more abstractly defined than the task append
5 Thymines - Usable in one narrow situation
- Is more abstractly defined than the task append
7 Adenines - Usable in one narrow situation
17Modules and Libraries
- Synonyms
- A module or library collects subroutine
definitions for use in other programs - Instead of copying code into each program, just
point to the file that contains it
18graphics.py Module
- We will gain some experience with Python modules
by using one that lets us build some GUI elements
into programs
19graphics.py
- Download graphics.py from our Moodle course site
or copy and paste the code into a text editor and
save it as graphics.py in the proper folder - The proper folder depends on how you did the
Python install - It will be something like c\python24\Lib\site-pac
kages
20Using graphics.py
- GraphWin is a method in the graphics library
- It opens a 200 pixel X 200 pixel window that can
display graphics - Try the code below in Pythons interactive mode
- The window created may lie underneath other
windows you have open - So you may have to minimize other windows to see
it
gtgtgtimport graphics gtgtgtwin graphics.GraphWin()
21Window for Graphics
22from import command
- After import graphics we used the GraphWin
subroutine - It was called with graphics.GraphWin()
- You can issue a command to import all the
functions in graphics so that you can call any
subroutine or function without prefacing the
function name with graphics.
gtgtgtfrom graphics import gtgtgt win GraphWin()
23Try Out Some Drawing Code
24Sample Drawing Code pg. 1
sampleDraw.py from graphics import def
sampledraw() newwin GraphWin(Plane
Figures) center Point(100,100)
circle1Circle(center,50) circle1.setFill(Green
) circle1.draw(newwin) Label1
Text(center,Hello there!) Label.draw(newwin)
25Sample Drawing Code pg. 2
label1.draw(newwin) for ival in 0,2,4,6
square1 Rectangle(Point(40ival10,20),
Point(100ival10,80))
square1.setFill('Blue')
square1.draw(newwin)
26Sample Drawing Code pg. 3
for ival in 0,2,4,6 square2
Rectangle(Point(140,20ival10),
Point(200,80ival10))
square2.setFill('Red') square2.draw(newwin
)
27sampledraw.py
28Assignment 3
- Search on the World Wide Web for a Python module
to demo - Write a short program that demos at least one
subroutine from the module for me and your fellow
students - Provide the program, the module, and whatever
documentation we need to install the module and
run your program
29End of Lesson 8