Algorithms and Programming

Similar documents
Algorithms and Programming

Algorithms and Programming

Algorithms and Programming

Introduction to programming using Python

Algorithms and Programming

LECTURE 4 Python Basics Part 3

TESTING, DEBUGGING, EXCEPTIONS, ASSERTIONS

Fundamentals of Programming (Python) File Processing. Ali Taheri Sharif University of Technology Spring 2018

FILE HANDLING AND EXCEPTIONS

What is an Exception? Exception Handling. What is an Exception? What is an Exception? test = [1,2,3] test[3]

CSc 120. Introduction to Computer Programming II. 07: Excep*ons. Adapted from slides by Dr. Saumya Debray

A Little Python Part 3

CIS192 Python Programming

#11: File manipulation Reading: Chapter 7

Python File Modes. Mode Description. Open a file for reading. (default)

CIS192 Python Programming

A Little Python Part 3

CIS192 Python Programming

Fundamentals of Programming (Python) File Processing. Sina Sajadmanesh Sharif University of Technology Fall 2017

Introduction to: Computers & Programming: Exception Handling

Chapter 6: Files and Exceptions. COSC 1436, Summer 2016 Dr. Ling Zhang 06/23/2016

COMP1730/COMP6730 Programming for Scientists. Exceptions and exception handling

Python Essential Reference, Second Edition - Chapter 5: Control Flow Page 1 of 8

CIS192 Python Programming

file_object=open( file_name.txt, mode ) f=open( sample.txt, w ) How to create a file:

Python for Astronomers. Errors and Exceptions

Introduction to python

COMP519 Web Programming Lecture 20: Python (Part 4) Handouts

Outline. the try-except statement the try-finally statement. exceptions are classes raising exceptions defining exceptions

CS Programming Languages: Python

Files. Files need to be opened in Python before they can be read from or written into Files are opened in Python using the open() built-in function

Chapter 6: Files and Exceptions. COSC 1436, Spring 2017 Hong Sun 3/6/2017

Exceptions CS GMU

4.3 FURTHER PROGRAMMING

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

File Operations. Working with files in Python. Files are persistent data storage. File Extensions. CS111 Computer Programming

Computer Science 9608 (Notes) Chapter: 4.3 Further programming

CSE : Python Programming. Homework 5 and Projects. Announcements. Course project: Overview. Course Project: Grading criteria

File Operations. Working with files in Python. Files are persistent data storage. File Extensions. CS111 Computer Programming

Python Tutorial. Day 2

Files on disk are organized hierarchically in directories (folders). We will first review some basics about working with them.

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

MEIN 50010: Python Flow Control

Exceptions and File I/O

Exception Handling and Debugging

Review 3. Exceptions and Try-Except Blocks

Computational Physics Laboratory Python scripting

Chapter 9: Dealing with Errors

Chapter 6. Files and Exceptions I

Lecture #12: Quick: Exceptions and SQL

Exceptions & error handling in Python 2 and Python 3

Lecture 21. Programming with Subclasses

CS 307: Software Engineering. Lecture 10: Software Design and Architecture

Question 1. tmp = Stack() # Transfer every item from stk onto tmp. while not stk.is_empty(): tmp.push(stk.pop())

1 Classes. 2 Exceptions. 3 Using Other Code. 4 Problems. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 16, / 19

DECOMPOSITION, ABSTRACTION, FUNCTIONS

CS 11 python track: lecture 2

Fundamentals of Programming The PYTHON programming language

Introduction Programming Using Python Lecture 8. Dr. Zhang COSC 1437 Fall 2017 Nov 30, 2017

A Problem. Loop-Body Returns. While-Loop Solution with a Loop-Body Return. 12. Logical Maneuvers. Typical While-Loop Solution 3/8/2016

61A Lecture 25. Friday, October 28

SBT 645 Introduction to Scientific Computing in Sports Science #6

WELCOME! (download slides and.py files and follow along!) LECTURE 1

12. Logical Maneuvers. Topics: Loop-Body Returns Exceptions Assertions Type Checking Try-Except

ECE 364 Software Engineering Tools Lab. Lecture 8 Python: Advanced I

Lecture 21. Programming with Subclasses

Lecture 8. Introduction to Python! Lecture 8

COMP 204: Sets, Commenting & Exceptions

Introduction to programming using Python

CME 193: Introduction to Scientific Python Lecture 4: Strings and File I/O

Exceptions & a Taste of Declarative Programming in SQL

CS Lecture 26: Grab Bag. Announcements

1 Strings (Review) CS151: Problem Solving and Programming

Advanced Python. Executive Summary, Session 1

