Fundamentals of Programming The PYTHON programming language

Size: px
Start display at page:

Download "Fundamentals of Programming The PYTHON programming language"

Transcription

1 Fundamentals of Programming The PYTHON programming language

2 What is programming Hardware and software Hardware - computers (desktop, mobile, etc) and related devices Software - programs or systems which run on hardware Programming language - notation that defines syntax and semantics of programs Python: a high level programming language [1]. It is a great language for beginner programmers [3]. Python interpreter: a program which allow us to run/interpret new programs. Python standard library: built-in functions and types [2]. print ( Hello world ) Program 1 - Hello world

3 What computers do storage and retrieval internal memory hard disk, memory stick operations processor communication keyboard, mouse, display network connector Information and data Data - collection of symbols stored in a computer (e.g. 123 decimal number or abc string are stored using binary representations) Information - interpretation of data for human purposes (e.g. 123, abc ) Information and data processing Input devices produce data from information (e.g. 123 taken from keyboard) Data is stored in memory (e.g. binary representation of 123) Output devices produce information from data (e.g. 123 displayed from a binary representation) Basic operations of processors binary representation operations on binary representations (and, or, not; add, etc)

4 Basic elements of a Python program Program 2 - Adding two integers # Reads two integers and prints the sum of them a = input("enter the first number: ") b = input("enter the second number: ") c = int(a) + int(b) print("the sum of ", a, " + ", b, " is ", c) Lexical elements A Python program is divided into a number of lines. Comments start with a hash (#) character and ends at the end of the line. Identifiers (or names) are sequences of characters which start with a letter (a..z, A..Z) or an underscore (_) followed by zero or more letters, underscores, and digits (0..9). Literals are notations for constant values of some built-in types.

5 Data model All data in Python programs is represented by objects, an object being the Python s abstraction for data. An object has: an identity - we may think of of it as the object s address in memory; a type - which determines the operations that the object supports and also defines the possible values; a value. Once an object has been created, its identity and type cannot be changed. The value of some objects can change. Objects whose values can change - mutable objects. Objects whose values cannot be changed - immutable objects.

6 Standard types Types classifies values. A type denotes a domain (a set of values) and operations on those values. Numbers - Numbers are immutable - once created, their values cannot be changed. int (integers): Represents the mathematical set of integers (positive and negative, unlimited precision) Operations: +, -, *, /, see [2, section 4.4]. Literals: 1 bool (booleans): Represents the the truth values True and False. Operations: [2, section 4.1]. Literals: False, True; 0, 1 float (reals): Represents the mathematical set of double precision floating point numbers. Operations: +, -, *, /, see [2, section 4.4]. Literals: 3.14 Sequences: Strings: Represents the finite ordered sets indexed by non-negative numbers. Let a be a sequence. len(a) returns the number of items; a[0], a[1],, a[len(a)-1] represent the set of items. Examples: [1, a ] A string is an immutable sequence; The items of a string are Unicode characters. Literals: abc, abc

7 Variables Variables are reserved memory locations to store values. A variable has a name which refers to an object; so a variable has also a type, and a value. A variable is introduced in a program using a name binding operation assignment. Variable: name value type domain operations memory location Variable in Python: name value type domain operations memory location Expressions An expression is a combination of explicit values, constants, variables, operators, and functions that are interpreted according to the particular rules of precedence, which computes and then produces/returns another value. Examples: numeric expression: boolean expression 1 < 2 string expression: 1 + 2

8 Statements Statements are the basic operations of a program. A program is a sequence of statements. Assignment Assignment is a statement. Assignments are used to (re)bind names to values and to modify items of mutable objects. Bind name: x = 1 #x is a variable (of type int) Rebind name: x = x + 2 #a new value is assigned to x Rebind name of mutable sequences: y = [1, 2] #mutable sequence y[0] = -1 #the first item is bound to -1 Block A block is a piece of Python program text that is executed as a unit. A sequence of statements is a block. Blocks of code are denoted by line indentation.

9 How to write programs Basic roles in software engineering Programmers/Developers Use computers to write/develop programs for users. Clients/stakeholders: Who is affected by the outcome of a project. Users Run programs on their computers. A software development process is an approach to building, deploying, and possible maintaining software. It indicates: What tasks/steps can be taken during development. In which order? We will use: a simple feature-driven development process

10 Problem statement A problem statement is a short description of the problem being solved. Calculator - Problem statement A teacher (client) needs a program for students (users) who learn or use rational numbers. The program shall help students to make basic arithmetic operation Requirements Requirements define in details what is needed from the client perspective. Requirements define: What the clients need; and What the system must include to satisfy the client' needs. Requirements guidelines Good requirements ensure your system works like your customers expect. (Don't create problems to solve problems!) Capture the list of features your software is supposed to do. The list of features must clarify the problem statement ambiguities.

11 Feature Feature is a small, client-valued function: expressed in the form <action> <result> <object>, action - a function that the application must provide result - the result obtained after executing the function object - an entity within the application that implements the function and typically can be implemented within a few hours (in order to be easy to make estimates). F1. Add a number to calculator. F2. Clear calculator. Calculator - Feature list F3. Undo the last operation (user may repeat this operation).

12 A simple feature-driven development process Build a feature list from the problem statement Plan iterations (at this stage, an iteration may include a single feature) For each iteration Model planned features Implement and test features An iteration is a set period of time within a project in which you produce a stable, executable version of the product, together with any other supporting documentation. Iteration will result in a working and usefoul program for the client (will interact with the user, perform some computation, show results) Example: iteration plan Iteration Planned features I1 I2 I3 F1. Add a number to calculator F2. Clear calculator F3. Undo the last operation (user may repeat this operation)

13 Iteration modeling At the beginning of each iteration you must understand the work required to implement it. You must investigate/analyze each feature in order to determine work items/tasks. Then, work items are scheduled. Each work item will be independently implemented and tested. Iteration 1 - Add a number to calculator For simple programs (e.g. Calculator), running scenarios help developer to understand what must be implemented. A running scenario shows possible interactions between users and the program under development. Running scenario for adding numbers User Program Description a 0 Shows total b 1/2 Add number to calculator c 1/2 Shows total d 2/3 Add number to calculator e 5/6 Shows total f 1/6 Add number to calculator g 1 Shows total h -6/6 Add number to calculator i 0 Shows total

14 Work items/tasks Guidelines for tasks Define a task for each operation not already provided by the platform (Python), e.g. T1, T2. Define a task for implementing the interaction between User and Program (User Interface), e.g. T4. Define a task for implementing all operations required by UI, e.g. T3. Determine dependencies between tasks (e.g. T4 --> T3 --> T2 -->T1, where --> means depends on). Schedule items based on the dependencies between them. bottom up: schedule first task without dependencies, or with dependencies scheduled before T1 Compute the greatest common divisor of two integers (see g, i) T2 Add two rational numbers (see c, e, g, i) T3 T4 Implement calculator: init, add, and total Implement user interface

15 Task 1. Compute gcd Test case A test case specifies a set of test inputs, execution conditions, and expected results that you identify to evaluate a particular part of a program. Input params: a, b ValueError 0-2 ValueError gcd (a, b): c, where c is the greatest common divisor of a and b

16 If statement The if statement is used for conditional execution. gcd - if statement def gcd(a, b): Return the greatest common divisor of two positive integers. if a == 0: return b else: if b == 0: return a print gcd(0,2) print gcd(2,0)

17 While statement The while statement is used for repeated execution as long as an expression is true. gcd - while statement def gcd(a, b): Return the greatest common divisor of two positive integers. if a == 0: return b else: if b == 0: return a else: while a!= b: if a > b: a = a - b else: b = b - a return a print gcd(0,2) print gcd(2,0) print gcd(2,4) print gcd(27,4)

18 References 1. The Python language reference The Python standard library The Python tutorial.

19 Structured types List Tuple Dictionary

20 Lists Lists represent the finite ordered sets indexed by non-negative numbers. See [3, sections 3.1.4, 5.1]. Operations: creation accessing values (index, len), changing values (lists are mutable) removing items (pop), inserting items (insert) slicing nesting generate list using range(), list in a for loop lists as stacks (append, pop) # create a = [1, 2, 'a'] print (a) x, y, z = a print(x, y, z) # indices: 0, 1,..., len(a) 1 print a[0] print ('last element = ', a[len(a)-1]) # lists are mutable a[1] = 3 print a # lists as stacks stack = [1, 2, 3] stack.append(4) print stack print stack.pop() print stack #generate lists using range l1 = range(10) print l1 l2 = range(0,10) print l2 l3 = range(0,10,2) print l3 l4 = range(9,0,-1) print l4 # slicing print a[:2] b = a[:] print (b) b[1] = 5 print (b) a[3:] = [7, 9] print(a) a[:0] = [-1] print(a) a[0:2] = [-10, 10] print(a) # nesting c = [1, b, 9] print (c) #list in a for loop l = range(0,10) for i in l: print i

21 Tuples Tuples are immutable sequences. A tuple consists of a number of values separated by commas. See [3, section 5.3]. Operations: packing values (creation) nesting empty tuple tuple with one item sequence unpacking # Tuples are immutable sequences # A tuple consists of a number of values separated by commas # tuple packing t = 12, 21, 'ab' print(t[0]) # empty tuple (0 items) empty = () # tuple with one item singleton = (12,) print (singleton) print (len(singleton)) #tuple in a for t = 1,2,3 for el in t: print el # sequence unpacking x, y, z = t print (x, y, z) # Tuples may be nested u = t, (23, 32) print(u)

22 Dictionaries A dictionary is an unordered set of (key, value) pairs with unique keys. The keys must be immutable. See [3, section 5.5] Operations: creation getting the value associated to a given key adding/updating a (key, value) pair removing an existing (key, value) pair checking whether a key exists #create a dictionary a = {'num': 1, 'denom': 2} print(a) #get a value for a key print(a['num']) #set a value for a key a['num'] = 3 print(a) print(a['num']) #delete a key value pair del a['num'] print (a) #check for a key if 'denom' in a: print('denom = ', a['denom']) if 'num' in a: print('num = ', a['num'])

23 Identity, value and type Recall what is a name and an object (identity, type, value). mutable objects: lists, dictionaries, sets immutable: numbers, strings, tuples Determine the identity and the type of an object using the built-in functions: id(object) type(object), isinstance(object, type)

24 Procedural programming A programming paradigm is a fundamental style of computer programming. Imperative programming is a programming paradigm that describes computation in terms of statements that change a program state. Procedural programming is imperative programming in which the program is built from one or more procedures (also known as subroutines or functions).

25 What is a function A function is a self contained block of statements that: has a name, may have a list of (formal) parameters, may return a value has a documentation (specification) which consists of: a short description type and description for the parameters conditions imposed over the input parameters (precondition) type and description for the return value conditions that must be true just after the execution (postcondition). Exceptions def max(a, b): Compute the maximum of 2 numbers a, b - numbers Return a number - the maximum of two integers. Raise TypeError if parameters are not integers. if a>b: return a return b def isprime(a): Verify if a number is prime a an integer value (a>1) return True if the number is prime, False otherwise

26 Functions The following function is working but we do not consider as a function at the lab/exam def f(k): l = 2 while l<k and k % l>0: l=l+1 return l>=k Every function written by you should: use meaningful names (function name, variable names) provide specification include comments have a test function (see later) def isprime(nr): Verify if a number is prime nr - integer number, nr>1 return True if nr is prime, False otherwise div = 2 #search for divider starting from 2 while div<nr and nr % div>0: div=div+1 #if the first divider is the number itself than the number is prime return div>=nr;

