Winter 2019 CISC101 4/29/2019 CISC101 Reminders

Slides:



Advertisements
Similar presentations
Today Quiz solutions are posted on the Grading page. Assignment 2 is posted. Due the first Friday after Reading Week. All about null Start File Input/Output.
Advertisements

Today Assignment 2 is posted. Due the first Friday after Reading Week. (“Virtual” lecture looked at the use of try/catch and try with resources blocks.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 13 Files and Exception Handling 1.
Winter 2006CISC121 - Prof. McLeod1 Last Time Misc. useful classes in Java: –String –StringTokenizer –Math –System.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
Today… The for loop. Introducing the Turtle! Loops and Drawing. Winter 2016CISC101 - Prof. McLeod1.
Today… Strings: –String Methods Demo. Raising Exceptions. os Module Winter 2016CISC101 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
Today… Files from the Web! Dictionaries. Lists of lists. Winter 2016CISC101 - Prof. McLeod1.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
FILE I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
Today… Loops and Drawing, Cont. –Two slightly more advanced demos. Collections Overview. Winter 2016CISC101 - Prof. McLeod1.
Topic: File Input/Output (I/O)
Chapter 8 Text Files We have, up to now, been storing data only in the variables and data structures of programs. However, such data is not available.
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
CMPE212 – Stuff… Assn 2 due next Friday. Winter 2018
Exceptions and files Taken from notes by Dr. Neil Moore
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
CISC101 Reminders Quiz 2 this week.
CISC101 Reminders Quiz 2 this week.
CISC101 Reminders Assn 3 due Friday, this week.
Winter 2018 CISC101 11/16/2018 CISC101 Reminders
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
File IO and Strings CIS 40 – Introduction to Programming in Python
Using files Taken from notes by Dr. Neil Moore
CISC101 Reminders Assn 3 due Friday, this week. Quiz 3 next week.
Exceptions and files Taken from notes by Dr. Neil Moore
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Fall 2018 CISC124 12/1/2018 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until.
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Winter 2018 CISC101 12/5/2018 CISC101 Reminders
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
CISC101 Reminders Quiz 2 this week.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
Winter 2019 CISC101 2/17/2019 CISC101 Reminders
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
CISC101 Reminders All assignments are now posted.
CISC101 Reminders Assn 3 sample solution is posted.
Topics Introduction to File Input and Output
CISC101 Reminders Assignment 2 due today.
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Winter 2019 CISC101 5/17/2019 CISC101 Reminders
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
Topics Introduction to File Input and Output
CISC101 Reminders Assignment 3 due today.
Winter 2019 CISC101 5/30/2019 CISC101 Reminders
Chapter 1 c++ structure C++ Input / Output
CMPE212 – Reminders Assignment 2 due next Friday.
Presentation transcript:

Winter 2019 CISC101 4/29/2019 CISC101 Reminders Assignment 3 due next Friday. The next few lectures will focus on what you need to know for this assignment. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

Winter 2019 CISC101 4/29/2019 Today Assignment 3 Topics. For assignment 3 you will build a real spell checker program. Sets. Text File Input/Output. Start Exceptions, if we have time. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

Assn 3 Topics Read a text file into a list. Use the slice operator with strings. Build lists using the .append() method. Combine lists together (using +). Use set methods .intersection() and .difference(). Use len() BIF with lists and sets. Use in keyword to search sets or lists. Build functions! Winter 2019 CISC101 - Prof. McLeod

Assn 3 Topics, Cont. The assignment tells you: What you need to know to use sets (but we’ll cover them in class, anyways…). What functions you need to write. However, you will need to figure out what arguments the functions need and what they need to return. Winter 2019 CISC101 - Prof. McLeod

Assn 3 Topics, Cont. We still need to cover: Sets. Text File Input. Building Functions. Winter 2019 CISC101 - Prof. McLeod

Sets One of the Python collection types. Makes your work in Assn 3 much easier! See section 5.4 in the Python Tutorial and look at “Set Types” in the “Built-in Types” section in the Python Standard Library. Focus on just those aspects of a set collection that you need for the assignment. Winter 2019 CISC101 - Prof. McLeod

Mathematical Set Operations Set Union operation: Set Intersection operation: All elements from both sets. Duplicates are eliminated. Just elements that are common to both sets. Winter 2019 CISC101 - Prof. McLeod

Mathematical Set Operations, Cont. Set Difference operation: There are other operations, but these are the most common ones. The assignment makes the most use of Intersection, but Difference might also be useful. The elements from set “A” that are not in set “B”. Winter 2019 CISC101 - Prof. McLeod

Sets, Cont. Suppose you have a set of candidate words and you have the dictionary of words as a set: In order to find out which of the candidate words are actually words, carry out an Intersection operation. If you have a set of the high probability words and a set of the low probability words carry out a Difference to eliminate the words from the other set that are the same. Winter 2019 CISC101 - Prof. McLeod

Sets in Python A set is mutable (use frozenset if you want an immutable set). Use the set() BIF to create a set from a list. Duplicates will be eliminated. A set can contain a mix of numbers and strings. A set is hashed, not indexed. So the slice operator and other operations like sorting and indexing will not work. You cannot order set elements. Things like in, len() and the for loop will work (but you don’t need any of these in the assignment). Winter 2019 CISC101 - Prof. McLeod

Aside – What is “Hashing”? Each element in a hashable collection is assigned a numeric value. The numbers will all be different, since each element is different. The numeric value makes it very easy to quickly find out the memory address of where the element is stored. Access to the element is through its hash code, not through its index value. The dictionary collection type is also hashed. Winter 2019 CISC101 - Prof. McLeod

Sets in Python, Cont. To carry out an Intersection, invoke the .intersection method and supply the other set as an argument. For the Difference operation, invoke the .difference method. See some sample code on the next slide: Winter 2019 CISC101 - Prof. McLeod

Python Set Interpreter Demo Winter 2019 CISC101 - Prof. McLeod

File I/O (“Input/Output”) CISC101 File I/O (“Input/Output”) Files provide a convenient way to store and re-store to memory larger amounts of data. Use a data structure like a list to store the data in memory. Three kinds of file I/O: Text Binary Random access For simplicity we will concentrate on text I/O in this course. Text files can be read by Notepad, for example. Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

file_variable = open(filename, mode) Text File I/O, Cont. Syntax: file_variable = open(filename, mode) filename is the name of a file in the same folder as your program, as a string. mode is also a string: ‘r’ for reading only ‘w’ for writing only ‘a’ for appending to a file The default mode is ‘r’, if you don’t supply the second argument. Winter 2019 CISC101 - Prof. McLeod

Aside - Other File Modes + - read and write (same as w+ or a+) rb - binary read wb - binary write ab - binary append rb+ - binary read and write (same as wb+ and ab+) Winter 2019 CISC101 - Prof. McLeod

Another Aside - Newline Support Different OSs (Linux, Mac, Windows) may use different line termination sequences, such as '\n', '\r\n' or '\r'. If you have difficulty reading individual lines, specify the newline sequence using the keyword argument: newline='\n' in the open() BIF. Try different escape sequences, until the file is read properly. Winter 2019 CISC101 - Prof. McLeod

Text File Output Warning: if you open an existing file for writing using mode ‘w’, the old file will be overwritten with a new file - all the old contents will be lost. If you want to add to an existing file without erasing the old contents, use the ‘a’ mode for appending. If you do not provide a path, the file is created in the same folder as your program. If you do put a path in the filename, remember to use \\ as your folder delimiter. Winter 2019 CISC101 - Prof. McLeod

Text File Output, Cont. To write information to a file, use the write() method: file_variable.write(a_string) Note that the write() method does not add a line terminator to the end of the string, so if you want to write a line, and have the next output go to the next line, you need something like: file_variable.write(a_string + '\n') Winter 2019 CISC101 - Prof. McLeod

Text File Output, Cont. Once you are finished writing to the file don’t forget to close the file using: file_variable.close() If you don’t do this, you run the risk of leaving a corrupted file on your hard disk! This method also releases the file resource back to the OS. Winter 2019 CISC101 - Prof. McLeod

Sequential File Access Text file I/O uses sequential access. Think of having a little “pointer” in the file marking the end of what you have read. As you read (or write), the pointer moves ahead. The pointer cannot move backwards. The only way to re-read something is to close the file and open it again - this moves the “pointer” back to the beginning. start end Winter 2019 CISC101 - Prof. McLeod

Aside - Random File Access This can be used only with binary files: Seek a certain byte location in the file (you must know the exact structure of the file). Read or write data from this location. Seek again… Winter 2019 CISC101 - Prof. McLeod

Text File Input Use the open() method as shown above. Use the readline() method to read a line (up to and including a linefeed character). This method returns a string. You might wish to use something like the string rstrip() method on the string to remove the linefeed, and any other whitespace at the end of the string. Winter 2019 CISC101 - Prof. McLeod

Text File Input, Cont. Other file reading methods: read() – reads entire file and returns a single string. readlines() – reads entire file and returns a list of lines of text. Winter 2019 CISC101 - Prof. McLeod

Text File Input, Cont. Invoke the close() method when you are done reading. A for loop can simplify input, because a file object is iterable: for line in inFile: fileContentsList.append(line.rstrip()) Winter 2019 CISC101 - Prof. McLeod

File Read and Write Errors Suppose a problem occurs: File does not exist. Folder does not exist. You do not have read or write access in the folder you are attempting to use. Access fails partway through a read or write attempt. What happens and what should we do about it? See FileInput.py Winter 2019 CISC101 - Prof. McLeod

File Read and Write Errors, Cont. How about using a try/except structure to catch the FileNotFoundError exception? See RobustFileInput.py Winter 2019 CISC101 - Prof. McLeod

Back To: The .index() List Method This method returns the index location of the first match to the supplied argument in a list. What does the method do if it cannot find a match? Winter 2019 CISC101 - Prof. McLeod

.index() List Method, Cont. How can you prevent this method from crashing your program? Two possibilities: Use the in keyword to see if the value is in the list at all, then invoke .index to find the location: if 100 in randNums : print(randNums.index(100)) else : print("100 is not in the list!") Winter 2019 CISC101 - Prof. McLeod

.index() List Method, Cont. The problem with this is that it is “costly” – you have to search through the list twice. How about letting the error occur, but prevent it from crashing your program and then allow the program to carry on? This can be done using a try-except construct. Winter 2019 CISC101 - Prof. McLeod

CISC101 Exceptions BIFs and methods throw exceptions as a means of indicating that they cannot do their job. Kind of like a temper tantrum! Observed as all that red coloured font when your program crashes: Winter 2019 CISC101 - Prof. McLeod Prof. Alan McLeod

What is an Exception? What do you see if you try something like: print(int("Hello!")) >>> print(int("Hello")) Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> print(int("Hello")) ValueError: invalid literal for int() with base 10: 'Hello' Winter 2019 CISC101 - Prof. McLeod

What is an Exception?, Cont. A syntax error is what you get when your syntax cannot be recognized by the interpreter. All other errors occur when your code is running. An exception is a run-time error. Every run-time error in Python has a name – this is the type of the exception. For a list of exception types see Chapter 5 in the Python Standard Library documentation. The example on the previous slide was a “ValueError” exception. Winter 2019 CISC101 - Prof. McLeod

Crash Prevention! Normally an exception is what you see when your program has crashed from a fatal error. Better programs catch exceptions before this happens! Or – they make sure the exception will never occur by checking for error conditions ahead of time! Catching exceptions gives you a chance to fix the problem. You catch exceptions using try-except statements: Winter 2019 CISC101 - Prof. McLeod