Reading and writing files

CME 193: Introduction to Scientific Python Lecture 4: File I/O and Classes

Lecture 21. Programming with Subclasses

Lecture 18. Classes and Types

COMP 204: Sets, Commenting & Exceptions

Getting Started with Python

1 Modules 2 IO. 3 Lambda Functions. 4 Some tips and tricks. 5 Regex. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 30, / 22

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Textbook. Topic 8: Files and Exceptions. Files. Types of Files

Exceptions in Python. AMath 483/583 Lecture 27 May 27, Exceptions in Python. Exceptions in Python

Final Exam Version A

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 12

Introduction to programming Lecture 4: processing les and counting words

Programming II (CS300)

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

Class definition. F21SC Industrial Programming: Python. Post-facto setting of class attributes. Class attributes

Lists, loops and decisions

F21SC Industrial Programming: Python: Classes and Exceptions

CSC326 Persistent Programming i. CSC326 Persistent Programming

Python Programming. Introduction Part II

STSCI Python Introduction. Class URL

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

COMP1730/COMP6730 Programming for Scientists. Testing and Debugging.

Programming II (CS300)

Exception Handling. Genome 559

Transcription:

Algorithms and Programming Lecture 4 Software design principles Camelia Chira

Course content Introduction in the software development process Procedural programming Modular programming Abstract data types Software development principles Testing and debugging Recursion Complexity of algorithms Search and sorting algorithms Backtracking Recap 2018-2019 Algorithms and Programming 2

Last time Modular programming Modules Packages import statement Exceptions Concept Mechanism Examples 2018-2019 Algorithms and Programming 3

Today Exceptions Architecture of an application Principles of organizing the code Working with files 2018-2019 Algorithms and Programming 4

Error messages SyntaxError a = input("first number is b = input("first number is ) NameError IndexError TypeError a = input('first number is') int(a) b = a % 2 print (b) AttributeError IOError 2018-2019 Algorithms and Programming 5

Exceptions Concept Exceptions are raised when errors are detected during program execution Exceptions can interrupt the normal execution of a block Mechanism Exceptions are identified and thrown by the Python interpretor Use the code to indicate the special situations d = int(input("enter a number: ")) # d = 0 print(5 / d) x = d * 10 2018-2019 Algorithms and Programming 6

Exceptions: mechanism Identify exceptions raise statement Python interpretor def functionthatraiseanexception(a, b): if (cond): raise ValueError("message") # code Catch and treat an exception try..except(..finally) statement try: # main code except ExceptionType1 as e1: #if e1 appears, this code is executed except ExceptionType2 as e2: #if e2 appears, this code is executed else: #if there is no exception, this code is executed 2018-2019 Algorithms and Programming 7

def gcd(a, b): if (a == 0): Exceptions if (b == 0): return -1 # a == b == 0 else: return b # a == 0, b!= 0 else: if (b == 0): # a!= 0, b == 0 return a else: # a!= 0, b!= 0 while (a!= b): if (a > b): a = a - b else: b = b - a return a # a == b def run_gcd(): a = int(input("input the first number: ")) b = int(input("input the second number: ")) print( GCD of ", a, "and ", b, " is ", gcd(a,b)) run_gcd() def gcd_v2(a, b): if (a == 0): if (b == 0): raise ValueError("one number must be!= 0") else: return b # a == 0, b!= 0 else: if (b == 0): # a!= 0, b == 0 return a else: # a!= 0, b!= 0 while (a!= b): if (a > b): a = a - b else: b = b - a return a # a == b def run_gcd(): a = int(input("input the first number: ")) b = int(input("input the second number: ")) print( GCD of ", a, "and ", b, " is ", gcd_v2(a,b)) run_gcd() 2018-2019 Algorithms and Programming 8

Exceptions Mechanism Catch exceptions Python code can include handlers for exceptions Statement try...except The clause finally - statements that will always be executed (clean-up code) try: #code that may raise exceptions except ValueError: #code to handle the error finally: #code that is executed in all the cases try: d = 0 print (5 / d) x = d * 10 except ZeroDivisionError: print("division by zero error...") finally: print("all cases...") def run_gcd(): a = int(input("input the first number: ")) b = int(input("input the second number: ")) try: div = gcd_v2(a,b) print("gcd of ", a, " and ", b, " is ", div) except ValueError as ex: print("exceptional case: ", ex) finally: print("do you want to try again?") 2018-2019 Algorithms and Programming 9