27 Definition A function definition is an executable statement introduced using the keyword def. The function definition does not execute the function body; this gets executed only when the function is called. A function definition defines a user-defined function object. def max(a, b): Compute the maximum of 2 numbers a, b - numbers Return a number - the maximum of two integers. Raise TypeError if parameters are not integers. if a>b: return a return b

28 Variable scope A scope defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block. All variables defined at a particular indentation level or scope are considered local to that indentation level or scope Local variable Global variable global_var = 100 def f(): local_var = 300 print local_var print global_var Rules to determine the scope of a particular name (variable, function name): a name defined inside a block is visible only inside that block formal parameters belong to the scope of the function body (visible only inside the function) name defined outside the function (at the module level) is belong to the module scope

29 Variable scope When a name is used in a code block, it is resolved using the nearest enclosing scope. a = 100 def f(): a = 300 print a f() print a a = 100 def f(): global a a = 300 print a f() print a At any time during execution, names are resolved using : the innermost scope, which is searched first, contains the local names (inside the block) the scopes of any enclosing functions, which are searched starting with the nearest enclosing scope, contains non-local, but also non-global names the next-to-last scope contains the current module s global names the outermost scope (searched last) is the namespace containing built-in names globals() locals() - python built in functions for inspecting global/local variables a = 300 def f(): a = 500 print a print locals() print globals() f() print a

30 Calls A block is a piece of Python program text that is executed as a unit. Blocks of code are denoted by line indentation. A function body is a block. A block is executed in an execution frame. When a function is invoked a new execution frame is created. max(2,5) An execution frame contains: some administrative information (used for debugging) determines where and how execution continues after the code block's execution has completed defines two namespaces, the local and the global namespace, that affect execution of the code block. A namespace is a mapping from names (identifiers) to objects. A particular namespace may be referenced by more than one execution frame, and from other places as well. Adding a name to a namespace is called binding a name (to an object); changing the mapping of a name is called rebinding; removing a name is unbinding. Namespaces are functionally equivalent to dictionaries (and often implemented as dictionaries).

31 Passing parameters Formal parameters is an identifier for an input parameter of a function. Each call to the function must supply a corresponding value (argument) for each mandatory parameter Actual parameter is a value provided by the caller of the function for a formal parameter. The actual parameters (arguments) to a function call are introduced in the local symbol table of the called function when it is called (arguments are passed by object reference ) def change_or_not_immutable(a): print ('Locals ', locals()) print ('Before assignment: a = ', a, ' id = ', id(a)) a = 0 print ('After assignment: a = ', a, ' id = ', id(a)) g1 = 1 #global immutable int print ('Globals ', globals()) print ('Before call: g1 = ', g1, ' id = ', id(g1)) change_or_not_immutable(g1) print ('After call: g1 = ', g1, ' id = ', id(g1)) def change_or_not_mutable(a): print ('Locals ', locals()) print ('Before assignment: a = ', a, ' id = ', id(a)) a[1] = 1 a = [0] print ('After assignment: a = ', a, ' id = ', id(a)) g2 = [0, 1] #global mutable list print ('Globals ', globals()) print ('Before call: g2 = ', g2, ' id = ', id(g2)) change_or_not_mutable(g2) print ('After call: g2 = ', g2, ' id = ', id(g2))

32 Test cases Before implementing a function we write test cases in order to understand what the function must perform. This is also a a design step. A test case specifies a set of test inputs, execution conditions, and expected results that you identify to evaluate a particular part of a program (e.g. a function). For each function we will create a test function, verify input output pairs using the built in assert function

33 Test Function - Calculator example 1 Feature 1. Add a number to calculator. 2 Running scenario for adding numbers 3 Workitems/Tasks T1 Compute the greatest common divisor of two integers (see g, i) T2 Add two rational numbers (see c, e, g, i) T3 T4 Implement calculator: init, add, and total Implement user interface T1 Compute the greatest common divisor of two integers (see g, I) Test case Table format Input: (params a,b) Output: gdc(a,b) Test function def test_gcd(): assert gcd(2, 3) == 1 assert gcd(2, 4) == 2 assert gcd(6, 4) == 2 assert gcd(0, 2) == 2 assert gcd(2, 0) == 2 assert gcd(24, 9) == 3 Implement gdc def gcd(a, b): Compute the greatest common divisor of two positive integers a, b integers a,b >=0 Return the greatest common divisor of two positive integers. if a == 0: return b if b == 0: return a while a!= b: if a > b: a = a - b else: b = b - a return a

34 How to write functions Apply test-driven development (TDD) steps TDD requires developers to create automated unit tests that clarify code requirements before writing the code itself. When you create a new function (f), follow TDD steps: Add a test Define a test function (test_f()) which contains test cases written using assertions. Concentrate on the specification of f. Define f: name, parameters, precondition, post-condition, and an empty body. Run all tests and see if the new one fails Your program may have many functions, so many test functions. At this stage, ensure the new test_f() fails, while other test functions pass (written previously). Write the body of f Now the specification of f is well written and you concentrate on implementing the function according to pre/post-conditions and on passing all test cases written for f. Do not concentrate on technical aspects (duplicated code, optimizations, etc). Run all tests and see them succeed Now, the developer is confident that the function meets the specification. The final step of the cycle can be performed. Refactor code Finally, you must clean up the code using refactorings techniques.

35 Refactorings Code refactoring is "disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior" [5]. Code smell is any symptom in the source code of a program that possibly indicates a deeper problem: Duplicated code: identical or very similar code exists in more than one location. Long method: a method, function, or procedure that has grown too large. Refactorings: Extract Method You have a code fragment that can be grouped together. Turn the fragment into a method whose name explains the purpose of the method. Substitute Algorithm You want to replace an algorithm with one that is clearer. Replace the body of the method with the new algorithm. Replace Temp with Query You are using a temporary variable to hold the result of an expression. Extract the expression into a method. Replace all references to the temp with the expression. The new method can then be used in other methods. References 1. The Python language reference. http ://docs.python.org /py 3 k /reference /index.html 2. The Python standard library. http ://docs.python.org /py 3 k /library /index.html 3. The Python tutorial. http ://docs.python.org /tutorial/index.html 4. Kent Beck.Test Driven Development: By Example. Addison-Wesley Longman, See also Test-driven development Martin Fowler. Refactoring. Improving the Design of Existing Code. Addison- Wesley, See also

36 Test-driven development (TDD) software development process How to write functions? Apply test-driven development (TDD)! TDD Steps: Add a test - Create automated test cases Run the test (initially will fail) Produces the minimum amount of code to pass that test Run the test (succeed) Refactor the code

37 TDD Step 1. Create automated test cases When you work on a task (work-item) start by creating a test function Work item: Compute the greatest common divider def test_gcd(): test function for gdc assert gcd(0, 2) == 2 assert gcd(2, 0) == 2 assert gcd(2, 3) == 1 assert gcd(2, 4) == 2 assert gcd(6, 4) == 2 assert gcd(24, 9) == 3 Concentrate on the specification of f. def gcd(a, b): Return the greatest common divisor of two positive integers. a,b integer numbers, a>=0; b>=0 return an integer number, the greatest common divisor of a and b pass

38 TDD Step 2 - Run the test #run the test - invoke the test function test_gcd() Traceback (most recent call last): File "C:/curs/lect3/tdd.py", line 20, in <module> test_gcd() File "C:/curs/lect3/tdd.py", line 13, in test_gcd assert gcd(0, 2) == 2 AssertionError validates that the test function is working correctly and that the new test does not mistakenly pass without requiring any new code. it rules out the possibility that the new test will always pass, and therefore be worthless

39 TDD Step 3 Write the code to pass the test concentrate on implementing the function according to pre/postconditions and on passing all test cases do not concentrate on technical aspects (duplicated code, optimizations, etc). def gcd(a, b): Return the greatest common divisor of two positive integers. a,b integer numbers, a>=0; b>=0 return an integer number, the greatest common divisor of a and b if a == 0: return b if b == 0: return a while a!= b: if a > b: a = a - b else: b = b - a return a

40 TDD Step 4 - Invoke the test functions and see them succeed >>> test_gcd() >>> If all test cases pass, the programmer can be confident that the code meets all the tested requirements. TDD Step 5 Refactor the code clean up the code using refactoring techniques.

41 Apply test-driven development (TDD) steps TDD requires developers to create automated unit tests that clarify code requirements before writing the code itself. When you create a new function (f), follow TDD steps: Add a test Define a test function (test_f()) which contains test cases written using assertions. Concentrate on the specification of f. Define f: name, parameters, specification, precondition, post condition, and an empty body. Run all tests and see if the new one fails Your program may have many functions, so many test functions. At this stage, ensure the new test_f() fails, while other test functions pass (written previously). Write the body of f Now the specification of f is well written and you concentrate on implementing the function according to pre/post-conditions and on passing all test cases written for f. Do not concentrate on technical aspects (duplicated code, optimizations, etc). Run all tests and see them succeed Now, the developer is confident that the function meets the specification. The final step of the cycle can be performed. Refactor code Finally, you must clean up the code using refactorings techniques.

42 Refactoring Code refactoring is "disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior" [5]. Code smell is any symptom in the source code of a program that possibly indicates a deeper problem: Duplicated code: identical or very similar code exists in more than one location. Long method: a method, function, or procedure that has grown too large.

43 Refactoring: Rename method/variable def verify(k): Verify if a number is prime nr - integer number, nr>1 return True if nr is prime l = 2 while l<k and k % l>0: l=l+1 return l>=k rename a variable or a method name to something meaningful def isprime(nr): Verify if a number is prime nr - integer number, nr>1 return True if nr is prime div = 2 #search for divider while div<nr and nr % div>0: div=div+1 #if the first divider is the # number itself than nr is prime return div>=nr;

44 Refactoring: Extract Method def startui(): list=[] print list #read user command menu = Enter command: 1-add element 0-exit print(menu) cmd=input("") while cmd!=0: if cmd==1: nr=input("give element:") add(list, nr) print list #read user command menu = Enter command: 1-add element 0-exit print(menu) cmd=input("") You have a code fragment that can be grouped together. Turn the fragment into a method whose name explains the purpose of the method. def getusercommand(): Print the application menu return the selected menu menu = Enter command: 1-add element 0-exit print(menu) cmd=input("") return cmd def startui(): list=[] print list cmd=getusercommand() while cmd!=0: if cmd==1: nr=input("give element:") add(list, nr) print list cmd=getusercommand() startui() startui()

45 Refactoring:Substitute Algorithm def isprime(nr): Verify if a number is prime nr - integer number, nr>1 return True if nr is prime div = 2 #search for divider while div<nr and nr % div>0: div=div+1 #if the first divider is the # number itself than nr is prime return div>=nr; You want to replace an algorithm with one that is clearer. Replace the body of the method with the new algorithm. def isprime(nr): Verify if a number is prime nr - integer number, nr>1 return True if nr is prime for div in range(2,nr): if nr%div == 0: return False return True

46 Calculator procedural version

47 Modular programming Modular programming is a software design technique that increases the extent to which software is composed of separate, interchangeable components, called modules by breaking down program functions into modules, each of which accomplishes one function and contains everything necessary to accomplish this. What is a module Module is a collection of functions and variables that implements a well defined functionality Python module definition A module is a file containing Python statements and definitions (executable statements). Module name: The file name is the module name with the suffix.py appended docstring: triple-quoted module doc string that defines the contents of the module file. Provide summary of the module and a description about the module s contents, purpose and usage. executable statements: function definitions, module variables,initialization code

