Modular Programming - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Modular Programming

Description:

Part of an evolutionary progression for program design. Structured Programming ... Abstraction. Writing code that is reusable in lots of situations ... – PowerPoint PPT presentation

Number of Views:490
Avg rating:3.0/5.0
Slides: 30
Provided by: pt14
Category:

less

Transcript and Presenter's Notes

Title: Modular Programming


1
Modular Programming
  • HI5100 Data Structures for BioInformatics
  • Lesson 8

2
OBJECTIVES
  • In this lesson you will learn
  • What is a module?
  • Why are programs developed using modules?

3
Modern Programming
  • Complex environments
  • Most systems are built using an object-oriented
    (OO) approach
  • Before OO programming there was modular
    programming

4
Modules
  • 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

5
Structured 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

6
Subroutines
  • 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

7
Advantages 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

8
Encapsulation
  • 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

9
Object-Oriented Programming
  • Depends on the principle of encapsulation
  • And reuse
  • And some other principles
  • Abstraction
  • Polymorphism
  • inheritance

10
Modular Programming
  • Depends on the principle of encapsulation
  • And reuse
  • And some other principles
  • Abstraction

11
Modular Programming
  • Part of an evolutionary progression for program
    design

Spaghetti Code
Programming
Structured Programming
Modular Programming
Object-Oriented Programming
12
Abstraction
  • Writing code that is reusable in lots of
    situations
  • As opposed to code that is useful in only a
    narrowly defined situation

13
Append 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

14
Append 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

15
Append 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
16
Abstraction
  • 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

17
Modules 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

18
graphics.py Module
  • We will gain some experience with Python modules
    by using one that lets us build some GUI elements
    into programs

19
graphics.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

20
Using 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()
21
Window for Graphics
22
from 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()
23
Try Out Some Drawing Code

24
Sample 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)
25
Sample 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)
26
Sample 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
)
27
sampledraw.py
28
Assignment 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

29
End of Lesson 8
Write a Comment
User Comments (0)
About PowerShow.com