Exceptions: more examples try: a = int(input("enter the first number: ")) b = int(input("enter the second number: ")) print("a+b = ", a+b) print("a/b = ", a/b) print("a**b = ", a**b) except ValueError: print("the value you entered is not a number!") except ZeroDivisionError: print("the second number can not be zero: division by zero!") except: print("an exception occurred...") 2018-2019 Algorithms and Programming 10

Exceptions and testing def gcd_v2(a, b): if (a == 0): if (b == 0): raise ValueError("one number must be!= 0") else: return b # a == 0, b!= 0 else: if (b == 0): # a!= 0, b == 0 return a else: # a!= 0, b!= 0 while (a!= b): if (a > b): a = a - b else: b = b - a return a # a == b def run_gcd(): a = int(input("input the first number: ")) b = int(input("input the second number: ")) try: div = gcd_v2(a,b) print("gcd of ", a, " and ", b, " is ", div) except ValueError as ex: print("exceptional case: ", ex) finally: print("do you want to try again?") def test_gcd_v2(): assert gcd_v2(0, 2) == 2 assert gcd_v2(2, 0) == 2 assert gcd_v2(3, 2) == 1 assert gcd_v2(6, 2) == 2 assert gcd_v2(4, 6) == 2 assert gcd_v2(24, 9) == 3 try: gcd_v2(0, 0) assert False except ValueError: assert True 2018-2019 Algorithms and Programming 11

Exceptions When should exceptions be used? Identify special situations Ex1: A function does not receive parameters according to its specification Ex2: Operating on data from files that do not exist or can not be accessed Ex3: Impossible operations (division by 0) Force compliance with specifications (pre-conditions) 2018-2019 Algorithms and Programming 12

Organize the code of an application Layered architectures Decomposing by features 2 perspectives: Functional perspective identifying different features specified by the problem Technical perspective introducing technical features (such as user interaction, file management, databases, networks, etc) Recommended solution: Decompose a complex application on layers Concentrate the code related to the domain of the problem in a single layer and isolate it Ensure cohesive layers 2018-2019 Algorithms and Programming 13

Organize the code of an application How to organize the code Create modules for User interface Functions dealing with user interaction Contains read operations and display methods The only module used to read and output data Domain of the application Functions dealing with the problem domain Infrastructure Useful functions that are highly to be reused (e.g. logging, network I/O) Application coordinator Initializes and starts the application 2018-2019 Algorithms and Programming 14

Organize the code of an application Good software design: Code easy to understand Easy to test Easy to maintain Easy to develop and modify (e.g. add features) Key design principles Single Responsibility Principle Separation of Concerns Principle Reuse Principle Cohesion and Coupling Principle 2018-2019 Algorithms and Programming 15

Organize the code of an application Single Responsibility Principle Responsibility = a reason to change something Responsibility Of a function = to compute something Of a module = responsibilities of all functions in the module The principle of a single responsibility A function / module should have one responsibility Multiple responsibilities: Difficulties in understanding and using Impossibility of testing Impossibility of reusing Difficulties in maintenance and development #Function with multiple responsibilities #implement user interaction (read/print) #implement a computation (filter) def filterscore(): global l left = input("start score:") right = input("end score:") for el in l: if ((el > left) and (el < right)): print el 2018-2019 Algorithms and Programming 16

Organize the code of an application Separation of concerns Process of separating a program based on features that overlap as little as possible def filterscoreui(): global l inf = input("start sc:") sup = input("end sc:") rez = filterscore(l, inf, sup) for e in rez: print(e) def filterscore(l, left, right): """ filter elements of list l st, end - integers limits return list of elements filtered by left and right limits """ rez = [] for el in l: if ((el > left) and (el < right)): rez.append(el) return rez def testfilterscore(): l = [5, 2, 6, 8] assert filterscore(l, 3, 4) == [] assert filterscore(l, 1, 30) == l assert filterscore(l,3,7)==[5, 6] 2018-2019 Algorithms and Programming 17

Organize the code of an application Reuse Principle Using modules improves the maintenance of an application For instance: it is easier to isolate and correct mistakes modify existing functionalities Using modules facilitates reuse of elements defined in the application Ex. Numericlibrary module (isprime, gcd,...) can be used in several applications Managing the dependencies increases reuse Dependencies between functions A function invokes (calls) another function / other functions Dependencies between modules The functions from a module invoke functions from other modules 2018-2019 Algorithms and Programming 18

Organize the code of an application Cohesion and Coupling Principle The definition of modules should consider: The cohesion degree how dependent are some elements on other elements of the module The coupling degree how dependent is a module on other modules 2018-2019 Algorithms and Programming 19