48 Eclipse + PyDev IDE (Integrated Development Environment) Project Package Module Run,Debug program

49 Import modules In order to use a module wee need to import it. The import statement: Search the global namespace for the module. If the module exists, it had already been imported nothing more needs to be done. Search for the module If the module name can t be found anywhere, an ImportError exception is raised. If the module file was found, execute the statements from the module. The executable statements (including function definitions) from a module are executed only the first time the module is imported somewhere. from doted.package[module] import {module, function}} from utils.numericlib import gcd #invoke the gdc function from module utils.numericlib print gdc(2,6) from rational import * #invoke the rational_add function from module rational print rational_add(2,6,1,6) import ui.console #invoke the run method from the module ui.console ui.console.run()

50 Module search path The import statement will search for a file called modulename.py: the directory containing the input script in the list of directories specified by the environment variable PHYTONPATH in the list of directories specified by the environment variable PYTHONHOME an installation-dependent default path; on Unix, this is usually.:/usr/local/lib/python. Initialize the module The module can contain any executable statements. When the module is first imported the statements are executed. We can put some statements (other than function definition) that will do any necessary initialization of the module.

51 Variable scope in a module On importing a module the variables and functions defined in the module will be inserted into a new symbol table (a new namespace). Only the module name will be added to the current symbol table You can use the built-in dir() dir(module_name) function to examine the symbol tables #only import the name ui.console into the current symbol table import ui.console #invoke run by providing the doted notation ui.console of the package ui.console.run() #import the function name gdc into the local symbol table from utils.numericlib import gcd #invoke the gdc function from module utils.numericlib print gdc(2,6) #import all the names (functions, variables) into the local symbol table from rational import * #invoke the rational_add function from module rational print rational_add(2,6,1,6)

52 Packages Packages are a way of structuring Python s module namespace by using dotted module names The init.py files are required to make Python treat the directories as containing packages ( it can also execute initialization code for the package)

53 How to organize your source code Create separate modules for: User interface - Functions related to the user interaction. Contains input, print operations. This is the only module where input/print operations are present Domain / Application Contains functions related to the domain of the problem Infrastructure Utility functions with high reuse potential Application coordinator Initialize and start up the application Calculator modular version

54 How to organize your source code Responsibilities Responsibility is a reason to change function: do a computation module: all the functions responsibilities Single responsibility principle (SRP) A function/module should have one, and only one, reason to change. #Function with multiple responsibilities #implement user interaction (read/print) #implement a computation (filter) def filterscore(): st = input("start score:") end = input("end score:") for c in l: if c[1]>st and c[1]<end: print c Multiple responsibilities Harder to understand and use Unable to test Unable to reuse Difficult to maintain and evolve

55 Separation of concerns Separation of concerns (SoC) is the process of separating a computer program into distinct features that overlap in functionality as little as possible def filterscoreui(): st = input("start sc:") end = input("end sc:") rez = filtrarescore(l,st, end) for e in rez: print e def testscore(): l = [["Ana", 100]] assert filterscore(l,10,30)==[] assert filterscore(l,1,30)==l l = [["Ana", 100],["Ion", 40],["P", 60]] assert filterscore(l,3,50)==[["ion", 40]] def filterscore(l,st, end): filter participants l - list of participants st, end - integers -scores return list of participants filtered by st end score rez = [] for p in l: if p[1]>st and p[1]<end: rez.append(p) return rez Dependency function: a function invokes another function module: any function from the module invoke a function from a another module In order to increase re usability we need to manage dependency

56 Coupling Coupling is a measure of how strongly one element is connected to, has knowledge of, or relies on other elements The more the connections between one module and the rest, the harder to understand that module, the harder to re-use that module in another situation, the harder it is to isolate failures caused by faults in the module The lower the coupling the better Low coupling facilitate the development of programs that can handle change because they minimize the interdependency between functions/modules

57 Cohesion Cohesion is a measure of how strongly related and focused the responsibilities of an element are. A module may have: High Cohesion: it is designed around a set of related functions Low Cohesion: it is designed around a set of unrelated functions A cohesive module performs a single task within a software, requiring little interaction with procedures being performed in other parts of a program. Stated simply, a cohesive module should (ideally) do just one thing The less tightly bound the internal elements, the more disparate the parts to the module, the harder it is to understand The higher the cohesion the better Cohesion is a more general concept than the SRP, modules that are follow the SRP tend to have high cohesion.

58 Layered Architecture Structure the application such that: Minimizes coupling between modules (modules don t need to know much about one another to interact, makes future change easier) Maximizes the cohesion of each module (the contents of each module are strongly interrelated) Layered Architecture is an architectural pattern that allows you to design flexible systems using components (The components are as independent of each other as possible) Each layer communicates only with the layer immediately below it. Each layer has a well-defined interface used by the layer immediately above. (implementation details are hidden) Common layers in an information system logical architecture User interface / Presentation (User interface related functions/modules/classes) Domain / Application Logic (provide the application functions determined by the use-cases) Infrastructure (general/utility functions/modules/classes) Application coordinator

59 Layered Architecture simple example #Ui def filterscoreui(): st = input("start sc:") end = input("end sc:") rez = filterscoredomain(st, end) for e in rez: print e #manage the user interaction #domain l = [["Ion",50],["Ana",30],["Pop",100]] def filterscoredomain(st, end): global l rez = filtermatrix(l, 1, st, end) l = rez return rez #filter the score board #Utility function - infrastructure def filtermatrix(matrice, col, st, end): linii = [] for linie in matrice: if linie[col]>st and linie[col]<end: linii.append(linie) return linii #filter matrix lines

60 How to organize your python projects

61 Exceptions Errors detected during execution are called exceptions. An exception is raised at the point where the error is detected; raised by the python interpreter raised by the code to signal exceptional situation (broken precondition) >>> x=0 >>> print 10/x Trace back (most recent call last): File "<pyshell#1>", line 1, in <module> print 10/x ZeroDivisionError: integer division or modulo by zero def rational_add(a1, a2, b1, b2): Return the sum of two rational numbers. a1,a2,b1,b2 integer numbers, a2<>0 and b2<>0 return a list with 2 int, representing a rational number a1/b2 + b1/b2 Raise ValueError if the denominators are zero. if a2 == 0 or b2 == 0: raise ValueError("0 denominator not allowed") c = [a1 * b2 + a2 * b1, a2 * b2] d = gcd(c[0], c[1]) c[0] = c[0] / d c[1] = c[1] / d return c

62 Execution flow Exceptions are a means of breaking out of the normal flow of execution in order to handle errors or other exceptional conditions. def compute(a,b): print "compute :start " aux = a/b print "compute:after division" rez = aux*10 print "compute: return" return rez def main(): print "main:start" a = 40 b = 1 c = compute(a, b) print "main:after compute" print "result:",c*c print "main:finish" main()

63 Exception handling is the process of handling error conditions in a program systematically by taking the necessary action. try: #code that may raise exceptions pass except ValueError: #code that handle the error pass Exceptions need to be handled by the surrounding code block or by any code block that directly or indirectly invoked the code block where the error occurred otherwise the program will crash raise, try-except statements try: calc_add (int(m), int(n)) printcurrent() except ValueError: print ("Enter integers for m, n, with n!=0")

64 Exception handling multiple except cases, propagate information about the exception def f(): # x = 1/0 raise ValueError("Error Message") try: f() except ValueError as msg: print "handle value error:", msg except KeyError: print "handle key error" except: print "handle any other errors" Only use exceptions to: signal an exceptional situation the function is unable to perform the promised situation enforce preconditions Do not use exception just to control the execution flow

65 Specification Abstraction (function acting as a black box) will only work if we provide: meaningful name for the function short description of the function (the problem solved by the function) meaning/type of each input parameter conditions imposed over the input parameters (precondition) meaning/type of each output parameter the relation between the input and output parameters (post condition) Exceptions that the function may signal (raise) A precondition is a condition that must be true just prior to the execution of some section of code. A post condition is a condition that must be true just after the execution of some section of code. def gcd(a, b): Return the greatest common divisor of two positive integers. a,b integer numbers return an integer number, the greatest common divisor of a and b Raise ValueError if a<=0 or b<=0

66 Test case for exceptions def test_rational_add(): Test function for rational_add assert rational_add(1, 2, 1, 3) == [5, 6] assert rational_add(1, 2, 1, 2) == [1, 1] try: rational_add(2, 0, 1, 2) assert False except ValueError: assert True try: rational_add(2, 3, 1, 0) assert False except ValueError: assert True def rational_add(a1, a2, b1, b2): Return the sum of two rational numbers. a1,a2,b1,b2 integer numbers, a2<>0 and b2<>0 return a list with 2 ints, representing a rational number a1/b2 + b1/b2 Raise ValueError if the denominators are zero. if a2 == 0 or b2 == 0: raise ValueError("0 denominator not allowed") c = [a1 * b2 + a2 * b1, a2 * b2] d = gcd(c[0], c[1]) c[0] = c[0] / d c[1] = c[1] / d return c

67 User defined type Object oriented programming is a programming paradigm that use objects to design applications. Objects and Classes Types classifies values. A type denotes a domain (a set of values) and operations on those values. Classes A class is a construct that is used as a template to create instances of itself referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behaviour.

68 Class definition in python class MyClass: <statement 1>. <statement n> Class definition is an executable statement. The statements inside a class definition will usually be function definitions, but other statements are allowed When a class definition is entered, a new namespace is created, and used as the local scope thus, all assignments to local variables go into this new namespace. In particular, function definitions bind the name of the new function here.

69 Objects Object is a collection of data and functions that operates on that data. Class instances are of the type of the associated class. Class objects support two kinds of operations: attribute (data or method) references and instantiation. Creating instances of a class ( init ) Class instantiation uses function notation. x = MyClass() The instantiation operation ( calling a class object) creates an empty object. x will be an instance of type MyClass A class may define a special method named init class MyClass: def init (self): self.somedata = [] init : create an instance use self to refer to that instance

70 We can have init method with some parameters class RationalNumber: Abstract data type for rational numbers Domain: {a/b where a and b are integer numbers b!=0} def init (self, a, b): Creates a new instance of RationalNumber self.n = a self.m = b r1 = RationalNumber(1,3) #create the rational number 1/3 self.n = a vs n=a 1 Creates an attribute for the current instance 2 Creates a function local variable

71 Object oriented programming Object oriented programming is a programming paradigm that use objects to design applications. Objects and Classes Types classifies values. A type denotes a domain (a set of values) and operations on those values. Classes A class is a construct that is used as a template to create instances of itself referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behaviour.

72 Class definition in python class MyClass: <statement 1>. <statement n> Class definition is an executable statement. Thee statements inside a class definition will usually be function definitions, but other statements are allowed When a class definition is entered, a new namespace is created, and used as the local scope thus, all assignments to local variables go into this new namespace. In particular, function definitions bind the name of the new function here.

73 Objects Object is a collection of data and functions that operates on that data. Class instances are of the type of the associated class. Class objects support two kinds of operations: attribute (data or method) references and instantiation.

74 Creating instances of a class ( init ) Class instantiation uses function notation. x = MyClass() The instantiation operation ( calling a class object) creates an empty object. x will be an instance of type MyClass A class may define a special method named init class MyClass: def init (self): self.somedata = [] init : create an instance use self to refer to that instance (similar with this from other object oriented languages) We can have init method with some parameters

75 Fields x = RationalNumber(1,3) y = RationalNumber(2,3) x.m = 7 x.n = 8 y.m = 44 y.n = 21 class RationalNumber: Abstract data type for rational numbers Domain: {a/b where a and b are integer numbers b!=0} def init (self, a, b): Creates a new instance of RationalNumber #create a field in the rational number #every instance (self) will have this field self.n = a self.m = b self.n = a vs n=a 1 Creates an attribute for the current instance 2 Creates a function local variable

76 Methods Methods are functions in a class that can access values from a specific instance In Python the method will automatically receive a first argument: the current instance All the methods need to have an argument (self) def testcreate(): Test function for creating rational numbers r1 = RationalNumber(1,3) #create the rational number 1/3 assert r1.getnominator()==1 assert r1.getdenominator()==3 r1 = RationalNumber(4,3) #create the rational number 4/3 assert r1.getnominator()==4 assert r1.getdenominator()==3 class RationalNumber: Abstract data type rational numbers Domain: {a/b where a,b integer numbers, b!=0, greatest common divisor a, b =1} def init (self, a, b): Initialize a rational number a,b integer numbers self. nr = [a, b] def getdenominator(self): Getter method return the denominator of the rational number return self. nr[1] def getnominator(self): " Getter method return the nominator of the method return self. nr[0]

77 Special class methods. Operator overloading str - convert into a string type (print representation) def str (self): provide a string representation for the rational number return a string return str(self. nr[0])+"/"+str(self. nr[1]) cmp - comparison (-1,0,1) def testcompareoperator(): Test function for < > r1 = RationalNumber(1, 3) r2 = RationalNumber(2, 3) assert r2>r1 assert r1<r2 def cmp (self, ot): Compare 2 rational numbers self the current instance ot a rational number return -1 if self<ot, 0 if self==ot, 1 if self>ot if (self==ot): return 0 if self.getfloat()<ot.getfloat(): return -1 return 1 eq - verify if equals def testequal(): test function for == r1 = RationalNumber(1, 3) assert r1==r1 r2 = RationalNumber(1, 3) assert r1==r2 r1 = RationalNumber(1, 3) r1 = r1.add(rationalnumber(2, 3)) r2 = RationalNumber(1, 1) assert r1==r2 def eq (self, other): Verify if 2 rational are equals other - a rational number return True if the instance is equal with other return self. nr==other. nr

78 Operator overloading Overload add (self, other) - to be able to use + operator def testaddoperator(): Test function for the + operator r1 = RationalNumber(1,3) r2 = RationalNumber(1,3) r3 = r1+r2 assert r3 == RationalNumber(2,3) def add (self,other): Overload + operator other - rational number return a rational number, the sum of self and other return self.add(other) Overload mul (self, other) - to be able to use * operator Overload setitem (self,index, value) to make a class behave like an array/dictionary, use the [] a = A() a[index] = value getitem (self, index) to make a class behave like an array a = A() for el in a: pass len (self) - overload len getslice (self,low,high) - overload slicing operator a = A() b = a[1:4] call (self, arg) - to make a class behave like a function, use the () a = A() a()

79 Python scope and namespace A namespace is a mapping from names to objects. Namespaces are implemented as Python dictionaries Key: name Value Object A class introduce a new name space Methods and fields of a class are in a separate namespace (the namespace of the class) All the rules (bound a name, scope/visibility, formal/actual parameters, etc.) related to the names (function, variable) are the same for class attributes (methods, fields) Just keep in mind that the class have it's own namespace

80 Class attributes vs instance attributes Data members (fields) class RationalNumber: Abstract data type for rational numbers Domain: {a/b where a and b are integer numbers b!=0} #class field, will be shared by all the instances numberofinstances =0 def init (self, a, b): Creates a new instance of RationalNumber self.n = a self.m = b RationalNumber.numberOfInstances+=1 # accessing class fields numberofinstances =0 def init (self,n,m): self.n = n self.m = m RationalNumber.numberOfInstances+=1 def testnumberinstances(): assert RationalNumber.numberOfInstances == 0 r1 = RationalNumber(1,3) #show the class field numberofinstances assert r1.numberofinstances==1 # set numberofinstances from the class r1.numberofinstances = 8 assert r1.numberofinstances==8 #access to the instance field assert RationalNumber.numberOfInstances==1 #access to the class field testnumberinstances()

81 Class Methods class RationalNumber: #class field, will be shared by all the instances numberofinstances =0 def init (self,n,m): Initialize the rational number n,m - integer numbers self.n = n self.m = m def gettotalnumberofinstances(): Get the number of instances created in the app return RationalNumber.numberOfInstances def testnumberofinstances(): test function for gettotalnumberofinstances assert RationalNumber.getTotalNumberOfInstances()==0 r1 = RationalNumber(2, 3) assert RationalNumber.getTotalNumberOfInstances()==1 testnumberofinstances() ClassName.attributeName used to access the class attributes (fields, methods) form is a function decorator. A static method does not receive an implicit first argument.

82 How to define new data types Encapsulation The data that represents the state of the object and the methods that manipulate that data are stored together as a cohesive unit. State and behaviour are kept together, they are encapsulated. Information hiding The internal representation of an object need to be hidden from view outside of the object's definition Hiding the internals of the object protects its integrity by preventing users from setting the internal data of the component into an invalid or inconsistent state Divide the code into a public interface, and a private implementation of that interface Why: Defining a specific interface and isolate the internals to keep other modules from doing anything incorrect to your data Limit the functions that are visible (part of the interface),so you are free to change the internal data without breaking the client code Write to the Interface, not the the Implementation If you are using only the public functions you can change large parts of your classes without affecting the rest of the program

83 Public and private members - Data hiding in Python We need to protect (hide) the internal representation (the implementation) Provide accessors (getter) to the data Encapsulations is particularly important when the class is used by others Nothing in Python makes it possible to enforce data hiding it is all based upon convention. use the convention: _name or name for fields, methods that are private A name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.

84 Guidelines the client code do not have to know about the implementation details of the methods or the internal data representation (abstraction, the class is a black box) the client code need to work even if we change the implementation or data representation function and class specification have to be independent of the data representation and the method s implementation (Data Abstraction)

85 Abstract data types Abstract data type: the operations are specified independently of their implementation the operations are specified independently of the data representation Abstract data type is a Data type + Data Abstraction + Data Encapsulation Review OOP Rational Number Calculator Change the internal of RationalNumber (use a,b instead of [a,b] as representation)

86 UML Diagrams Unified Modeling Language (UML) is a standardized generalpurpose modeling language in the field of object-oriented software engineering. UML includes a set of graphic notation techniques to create visual models of object-oriented software.

87 Class Diagrams UML Class diagrams describes the structure of a system by showing the system's classes, their attributes, and the relationships among the classes. class RationalNumber: def init (self, a, b): Initialize a rational number a,b integer numbers self. nr = [a, b] def getdenominator(self): Getter method return the denominator return self. nr[1] def getnominator(self): " Getter method return the nominator return self. nr[0] def add(self, a): In the class diagram these classes are represented with boxes which contain three parts: The upper part holds the name of the class The middle part contains the attributes of the class The bottom part contains the methods or operations

88 Relationships A relationship is a general term covering the specific types of logical connections found on class diagram. A Link is the basic relationship among objects. It is represented as a line connecting two or more object boxes. Associations Binary associations (with two ends) are normally represented as a line, with each end connected to a class box. An association can be named, and the ends of an association can be ad noted with role names, ownership indicators, multiplicity, visibility, and other properties. Association can be Bi-directional and uni-directional

89 Aggregation Aggregation is more specific than association. It is an association that represents a part-whole or part-of relationship. class Car: def init (self,eng,col): Initialize a car eng - engine col - string, ie White self. engine = eng self. color = col def getcolor(self): Getter method for color return string return self. color def getengine(self): Getter method for engine return engine return self. engine class Engine: def init (self,cap,type): initialize the engine cap positive integer type string self. capacity = cap self. type = type def getcapacity(self): Getter method for the capacity return self. capacity def gettype(self): Getter method for type return string return self. type

90 Dependency, Package dependency relationship is a relationship in which one element, the client, uses or depends on another element, the suplier create instances have a method parameter use an object in a method

91 Design principles Create software: Easy to understand, modify, maintain, test Classes abstract, encapsulate, hide implementation, easy to test, easy to reuse General scope: managing dependency Single responsibility Separation of concerns Low Coupling High Cohesion Problem statement Write a program for managing students (CRUD operations Create Read Update Delete) Features F1 F2 F3 F4 create a student list students find a student delete student Iteration Plan IT1 - F1; IT2 F2; IT3 F3; IT4 - F4

92 Running scenario user app description a add a student give student id 1 give name Ion new student added a add student give student id 1 give name id already exists, name can not be empty

93 Layered architecture Layer is a logical structuring mechanism for the elements that make up your software solution A multilayered software architecture is using different layers for allocating the responsibilities of an application. Layer is a group of classes (or modules) that have the same set of module dependencies to other modules and are reusable in similar circumstances. User Interface Layer (aka View Layer, UI layer or Presentation layer) Application Layer (aka Service Layer or GRASP Controller Layer) Domain layer (Business Layer, Business logic Layer or Model Layer) Infrastructure Layer (data access or other persistence, logging, network I/O e.g. sending s, and other kind of technical services)

94 Grasp patterns General Responsibility Assignment Software Patterns (or Principles) consists of guidelines for assigning responsibility to classes and objects in object oriented design. High Cohesion Low Coupling Information Expert Controller Protected Variations Creator Pure Fabrication

95 High Cohesion Assign responsibilities so that cohesion remains high High Cohesion is an evaluative pattern that attempts to keep objects appropriately focused, manageable and understandable. High cohesion means that the responsibilities of a given element are strongly related and highly focused. Breaking programs into classes and subsystems is an example of activities that increase the cohesive properties of a system. Alternatively, low cohesion is a situation in which a given element has too many unrelated responsibilities. Elements with low cohesion often suffer from being hard to comprehend, hard to reuse, hard to maintain and adverse to change

96 Low Coupling Assign responsibilities so that coupling remains low Low Coupling dictates how to assign responsibilities to support: low dependency between classes; low impact in a class of changes in other classes; high reuse potential; Form of coupling: TypeX has an attribute (field) that refers to a TypeY instance, or TypeY itself. TypeX has a method which references an instance of TypeY, or TypeY itself, by any means. (parameter, local variable, return value, method invocation) TypeX is a direct or indirect subclass of TypeY.

97 Information Expert Assign a responsibility to the class that has the information necessary to fulfill the responsibility. Information Expert is a principle used to determine where to delegate responsibilities. These responsibilities include methods, computed fields and so on. Using the principle of Information Expert a general approach to assigning responsibilities is to look at a given responsibility, determine the information needed to fulfill it, and then determine where that information is stored. Information Expert will lead to placing the responsibility on the class with the most information required to fulfill it

98 Information Expert Point of Sale application Who is responsible with computing the total? Wee need all the SaleItems to compute the total. Information Expert Sale According to the Expert SaleItem should be responsible with computing the subtotal (quantity * price) 1. Maintain encapsulation of information 2. Promotes low coupling 3. Promotes highly cohesive classes 4. Can cause a class to become excessively complex