Organize the code of an application Cohesion and Coupling Principle Cohesion Measures the degree to which a module has a single, well-focused purpose A module can have: High cohesion the elements of the module are highly dependent on each other Ex. Rational module contains operations specific to rational numbers Low cohesion the elements relate more to other activities (and not to each other) Ex. Rational module uses functions from numericlibrary module A highly cohesive module performs only one task Needs reduced interaction with other parts of the program Advantages: Such modules are easier to maintain and not frequently changed More usable because they are modules designed for a well-focused purpose 2018-2019 Algorithms and Programming 20

Organize the code of an application Cohesion and Coupling Principle Coupling Measures the intensity of connections between modules (how much a module knows of another module) Modules can have: High coupling modules that are highly dependent on each other A high coupling between modules leads to difficulties in: Understanding the code Reuse the code Isolate possible mistakes Low coupling independent modules Low coupling facilitates development of applications that are easy to modify (as the interdependency between functions/modules is minimal) A low coupling between modules is desired Good design principle 2018-2019 Algorithms and Programming 21

Organize the code of an application Layered architecture Organizing the application on layers should consider: Low coupling between modules Modules do not need to know details about other modules futures changes are easier to make High cohesion of each module The elements of a module should be highly related 2018-2019 Algorithms and Programming 22

Organize the code of an application Layered architecture Organizing the application on layers should follow an architectural design pattern that: Allows design of systems Flexible Using components (as independent as possible) Each layer communicates with the previous layer Each layer has a well-defined interface that is used by the superior layer (implementation details are hidden) 2018-2019 Algorithms and Programming 23

Organize the code of an application Layered architecture Generally, the architecture of a system can be designed using the following layers: User Interface Layer Functions, modules, classes for the user interface User Interface Layer / UI Layer / View Layer / Presentation Layer Domain Layer All functions generated by the use cases (the logic of the application) Domain Layer / Business Logic Layer / Model Layer Infrastructure Layer Functions with a general character, reusable Coordinator 2018-2019 Algorithms and Programming 24

Example #UI def filterscoreui(): global l inf = input("start sc:") sup = input("end sc:") rez = filterscoredomain(l, inf, sup) for e in rez: print(e) #domain l = [5, 2, 6, 8] def filterscoredomain(left, right): global l rez = filterscore(l, left, right) l = rez return rez #utility function (infrastructure) def filterscore(l, left, right): """ filter elements of list l st, end - integers limits return list of elements filtered by left and right limits """ rez = [] for el in l: if ((el > left) and (el <right)): rez.append(el) return rez def testfilterscore(): l = [5, 2, 6, 8] assert filterscore(l, 3, 4) == [] assert filterscore(l, 1, 30) == l assert filterscore(l,3,7)==[5, 6] testfilterscore() 2018-2019 Algorithms and Programming 25

Other examples Rational Example from lecture 3 Application to manage rational numbers Layers: domain, ui, utils, app More examples Next lecture 2018-2019 Algorithms and Programming 26

Files Working with files in Python Processing files using file type objects Operations with file objects Open Read Write Close Exceptions 2018-2019 Algorithms and Programming 27

File operations Open a file create an instance of a file type object Function open(filename, openmode) filename is a string containing the name of the file (and its path) openmode r open to read w open to write a open to add def read_from_file(self): fin = open("../data.txt", "r") # read operations fin.close() def read_from_file(self): try: fin = open("../data.txt", "r") for line in fin: print(line) fin.close() except IOError as ex: print("reading File errors: ", ex) 2018-2019 Algorithms and Programming 28

File operations Write to a file Function write(info) writes the string info to a file If info is a number or a list conversion to string and then write to file If info is an object (instance of a class) the class must implement the str method def appendtofile(): fout = open("test.out", "a") fout.write("\n appended lines\n") x = 5 fout.write(str(x)) fout.close() def writetofile(): fout = open("test.out", "w") fout.write("first file test...") x = 5 fout.write(str(x)) l1 = [2, 3, 4] fout.write(str(l1)) l2 = ["aw", "ert", "45", "GGGGGGGGG"] fout.write(str(l2)) fout.close() 2018-2019 Algorithms and Programming 29

File operations Read from a file readline() reads a line from a file and returns it as string read() reads all lines from a file and returns the content as a string Close a file close() closes the file, possibly making the memory space free def readfromfilealine(): fin = open("test.in", "r") line = fin.readline() print(line) fin.close() def readfromfilealllines(): fin = open("test.in", "r") line = fin.readline() while (line!= ""): print(line) line = fin.readline() fin.close() def readentirefile(): fin = open("test.in", "r") line = fin.read() print(line) fin.close() 2018-2019 Algorithms and Programming 30