99 Creator Creation of objects is one of the most common activities in an objectoriented system. Which class is responsible for creating objects is a fundamental property of the relationship between objects of particular classes. Creator pattern is responsible for creating an object of class In general, a class B should be responsible for creating instances of class A if one, or preferably more, of the following apply: Instances of B contains or compositely aggregates instances of A Instances of B record instances of A Instances of B closely use instances of A Instances of B have the initializing information for instances of A and pass it on creation.

100 Work items T1 T2 T3 T4 T5 Task Create Student Validate student Store student (Create repository) Add student (Create Controller) Create UI Task: create Student def testcreatestudent(): Testing student creation st = Student("1", "Ion", "Adr") assert st.getid() == "1" assert st.getname() == "Ion" assert st.getadr() == "Adr" class Student: def init (self, id, name, adr): Create a new student id, name, address String self.id = id self.name = name self.adr = adr def getid(self): return self.id def getname(self): return self.name def getadr(self): return self.adr

101 Protected Variations How responsibilities should be assigned in such a fashion that the current or future variations in the system do not cause major problems with system operation and/or revision? Create new classes to encapsulate such variations. The Protected Variations pattern protects elements from the variations on other elements (objects, systems, subsystems) by wrapping the focus of instability to a separate class. (with an interface and using polymorphism to create various implementations of this interface).

102 Task: Validate student Possible validation design a class member function in Student that returns true/false a static function returning the list of errors a separate class that encapsulate the validation algorithm Validator class: Protect Variation principle: The Protected Variations pattern protects elements from the variations on other elements (objects, systems, subsystems) by wrapping the focus of instability to a separate class def teststudentvalidator(): Test validate functionality validator = StudentValidator() st = Student("", "Ion", "str") try: validator.validate(st) assert False except ValueError: assert True st = Student("", "", "") try: validator.validate(st) assert False except ValueError: assert True class StudentValidator: Class responsible with validation def validate(self, st): Validate a student st - student raise ValueError if: Id, name or address is empty errors = "" if (st.id==""): errors+="id can not be empty;" if (st.name==""): errors+="name can not be empty;" if (st.adr==""): errors+="address can not be empty" if len(errors)>0: raise ValueError(errors)

103 Pure Fabrication when an expert violates high cohesion and low coupling Assign a highly cohesive set of responsibilities to an artificial class that does not represent anything in the problem domain, in order to support high cohesion, low coupling, and reuse Pure Fabrication is a class that does not represent a concept in the problem domain is specially made up to achieve low coupling, high cohesion Problem: Store Student (in memory, file or database) Expert pattern Student is the expert to perform this operation

104 Pure Fabrication - Repository Problem: Store Student (in memory, file or database) Expert pattern Student is the expert to perform this operation But putting this responsibility into the Student class will result in low cohesion, poor reuse Solution Pure Fabrication Class created with the responsibility to store Students The Student class easy to reuse, has High cohesion, Low coupling Repository will deal with the problem of managing a list o students (persistent storage) Repository pattern A repository represents all objects of a certain type as a conceptual set. Objects of the appropriate type are added and removed, and the machinery behind the REPOSITORY inserts them or deletes them from a persistent storage.

105 Task: Create repository def teststorestudent(): st = Student("1", "Ion", "Adr") rep = InMemoryRepository() assert rep.size()==0 rep.store(st) assert rep.size()==1 st2 = Student("2", "Vasile", "Adr2") rep.store(st2) assert rep.size()==2 st3 = Student("2", "Ana", "Adr3") try: rep.store(st3) assert False except ValueError: pass class InMemoryRepository: Manage the store/retrieval of students def init (self): self.students = {} def store(self, st): Store students st is a student raise RepositoryException if we have a student with the same id if st.getid() in self.students: raise ValueError("A student with this id already exist") if (self.validator!=none): self.validator.validate(st) self.students[st.getid()] = st

106 GRASP Controller Decouple the event source(s) from the objects that actually handle the events. Controller is defined as the first object beyond the UI layer that receives and coordinates ("controls") a system operation. The controller should delegate to other objects the work that needs to be done; it coordinates or controls the activity. It should not do much work itself. Controller encapsulate knowledge about the current state of a use case presentation layer decoupled from problem domain

107 Task: create controller def tescreatestudent(): Test store student rep = InMemoryRepository() val = StudentValidator() ctr = StudentController(rep, val) st = ctr.createstudent("1", "Ion", "Adr") assert st.getid()=="1" assert st.getname()=="ion" try: st = ctr.createstudent("1", "Vasile", "Adr") assert False except ValueError: pass try: st = ctr.createstudent("1", "", "") assert False except ValueError: pass class StudentController: Use case controller for CRUD Operations on student def init (self, rep, validator): self.rep = rep self.validator = validator def createstudent(self, id, name, adr): store a student id, name, address of the student as strings return the Student raise ValueError if a student with this id already exists raise ValueError if the student is invalid st = Student(id, name, adr) if (self.validator!=none): self.validator.validate(st) self.rep.store(st) return st

108 Application coordinator Dependency injection (DI) is a design pattern in object-oriented computer programming whose purpose is to reduce the coupling between software components. Frequently an object uses (depends on) work produced by another part of the system. With DI, the object does not need to know in advance about how the other part of the system works. Instead, the programmer provides (injects) the relevant system component in advance along with a contract that it will behave in a certain way #create validator validator = StudentValidator() #crate repository rep = InMemoryRepository(None) #create console provide(inject) a validator and a repository ctr = StudentController(rep, validator) #create console provide controller ui = Console(ctr) ui.showui() Review the sample application and outline the used patterns

109 Layered arhitecture Partition a complex program into layers. The essential principle is that any element of a layer depends only on other elements in the same layer or on elements of the layers "beneath" it. Develop a design within each layer that is cohesive and that depends only on the layers below

110 Layered Arhitecture Responsabilities Presentation Layer User Interface : Responsible for showing information to the user, collect infromation from the user, and interpreting the user's commands Middle Layer Controller The first object beyond the UI layer that receives and coordinates ("controls") a system operation. Entities Responsible for representing concepts of the business Repositories Provide methods to add and remove objects, will encapsulate the actual insertion or removal of data in the data store. Provide methods that select objects based on some criteria and return Infrastructure Layer Provides generic technical capabilities (utility, files, database, etc)

111 StudentCRUD application Review StudentCRUD application

112 Entities Entity is an object that is not defined by its attributes, but rather by a thread of continuity and its identity Does an object represent something with continuity and identity something that is tracked through different states or even across different implementations? define what it means to be the same thing. def testidentity(): #attributes may change st = Student("1", "Ion", "Adr") st2 = Student("1", "Ion", "Adr2") assert st==st2 #is defined by its identity st = Student("1", "Popescu", "Adr") st2 = Student("2", "Popescu", "Adr2") assert st!=st2 class Student: def init (self, id, name, adr): Create a new student id, name, address String self. id = id self. name = name self. adr = adr def eq (self,ot): Define equal for students ot - student return True if ot and the current instance represent the same student return self. id==ot. id Attributes of the entity may change but the identity remain the same Mistaken identity can lead to data corruption.

113 Value Objects Many objects have no conceptual identity. These objects describe some characteristic of a thing An object that represents a descriptive aspect of the domain with no conceptual identity is called a VALUE OBJECT. When you care only about the attributes of an element of the model, classify it as a VALUE OBJECT def testcreatestudent(): Testing student creation Feature 1 - add a student Task 1 - Create student st = Student("1", "Ion", Address("Adr", 1, "Cluj")) assert st.getid() == "1" assert st.getname() == "Ion" assert st.getadr().getstreet()=="adr" st = Student("2", "Ion2", Address("Adr2", 1, "Cluj")) assert st.getid() == "2" assert st.getname() == "Ion2" assert st.getadr().getstreet() == "Adr2" assert st.getadr().getcity() == "Cluj" class Address: Represent an address def init (self,street,nr,city): self. street = street self. nr = nr self. city = city def getstreet(self): return self. street def getnr(self): return self. nr def getcity(self): return self. city class Student: Represent a student def init (self, id, name, adr): Create a new student id, name String address - Address self. id = id self. name = name self. adr = adr def getid(self): Getter method for id return self. id

114 Aggregates and Repositories Cluster the ENTITIES and VALUE OBJECTS into AGGREGATES and define boundaries around each. Choose one ENTITY to be the root of each AGGREGATE, and control all access to the objects inside the boundary through the root. Allow external objects to hold references to the root only. Repositories provide the illusion of an in-memory collection of all objects of that type Provide REPOSITORIES only for AGGREGATE roots. Only StudentRepository (no AddressRepository)

115 Working with text files in Python Built in function: open() returns a file object, and is most commonly used with two arguments: open(filename,mode). Filename string representing the path to the file (absolute or relative path) Mode: "r" open for read "w" open for write (overwrites the existing content) "a" open for append Methods: write(str) write the string to the file readline() - read a line from the file, return as a string read() - read the entire file, return as a string close() - close the file, free up any system resources Exception: IOError raise this exception if there is an input/output error (no file, no disk space, etc)

116 Python text file examples #open file for write (overwrite if exists, create if not) f = open("test.txt","w") f.write("test data\n") f.close() #open file for write (append if exist, create if not) f = open("test.txt","a") f.write("test data line 2\n") f.close() #open for read f = open("test.txt","r") #read a line from the file line = f.readline() print line f.close() #open for read f = open("test.txt","r") #read a line from the file line = f.readline().strip() while line!="": print line line = f.readline().strip() f.close() #open for read f = open("test.txt","r") #read the entire content from the file line = f.read() print line f.close()

117 Dynamic Typing Type checking is performed at run-time as opposed to at compile-time. In dynamic typing values have types, but variables do not; that is, a variable can refer to a value of any type Duck Typing Duck typing is a style of dynamic typing in which an object's methods and properties determine the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. Duck test: When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck class Student: def init (self, id, name): self. name = name self. id = id def getid(self): return self. id def getname(self): return self. name class Professor: def init (self, id, name, course): self. id = id self. name = name self. course = course def getid(self): return self. id def getname(self): return self. name def getcourse(self): return self. course l = [Student(1, "Ion"), Professor("1", "Popescu", "FP"), Student(31, "Ion2"), Student(11, "Ion3"), Professor("2", "Popescu3", "asd")] for el in l: print el.getname()+" id "+str(el.getid()) def myprint(st): print el.getname(), " id ", el.getid() for el in l: myprint(el)

118 Inheritance Classes can inherit attributes and behavior (i.e., previously coded algorithms associated with a class) from pre-existing classes called base classes or superclasses or parent classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy. Code reuse One of the motivations for using inheritance is the reuse of code that already exist in another class (implementation inheritance). Before the object-oriented paradigm was in use, one had to write similar functionality over and over again. With inheritance, behaviour of a superclass can be inherited by subclasses. It not only possible to call the overridden behaviour (method) of the ancestor (superclass) before adding other functionalities, one can override the behaviour of the ancestor completely.

119 Inheritance in Python Syntax: class DerivedClassName(BaseClassName): DerivedClass will inherit: fields: methods If a requested attribute (field,method) is not found in the class, the search proceeds to look in the base class Derived classes may override methods of their base classes. An overriding method in a derived class may in fact want to extend rather than simply replace the base class method of the same name. There is a simple way to call the base class method directly: call BaseClassName.methodname(self,arguments) class B(A): This class extends A A is the base class, B is the derived class B is inheriting everything from class A def init (self): #initialise the base class A. init (self) print "Initialise B" class A: def init (self): print "Initialise A" def f(self): print "in method f from A" def g(self): print "in method g from A" def g(self): Overwrite method g from A #we may invoke the function from the base class A.f(self) print "in method g from B" b = B() #f is inherited from A b.f() b.g()