Working with files Exceptions IOError When one of the file operations (open, read, write, close) can not be executed The file that has to be opened is not found The file can not be opened (technical reasons) The file can not be written to (no more disk space) def runfiles(): try: writetofile() appendtofile() readfromfilealine() readfromfilealllines() readentirefile() except IOError as ex: print("some errors: ", ex) 2018-2019 Algorithms and Programming 31

Open function fileobject = open(filename, openmode) openmode: r Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode. r+ Opens a file for both reading and writing. The file pointer placed at the beginning of the file. w Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. w+ Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing. a Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing. a+ Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing. def processfile1(): try: f = open("data.txt", "r") #some operations on the file f.close() except IOError as e1: print("something is wrong as IO..." + str(e1)) except ValueError as e2: print("something is wrong as value..." + str(e2)) something is wrong as IO...[Errno 2] No such file or directory: 'data.txt' 2018-2019 Algorithms and Programming 32

Example Reading text files def processfile2(): try: f = open("data.txt", "r") alllines = f.read() #read all the lines for char in alllines: #consider each char from lines print(char) lines = alllines.split("\n") for line in lines: #consider each line from the file print(line) for line in lines: #consider each line from the file words = line.split(" ") for w in words: print(w) lastline = lines[2] words = lastline.split(" ") s = 0 for w in words: s = s + int(w) print("sum = ", s) f.close() except IOError as e1: print("something is wrong as IO..." + str(e1)) except ValueError as e2: print("something is wrong as value..." + str(e2)) 2018-2019 Algorithms and Programming 33

Example Writing to text files def processfile3(): try: f = open("results.txt", "w") f.write("message1" + "\n") f.write("message2\n") f.write(str(123) + "\n") f.write(str(156) + "\n") l1 = [1,2,3] f.write(str(l1) + "\n") l2 = ["ab", "abc", "abcd"] f.write(str(l2) + "\n") f.close() except IOError as e1: print("something is wrong as IO..." + str(e1)) except ValueError as e2: print("something is wrong as value..." + str(e2)) 2018-2019 Algorithms and Programming 34

Python object serialization Serialization: transform an object to a format that can be stored so that the original object can be recreated later Python Serialization in binary files Marshal Pickle Serialization in text files JSON 2018-2019 Algorithms and Programming 35

Pickle import pickle as pk The serialization process in Python pickle module Pickling Convert an object to a binary format that can be stored dump function Unpickling The process that takes a binary array and converts it to an object load function def processfile4(): data = [1, 2, 3] try: f = open("res.pk", "wb") pk.dump(data, f) f.close() except IOError as e1: print("something is wrong as IO..." + str(e1)) except ValueError as e2: print("something is wrong as value..." + str(e2)) def processfile5(): try: f = open("res.pk", "rb") data = pk.load(f) print(data) f.close() except IOError as e1: print("something is wrong as IO..." + str(e1)) except ValueError as e2: print("something is wrong as value..." + str(e2)) 2018-2019 Algorithms and Programming 36

Recap today Layered Architectures (UI, Domain, Infrastructure) Design Principles Responsibility Principle Separation of Concerns Principle Reuse Principle Cohesion and Coupling Principle Working with files File operations Examples 2018-2019 Algorithms and Programming 37

Reading materials and useful links 1. The Python Programming Language - https://www.python.org/ 2. The Python Standard Library - https://docs.python.org/3/library/index.html 3. The Python Tutorial - https://docs.python.org/3/tutorial/ 4. M. Frentiu, H.F. Pop, Fundamentals of Programming, Cluj University Press, 2006. 5. MIT OpenCourseWare, Introduction to Computer Science and Programming in Python, https://ocw.mit.edu, 2016. 6. K. Beck, Test Driven Development: By Example. Addison-Wesley Longman, 2002. http://en.wikipedia.org/wiki/test-driven_development 7. M. Fowler, Refactoring. Improving the Design of Existing Code, Addison-Wesley, 1999. http://refactoring.com/catalog/index.html 2018-2019 Algorithms and Programming 38

Bibliography The content of this course has been prepared using the reading materials from previous slide, different sources from the Internet as well as lectures on Fundamentals of Programming held in previous years by: Prof. Dr. Laura Dioşan - www.cs.ubbcluj.ro/~lauras Conf. Dr. Istvan Czibula - www.cs.ubbcluj.ro/~istvanc Lect. Dr. Andreea Vescan - www.cs.ubbcluj.ro/~avescan Lect. Dr. Arthur Molnar - www.cs.ubbcluj.ro/~arthur 2018-2019 Algorithms and Programming 39