120 UML Diagram Generalization The Generalization relationship ("is a") indicates that one of the two related classes (the subclass) is considered to be a specialized form of the other (the super type) and superclass is considered as 'Generalization' of subclass. Any instance of the subtype is also an instance of the superclass. The generalization relationship is also known as the inheritance or "is a" relationship.

121 File Repositories class StudentFileRepository(StudentRepository): Repository for students (stored in a file) pass class StudentFileRepository(StudentRepository): Store/retrieve students from file def init (self,filen): #properly initialise the base class StudentRepository. init (self) self. fname = filen #load student from the file self. loadfromfile() def loadfromfile(self): Load students from file raise ValueError if there is an error when reading from the file try: f = open(self. fname,"r") except IOError: #file not exist return line = f.readline().strip() while line!="": attrs = line.split(";") st = Student(attrs[0],attrs[1],Address(attrs[2], attrs[3], attrs[4])) StudentRepository.store(self, st) line = f.readline().strip() f.close()

122 Overwriting methods def teststore(): filename = "teststudent.txt" repo = StudentFileRepository(fileName) repo.removeall() st = Student("1","Ion",Address("str",3,"Cluj")) repo.store(st) assert repo.size()==1 assert repo.find("1") == st #verify if the student is stored in the file repo2 = StudentFileRepository(fileName) assert repo2.size()==1 assert repo2.find("1") == st def store(self,st): Store the student to the file.overwrite store st - student Post: student is stored to the file raise DuplicatedIdException for duplicated id StudentRepository.store(self, st) self. storetofile() def storetofile(self): Store all the students in to the file raise CorruptedFileException if we can not store to the file f = open(self. fname,"w") sts = StudentRepository.getAll(self) for st in sts: strf = st.getid()+";"+st.getname()+";" strf = strf + st.getadr().getstreet()+";"+str(st.getadr().getnr()) +";"+st.getadr().getcity() strf = strf+"\n" f.write(strf) f.close()

123 Exceptions def createdstudent(self): Read a student and store in the apllication id = raw_input("student id:").strip() name = raw_input("student name:").strip() street = raw_input("address - street:").strip() nr = raw_input("address - number:").strip() city = raw_input("address - city:").strip() try: self. ctr.create(id, name,street,nr,city) except ValueError, msg: print msg def createdstudent(self): Read a student and store in the apllication id = raw_input("student id:").strip() name = raw_input("student name:").strip() street = raw_input("address - street:").strip() nr = raw_input("address - number:").strip() city = raw_input("address - city:").strip() try: self. ctr.create(id, name,street,nr,city) except ValidationException as ex: print ex except DuplicatedIDException as ex: print ex class ValidationException(Exception): def init (self,msgs): Initialise msg is a list of strings (errors) self. msgs = msgs def getmsgs(self): return self. msgs def str (self): return str(self. msgs)

124 Exception hierarhies class StudentCRUDException(Exception): pass class ValidationException(StudentCRUDException): def init (self,msgs): Initialise msg is a list of strings (errors) self. msgs = msgs def getmsgs(self): return self. msgs def str (self): return str(self. msgs) class RepositorException(StudentCRUDException): Base class for the exceptions in the repository def init (self, msg): self. msg = msg def getmsg(self): return self. msg def str (self): return self. msg class DuplicatedIDException(RepositorException): def init (self): RepositorException. init (self, "Duplicated ID") def createdstudent(self): Read a student and store in the apllication id = raw_input("student id:").strip() name = raw_input("student name:").strip() street = raw_input("address - street:").strip() nr = raw_input("address - number:").strip() city = raw_input("address - city:").strip() try: self. ctr.create(id, name,street,nr,city) except StudentCRUDException as ex: print ex

125 Layered arhitecture - Project structure

126 Layered architecture GUI Example Tkinter is the most commonly used GUI toolkit for Python (available on most Unix platforms, as well as on Windows and Macintosh systems ) Review StudentCRUD application with GUI Tkinter (or any other python GUI toolkit) is not a requirement for the exam

127 Associations In real life, there are lots of many-to-many associations In a software these general associations complicate implementation and maintenance bidirectional association means that both objects can be understood only together It is important to constrain relationships as much as possible: Imposing a traversal direction Adding a qualifier, reduce multiplicity Eliminating nonessential associations

128 Associations Catalog example gr = ctr.assign("1", "FP", 10) assert gr.getdiscipline()=="fp" assert gr.getgrade()==10 assert gr.getstudent().getid()=="1" assert gr.getstudent().getname()=="ion" st = Student("1", "Ion", Address("Adr", 1, "Cluj")) rep = GradeRepository() grades = rep.getall(st) assert grades[0].getstudent()==st assert grades[0].getgrade()==10

129 Client Code Ignores Repository Implementation; Developers Do Not def store(self, gr): Store a grade post: grade is in the repository raise GradeAlreadyAssigned exception if we already have a grade for the student at the given discipline raise RepositoryException if there is an IO error when writing to the file if self.find(gr.getstudent(), gr.getdiscipline())!=none: raise GradeAlreadyAssigned() #open the file for append try: f = open(self. fname, "a") grstr = gr.getstudent().getid()+","+gr.getdiscipline() grstr =grstr+","+str(gr.getgrade())+"\n" f.write(grstr) f.close() except IOError: raise RepositorException("Unable to write a grade to the file")

130 Data transfer objects Functionality: Show the best 5 students at a given discipline. Show a table containing the name of the student and his grade at the disicipline Create a new class that contains just the needed information to implement the requested functionality

131 Program testing Testing is observing the behavior of a program in many executions. Execute the program for some input data and observe if the results are correct for these inputs. Testing does not prove program correctness (only give us some confidence). On the contrary, it may prove its incorrectness if one execution give wrong results. Testing can never completely identify all the defects within software.

132 Testing methods Exhaustive testing Check the program for all possible inputs. Impractical so we need to choose a finite number of test cases. Black box testing The selection of input data for testing is decided by analyzing the specification. Distinct cases of the problem are decided and we use a test input data for each case White box testing Select the test data by analyzing the text of the program. We select test data such that all the execution paths are covered. We test a function such that each statement is executed.

133 White box vs Black Box testing def isprime(nr): Verify if a number is prime return True if nr is prime False if not raise ValueError if nr<=0 if nr<=0: raise ValueError("nr need to be positive") if nr==1:#1 is not a prime number return False if nr<=3: return True for i in range(2,nr): if nr%i==0: return False return True Black Box test case for a prime/not prime test case for 0 test case for negative number def blackboxprimetest(): assert (isprime(5)==true) assert (isprime(9)==false) try: isprime(-2) assert False except ValueError: assert True try: isprime(0) assert False except ValueError: assert True White Box (cover all the paths) test case for 0 test case fot negative test case for 1 test case 3 test case for prime (no divider) test case for not prime def whiteboxprimetest(): assert (isprime(1)==false) assert (isprime(3)==true) assert (isprime(11)==true) assert (isprime(9)==true) try: isprime(-2) assert False except ValueError: assert True try: isprime(0) assert False except ValueError: assert True

134 Testing levels Tests are frequently grouped by where they are added in the software development process, or by the level of specificity of the test. Unit testing Unit testing refers to tests that verify the functionality of a specific section of code, usually at the function level. Testing unit of code in isolation (functions). Test small parts of the program independently Integration testing Considers the way program works as a whole. After all modules have been tested and corrected we need to verify the overall behavior of the program.

135 Automated testing Test automation is the process of writing a computer program to do testing that would otherwise need to be done manually. use of software to control the execution of tests, the comparison of actual outcomes to predicted outcomes, the setting up of test preconditions TDD: TDD Steps: automated test cases writing specification (inv, pre/post) throwing or not exceptions production code

136 Python unit testing framework (PyUnit) unittest module supports: test automation sharing of setup and shutdown code for tests aggregation of tests into collections independence of the tests from the reporting framework import unittest class TestCase(unittest.TestCase): def setup(self): #code executed before every testmethod val=studentvalidator() self.ctr=studentcontroller(val, StudentRepository()) st = self.ctr.create("1", "Ion", "Adr", 1, "Cluj") def teardown(self): #cleanup code executed after every testmethod def testcreate(self): self.asserttrue(self.ctr.getnrstudents()==1) #test for an invalid student self.assertraises(validationex,self.ctr.create,"1", "", "", 1, "Cj") #test for duplicated id self.assertraises(duplicatedidexception,self.ctr.create,"1", "I", "A", 1, "j") def testremove(self): #test for an invalid id self.assertraises(valueerror,self.ctr.remove,"2") self.asserttrue(self.ctr.getnrstudents()==1) st = self.ctr.remove("1") self.asserttrue(self.ctr.getnrstudents()==0) self.assertequals(st.getid(),"1") self.asserttrue(st.getname()=="ion") self.asserttrue(st.getadr().getstreet()=="adr") if name == ' main ': unittest.main()

137 Debugging Debugging is the activity that must be performed when testing indicates the presence of errors, to find errors, and rewrite the program with the purpose of eliminating the errors. using print statement using IDE Debugging is the most unpleasant activity. Debugging must be avoided. Eclipse debug perspective Debug view wiew the current execution trace (stack trace) execute step by step, resume/pause execution Variables view view variable values

User Defined Types. Babes-Bolyai University Lecture 06. Lect Phd. Arthur Molnar. User defined types. Python scope and namespace

User Defined Types. Babes-Bolyai University Lecture 06. Lect Phd. Arthur Molnar. User defined types. Python scope and namespace ? User Defined Types Babes-Bolyai University arthur@cs.ubbcluj.ro Overview? 1? 2 3 ? NB! Types classify values. A type denotes a domain (a set of values) operations on those values. ? Object oriented programming

More information

Algorithms and Programming

Algorithms and Programming Algorithms and Programming Lecture 2 Procedural Programming Camelia Chira Last time Programming process What is programming? Basic elements of Python Python programs Data types: string, number Variables

More information

Algorithms and Programming

Algorithms and Programming 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

More information

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines. Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of

More information

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of

More information

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals: Numeric Types There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals: 1-123 +456 2. Long integers, of unlimited

More information

Babes-Bolyai University

Babes-Bolyai University Babes-Bolyai University arthur@cs.ubbcluj.ro Overview 1 Modules programming - a software design technique that increases the extent to which software is composed of independent, interchangeable components

More information

The PCAT Programming Language Reference Manual

The PCAT Programming Language Reference Manual The PCAT Programming Language Reference Manual Andrew Tolmach and Jingke Li Dept. of Computer Science Portland State University September 27, 1995 (revised October 15, 2002) 1 Introduction The PCAT language

More information

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Python I. Some material adapted from Upenn cmpe391 slides and other sources Python I Some material adapted from Upenn cmpe391 slides and other sources Overview Names & Assignment Data types Sequences types: Lists, Tuples, and Strings Mutability Understanding Reference Semantics

More information

Download Python from Any version will do for this class

Download Python from  Any version will do for this class Let s Start Python Let s Start! Download Python from www.python.org Any version will do for this class By and large they are all mutually compatible Recommended version: 2.1.1 or 2.2 Oldest version still

More information

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017 Overview of OOP Dr. Zhang COSC 1436 Summer, 2017 7/18/2017 Review Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in square brackets: l = [1, 2, "a"] (access by index, is mutable

More information

Babu Madhav Institute of Information Technology, UTU 2015

Babu Madhav Institute of Information Technology, UTU 2015 Five years Integrated M.Sc.(IT)(Semester 5) Question Bank 060010502:Programming in Python Unit-1:Introduction To Python Q-1 Answer the following Questions in short. 1. Which operator is used for slicing?

More information

Python in 10 (50) minutes

Python in 10 (50) minutes Python in 10 (50) minutes https://www.stavros.io/tutorials/python/ Python for Microcontrollers Getting started with MicroPython Donald Norris, McGrawHill (2017) Python is strongly typed (i.e. types are

More information

Data Structures (list, dictionary, tuples, sets, strings)

Data Structures (list, dictionary, tuples, sets, strings) Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in brackets: l = [1, 2, "a"] (access by index, is mutable sequence) Tuples are enclosed in parentheses: t = (1, 2, "a") (access

More information

Introduction to Python! Lecture 2

Introduction to Python! Lecture 2 .. Introduction to Python Lecture 2 Summary Summary: Lists Sets Tuples Variables while loop for loop Functions Names and values Passing parameters to functions Lists Characteristics of the Python lists

More information

The SPL Programming Language Reference Manual

The SPL Programming Language Reference Manual The SPL Programming Language Reference Manual Leonidas Fegaras University of Texas at Arlington Arlington, TX 76019 fegaras@cse.uta.edu February 27, 2018 1 Introduction The SPL language is a Small Programming

More information

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

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D 1/60 Interactive use $ python Python 2.7.5 (default, Mar 9 2014, 22:15:05) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information.

More information

Advanced Algorithms and Computational Models (module A)

Advanced Algorithms and Computational Models (module A) Advanced Algorithms and Computational Models (module A) Giacomo Fiumara giacomo.fiumara@unime.it 2014-2015 1 / 34 Python's built-in classes A class is immutable if each object of that class has a xed value

More information

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York CSc 10200! Introduction to Computing Lecture 2-3 Edgardo Molina Fall 2013 City College of New York 1 C++ for Engineers and Scientists Third Edition Chapter 2 Problem Solving Using C++ 2 Objectives In this

More information

CIS192: Python Programming

CIS192: Python Programming CIS192: Python Programming Introduction Harry Smith University of Pennsylvania January 18, 2017 Harry Smith (University of Pennsylvania) CIS 192 Lecture 1 January 18, 2017 1 / 34 Outline 1 Logistics Rooms

More information

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

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D 1/58 Interactive use $ python Python 2.7.5 (default, Mar 9 2014, 22:15:05) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information.

More information

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes GIS 4653/5653: Spatial Programming and GIS More Python: Statements, Types, Functions, Modules, Classes Statement Syntax The if-elif-else statement Indentation and and colons are important Parentheses and

More information

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers) Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement

More information

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax Scheme Tutorial Introduction Scheme is an imperative language with a functional core. The functional core is based on the lambda calculus. In this chapter only the functional core and some simple I/O is

More information

egrapher Language Reference Manual

egrapher Language Reference Manual egrapher Language Reference Manual Long Long: ll3078@columbia.edu Xinli Jia: xj2191@columbia.edu Jiefu Ying: jy2799@columbia.edu Linnan Wang: lw2645@columbia.edu Darren Chen: dsc2155@columbia.edu 1. Introduction

More information

PYTHON CONTENT NOTE: Almost every task is explained with an example

PYTHON CONTENT NOTE: Almost every task is explained with an example PYTHON CONTENT NOTE: Almost every task is explained with an example Introduction: 1. What is a script and program? 2. Difference between scripting and programming languages? 3. What is Python? 4. Characteristics

More information

Downloaded from Chapter 2. Functions

Downloaded from   Chapter 2. Functions Chapter 2 Functions After studying this lesson, students will be able to: Understand and apply the concept of module programming Write functions Identify and invoke appropriate predefined functions Create

More information

Here n is a variable name. The value of that variable is 176.

Here n is a variable name. The value of that variable is 176. UNIT II DATA, EXPRESSIONS, STATEMENTS 9 Python interpreter and interactive mode; values and types: int, float, boolean, string, and list; variables, expressions, statements, tuple assignment, precedence

More information

Lecture Writing Programs. Richard E Sarkis CSC 161: The Art of Programming

Lecture Writing Programs. Richard E Sarkis CSC 161: The Art of Programming Lecture Writing Programs Richard E Sarkis CSC 161: The Art of Programming Class Administrivia Agenda To be able to understand and write Python statements to output information to the screen To assign values

More information

PYTHON. Varun Jain & Senior Software Engineer. Pratap, Mysore Narasimha Raju & TEST AUTOMATION ARCHITECT. CenturyLink Technologies India PVT LTD

PYTHON. Varun Jain & Senior Software Engineer. Pratap, Mysore Narasimha Raju & TEST AUTOMATION ARCHITECT. CenturyLink Technologies India PVT LTD PYTHON Varun Jain & Senior Software Engineer Pratap, Mysore Narasimha Raju & TEST AUTOMATION ARCHITECT CenturyLink Technologies India PVT LTD 1 About Python Python is a general-purpose interpreted, interactive,

More information

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

Unit-II Programming and Problem Solving (BE1/4 CSE-2) Unit-II Programming and Problem Solving (BE1/4 CSE-2) Problem Solving: Algorithm: It is a part of the plan for the computer program. An algorithm is an effective procedure for solving a problem in a finite

More information

Fundamentals of Programming (Python) Getting Started with Programming

Fundamentals of Programming (Python) Getting Started with Programming Fundamentals of Programming (Python) Getting Started with Programming Ali Taheri Sharif University of Technology Some slides have been adapted from Python Programming: An Introduction to Computer Science

More information

Lecture 2 Tao Wang 1

Lecture 2 Tao Wang 1 Lecture 2 Tao Wang 1 Objectives In this chapter, you will learn about: Modular programs Programming style Data types Arithmetic operations Variables and declaration statements Common programming errors

More information

[CHAPTER] 1 INTRODUCTION 1

[CHAPTER] 1 INTRODUCTION 1 FM_TOC C7817 47493 1/28/11 9:29 AM Page iii Table of Contents [CHAPTER] 1 INTRODUCTION 1 1.1 Two Fundamental Ideas of Computer Science: Algorithms and Information Processing...2 1.1.1 Algorithms...2 1.1.2

More information

Exam 1 Format, Concepts, What you should be able to do, and Sample Problems

Exam 1 Format, Concepts, What you should be able to do, and Sample Problems CSSE 120 Introduction to Software Development Exam 1 Format, Concepts, What you should be able to do, and Sample Problems Page 1 of 6 Format: The exam will have two sections: Part 1: Paper-and-Pencil o

More information

Absolute C++ Walter Savitch

Absolute C++ Walter Savitch Absolute C++ sixth edition Walter Savitch Global edition This page intentionally left blank Absolute C++, Global Edition Cover Title Page Copyright Page Preface Acknowledgments Brief Contents Contents

More information

Data Structures. Lists, Tuples, Sets, Dictionaries

Data Structures. Lists, Tuples, Sets, Dictionaries Data Structures Lists, Tuples, Sets, Dictionaries Collections Programs work with simple values: integers, floats, booleans, strings Often, however, we need to work with collections of values (customers,

More information

FRAC: Language Reference Manual

FRAC: Language Reference Manual FRAC: Language Reference Manual Justin Chiang jc4127 Kunal Kamath kak2211 Calvin Li ctl2124 Anne Zhang az2350 1. Introduction FRAC is a domain-specific programming language that enables the programmer

More information

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

CSE : Python Programming. Homework 5 and Projects. Announcements. Course project: Overview. Course Project: Grading criteria CSE 399-004: Python Programming Lecture 5: Course project and Exceptions February 12, 2007 Announcements Still working on grading Homeworks 3 and 4 (and 2 ) Homework 5 will be out by tomorrow morning I

More information

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement Outline Expression Evaluation and Control Flow In Text: Chapter 6 Notation Operator evaluation order Operand evaluation order Overloaded operators Type conversions Short-circuit evaluation of conditions

More information

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

Python Essential Reference, Second Edition - Chapter 5: Control Flow Page 1 of 8 Python Essential Reference, Second Edition - Chapter 5: Control Flow Page 1 of 8 Chapter 5: Control Flow This chapter describes related to the control flow of a program. Topics include conditionals, loops,

More information

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program Overview - General Data Types - Categories of Words - The Three S s - Define Before Use - End of Statement - My First Program a description of data, defining a set of valid values and operations List of

More information

COMP519 Web Programming Lecture 21: Python (Part 5) Handouts

COMP519 Web Programming Lecture 21: Python (Part 5) Handouts COMP519 Web Programming Lecture 21: Python (Part 5) Handouts Ullrich Hustadt Department of Computer Science School of Electrical Engineering, Electronics, and Computer Science University of Liverpool Functions

More information

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

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples Abstract Data Types CS 234, Fall 2017 Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples Data Types Data is stored in a computer as a sequence of binary digits:

More information

What is a class? Responding to messages. Short answer 7/19/2017. Code Listing 11.1 First Class. chapter 11. Introduction to Classes

What is a class? Responding to messages. Short answer 7/19/2017. Code Listing 11.1 First Class. chapter 11. Introduction to Classes chapter 11 Code Listing 11.1 First Class Introduction to Classes What is a class? If you have done anything in computer science before, you likely will have heard the term object oriented programming (OOP)

More information

Python Intro GIS Week 1. Jake K. Carr

Python Intro GIS Week 1. Jake K. Carr GIS 5222 Week 1 Why Python It s simple and easy to learn It s free - open source! It s cross platform IT S expandable!! Why Python: Example Consider having to convert 1,000 shapefiles into feature classes

More information

Course Outline - COMP150. Lectures and Labs

Course Outline - COMP150. Lectures and Labs Course Outline - COMP150 Lectures and Labs 1 The way of the program 1.1 The Python programming language 1.2 What is a program? 1.3 What is debugging? 1.4 Experimental debugging 1.5 Formal and natural languages

More information

Part III Appendices 165

Part III Appendices 165 Part III Appendices 165 Appendix A Technical Instructions Learning Outcomes This material will help you learn how to use the software you need to do your work in this course. You won t be tested on it.

More information

Variables, expressions and statements

Variables, expressions and statements Variables, expressions and statements 2.1. Values and data types A value is one of the fundamental things like a letter or a number that a program manipulates. The values we have seen so far are 2 (the

More information

Some material adapted from Upenn cmpe391 slides and other sources

Some material adapted from Upenn cmpe391 slides and other sources Some material adapted from Upenn cmpe391 slides and other sources History Installing & Running Python Names & Assignment Sequences types: Lists, Tuples, and Strings Mutability Understanding Reference Semantics

More information

Object-Oriented Programming

Object-Oriented Programming iuliana@cs.ubbcluj.ro Babes-Bolyai University 2018 Overview 1 2 3 4 5 6 7 I No beard, no belly, no guru... Ken Thompson (B), Dennis Ritchie (C) - UNIX Bjarne Stroustrup (C++) James Gosling (Java) Figure:

More information

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

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an  . Michigan State University CSE 231, Fall 2013 CSE 231, Rich Enbody Office Hours After class By appointment send an email 2 1 Project 1 Python arithmetic Do with pencil, paper and calculator first Idle Handin Help room 3 What is a Computer Program?

More information

CIS192 Python Programming. Robert Rand. August 27, 2015

CIS192 Python Programming. Robert Rand. August 27, 2015 CIS192 Python Programming Introduction Robert Rand University of Pennsylvania August 27, 2015 Robert Rand (University of Pennsylvania) CIS 192 August 27, 2015 1 / 30 Outline 1 Logistics Grading Office

More information

Functions: Decomposition And Code Reuse

Functions: Decomposition And Code Reuse Programming: problem decomposition into functions 1 Functions: Decomposition And Code Reuse This section of notes shows you how to write functions that can be used to: decompose large problems, and to

More information

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python Python Sequence Types Sequence types str and bytes are sequence types Sequence types have several operations defined for them Indexing Python Sequence Types Each element in a sequence can be extracted

More information

Getting Started with Python

Getting Started with Python Fundamentals of Programming (Python) Getting Started with Python Sina Sajadmanesh Sharif University of Technology Some slides have been adapted from Python Programming: An Introduction to Computer Science

More information

1 Lexical Considerations

1 Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

More information

PREPARING FOR THE FINAL EXAM

PREPARING FOR THE FINAL EXAM PREPARING FOR THE FINAL EXAM CS 1110: FALL 2017 This handout explains what you have to know for the final exam. Most of the exam will include topics from the previous two prelims. We have uploaded the

More information

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CMSC 201 Fall 2016 Lab 09 Advanced Debugging CMSC 201 Fall 2016 Lab 09 Advanced Debugging Assignment: Lab 09 Advanced Debugging Due Date: During discussion Value: 10 points Part 1: Introduction to Errors Throughout this semester, we have been working

More information

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square) CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square) Introduction This semester, through a project split into 3 phases, we are going

More information

Pace University. Fundamental Concepts of CS121 1

Pace University. Fundamental Concepts of CS121 1 Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction

More information

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore CS 115 Data Types and Arithmetic; Testing Taken from notes by Dr. Neil Moore Statements A statement is the smallest unit of code that can be executed on its own. So far we ve seen simple statements: Assignment:

More information

Webgurukul Programming Language Course

Webgurukul Programming Language Course Webgurukul Programming Language Course Take One step towards IT profession with us Python Syllabus Python Training Overview > What are the Python Course Pre-requisites > Objectives of the Course > Who

More information

CS 320: Concepts of Programming Languages

CS 320: Concepts of Programming Languages CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 04: Basic Haskell Continued o Polymorphic Types o Type Inference with Polymorphism o Standard

More information

Glossary. For Introduction to Programming Using Python By Y. Daniel Liang

Glossary. For Introduction to Programming Using Python By Y. Daniel Liang Chapter 1 Glossary For Introduction to Programming Using Python By Y. Daniel Liang.py Python script file extension name. assembler A software used to translate assemblylanguage programs into machine code.

More information

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Compiler Theory. (Semantic Analysis and Run-Time Environments) Compiler Theory (Semantic Analysis and Run-Time Environments) 005 Semantic Actions A compiler must do more than recognise whether a sentence belongs to the language of a grammar it must do something useful

More information

What is a Class? Short Answer. Responding to Messages. Everything in Python is an Object 11/8/2010

What is a Class? Short Answer. Responding to Messages. Everything in Python is an Object 11/8/2010 The Practice of Computing Using PYTHON William Punch Richard Enbody Chapter 11 Introduction to Classes class Student(object): """Simple Student class.""" def init (self,first='', last='', id=0): # init

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

DaMPL. Language Reference Manual. Henrique Grando

DaMPL. Language Reference Manual. Henrique Grando DaMPL Language Reference Manual Bernardo Abreu Felipe Rocha Henrique Grando Hugo Sousa bd2440 flt2107 hp2409 ha2398 Contents 1. Getting Started... 4 2. Syntax Notations... 4 3. Lexical Conventions... 4

More information

And Parallelism. Parallelism in Prolog. OR Parallelism

And Parallelism. Parallelism in Prolog. OR Parallelism Parallelism in Prolog And Parallelism One reason that Prolog is of interest to computer scientists is that its search mechanism lends itself to parallel evaluation. In fact, it supports two different kinds

More information

Introduction to Python

Introduction to Python Introduction to Python Reading assignment: Perkovic text, Ch. 1 and 2.1-2.5 Python Python is an interactive language. Java or C++: compile, run Also, a main function or method Python: type expressions

More information

Beyond Blocks: Python Session #1

Beyond Blocks: Python Session #1 Beyond Blocks: Session #1 CS10 Spring 2013 Thursday, April 30, 2013 Michael Ball Beyond Blocks : : Session #1 by Michael Ball adapted from Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike

More information

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1 Review Midterm Exam 1 Review Midterm Exam 1 Exam on Monday, October 7 Data Types and Variables = Data Types and Variables Basic Data Types Integers Floating Point Numbers Strings Data Types and Variables

More information

COMP 204: Sets, Commenting & Exceptions

COMP 204: Sets, Commenting & Exceptions COMP 204: Sets, Commenting & Exceptions Yue Li based on material from Mathieu Blanchette, Carlos Oliver Gonzalez and Christopher Cameron 1/29 Outline Quiz 14 review Set Commenting code Bugs 2/29 Quiz 15

More information

Lists, loops and decisions

Lists, loops and decisions Caltech/LEAD Summer 2012 Computer Science Lecture 4: July 11, 2012 Lists, loops and decisions Lists Today Looping with the for statement Making decisions with the if statement Lists A list is a sequence

More information

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

CS2304: Python for Java Programmers. CS2304: Sequences and Collections CS2304: Sequences and Collections Sequences In Python A sequence type in python supports: The in membership operator. The len() function. Slicing like we saw with strings, s[1:3]. And is iterable (for

More information

About Python. Python Duration. Training Objectives. Training Pre - Requisites & Who Should Learn Python

About Python. Python Duration. Training Objectives. Training Pre - Requisites & Who Should Learn Python About Python Python course is a great introduction to both fundamental programming concepts and the Python programming language. By the end, you'll be familiar with Python syntax and you'll be able to

More information

ENGR 102 Engineering Lab I - Computation

ENGR 102 Engineering Lab I - Computation ENGR 102 Engineering Lab I - Computation Learning Objectives by Week 1 ENGR 102 Engineering Lab I Computation 2 Credits 2. Introduction to the design and development of computer applications for engineers;

More information

Decaf Language Reference Manual

Decaf Language Reference Manual Decaf Language Reference Manual C. R. Ramakrishnan Department of Computer Science SUNY at Stony Brook Stony Brook, NY 11794-4400 cram@cs.stonybrook.edu February 12, 2012 Decaf is a small object oriented

More information

COMP 204: Sets, Commenting & Exceptions

COMP 204: Sets, Commenting & Exceptions COMP 204: Sets, Commenting & Exceptions Material from Carlos G. Oliver, Christopher J.F. Cameron October 12, 2018 1/31 Reminder CSUS is holding a midterm review session on Monday, October 15th, from 6-9pm.

More information

Python Evaluation Rules

Python Evaluation Rules Python Evaluation Rules UW CSE 160 https://courses.cs.washington.edu/courses/cse160/15sp/ Michael Ernst and Isaac Reynolds mernst@cs.washington.edu April 1, 2015 Contents 1 Introduction 2 1.1 The Structure

More information

Programming in Python 3

Programming in Python 3 Programming in Python 3 Programming transforms your computer from a home appliance to a power tool Al Sweigart, The invent with Python Blog Programming Introduction Write programs that solve a problem

More information

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */ Overview Language Basics This chapter describes the basic elements of Rexx. It discusses the simple components that make up the language. These include script structure, elements of the language, operators,

More information

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309 A Arithmetic operation floating-point arithmetic, 11 12 integer numbers, 9 11 Arrays, 97 copying, 59 60 creation, 48 elements, 48 empty arrays and vectors, 57 58 executable program, 49 expressions, 48

More information

Chapter 9: Dealing with Errors

Chapter 9: Dealing with Errors Chapter 9: Dealing with Errors What we will learn: How to identify errors Categorising different types of error How to fix different errors Example of errors What you need to know before: Writing simple

More information

Collections. Lists, Tuples, Sets, Dictionaries

Collections. Lists, Tuples, Sets, Dictionaries Collections Lists, Tuples, Sets, Dictionaries Homework notes Homework 1 grades on canvas People mostly lost points for not reading the document carefully Didn t play again Didn t use Y/N for playing again

More information

Chapter 2 Basic Elements of C++

Chapter 2 Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2-1 Chapter 2 Basic Elements of C++ At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion

More information

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts COMP519 Web Programming Lecture 17: Python (Part 1) Handouts Ullrich Hustadt Department of Computer Science School of Electrical Engineering, Electronics, and Computer Science University of Liverpool Contents

More information

PREPARING FOR PRELIM 2

PREPARING FOR PRELIM 2 PREPARING FOR PRELIM 2 CS 1110: FALL 2012 This handout explains what you have to know for the second prelim. There will be a review session with detailed examples to help you study. To prepare for the

More information

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS Chapter 1 : Chapter-wise Java Multiple Choice Questions and Answers Interview MCQs Java Programming questions and answers with explanation for interview, competitive examination and entrance test. Fully

More information

LECTURE 16. Functional Programming

LECTURE 16. Functional Programming LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative

More information

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010

IPCoreL. Phillip Duane Douglas, Jr. 11/3/2010 IPCoreL Programming Language Reference Manual Phillip Duane Douglas, Jr. 11/3/2010 The IPCoreL Programming Language Reference Manual provides concise information about the grammar, syntax, semantics, and

More information

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING GE8151 - PROBLEM SOVING AND PYTHON PROGRAMMING Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING 1) Define Computer 2) Define algorithm 3) What are the two phases in algorithmic problem solving? 4) Why

More information

ENGR 101 Engineering Design Workshop

ENGR 101 Engineering Design Workshop ENGR 101 Engineering Design Workshop Lecture 2: Variables, Statements/Expressions, if-else Edgardo Molina City College of New York Literals, Variables, Data Types, Statements and Expressions Python as

More information

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces Basic memory model Using functions Writing functions Basics Prototypes Parameters Return types Functions and memory Names and namespaces When a program runs it requires main memory (RAM) space for Program

More information

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion CSC 148 Lecture 3 Dynamic Typing, Scoping, and Namespaces Recursion Announcements Python Ramp Up Session Monday June 1st, 1 5pm. BA3195 This will be a more detailed introduction to the Python language

More information

Java+- Language Reference Manual

Java+- Language Reference Manual Fall 2016 COMS4115 Programming Languages & Translators Java+- Language Reference Manual Authors Ashley Daguanno (ad3079) - Manager Anna Wen (aw2802) - Tester Tin Nilar Hlaing (th2520) - Systems Architect

More information

RSL Reference Manual

RSL Reference Manual RSL Reference Manual Part No.: Date: April 6, 1990 Original Authors: Klaus Havelund, Anne Haxthausen Copyright c 1990 Computer Resources International A/S This document is issued on a restricted basis

More information

LECTURE 18. Control Flow

LECTURE 18. Control Flow LECTURE 18 Control Flow CONTROL FLOW Sequencing: the execution of statements and evaluation of expressions is usually in the order in which they appear in a program text. Selection (or alternation): a

More information

3. Logical Values. Boolean Functions; the Type bool; logical and relational operators; shortcut evaluation

3. Logical Values. Boolean Functions; the Type bool; logical and relational operators; shortcut evaluation 140 3. Logical Values Boolean Functions; the Type bool; logical and relational operators; shortcut evaluation Our Goal 141 int a; std::cin >> a; if (a % 2 == 0) std::cout

More information