Presentation is loading. Please wait.

Presentation is loading. Please wait.

Winter 2018 CISC101 12/5/2018 CISC101 Reminders

Similar presentations


Presentation on theme: "Winter 2018 CISC101 12/5/2018 CISC101 Reminders"— Presentation transcript:

1 Winter 2018 CISC101 12/5/2018 CISC101 Reminders Quiz 3 this week in lab. Topics in slides from last Tuesday. Turtle not on quiz. Sets not on quiz. Keyword and Default Arguments on quiz. Assignment 4 due next Friday. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

2 Computing Undergraduate Information Night
If you are interested in going any further in Computing: Thursday, March 15 TODAY! 5:30 to 7:30pm Walter Light Hall, Rm. 210 Speakers from each program, Pizza and drinks! RSVP to Karen Knight at Winter 2018 CISC101 - Prof. McLeod

3 Today Continue Strings. Finish StringMethodsDemo.py.
Some useful “odds ‘n ends”: os and sys modules. exec() BIF. Winter 2018 CISC101 - Prof. McLeod

4 Demo of String Methods Starts with the tuple or list returns methods, then looks at how you could analyze larger amounts of text. See StringMethodsDemo.py This demo did not work with a very large amount of text, but the same techniques could easily be applied to much larger documents – newspapers, transcribed speeches, books, texts, s,… Python text manipulation code is very compact and can be quite powerful. Winter 2018 CISC101 - Prof. McLeod

5 Could You Write a String Method?
Knowing only: ASCII character codes (or how to get them). How to loop through a string String operators And the len(), ord() and chr() BIFS Could you write all of the string methods yourself? Sure!! Winter 2018 CISC101 - Prof. McLeod

6 You Can Write These! But, since we don’t know how to extend the string class in order to add methods to it, let’s just write a function instead. Imitate something like .swapcase(), for example: Write a function called upperLower (?) that changes upper case letters to lower case and lower case letters to upper case. See TestUpperLower.py Winter 2018 CISC101 - Prof. McLeod

7 os Module This module is very helpful if you have to do quite a bit of work with files and folders. Basically the module provides an interface to your OS. Winter 2018 CISC101 - Prof. McLeod

8 CISC101 os Module Behind the scenes, this module loads a module for your particular operating system. So regardless of your OS, you import os… See section 16.1 in the Python Library Reference. Lots of goodies, particularly file system utilities. Here’s a selection of file-related functions: Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

9 os Module, Cont. remove() – deletes a file rename() – renames a file
walk() – generates filenames in a directory tree (a generator object) chdir() – change working directory chroot() – change root directory of current process listdir() – list files and folders in directory getcwd() – gets current working directory mkdir() – creates directory Winter 2018 CISC101 - Prof. McLeod

10 os Module, Cont. rmdir() – removes directory
access() – verify permission modes (2 parameters) chmod() – change permission modes In os.path: basename() – returns name of file dirname() – returns name of directory join() – joins directory and filename split() – returns both as tuple splitext() – returns filename and extension as tuple Winter 2018 CISC101 - Prof. McLeod

11 os Module, Cont. In os.path, Cont:
getatime() – returns last file access time getctime() – returns file creation time getmtime() – returns file modification time getsize() – returns file size in bytes exists() – does file or directory name exist? isdir() – is this a directory name and does it exist? isfile() – is this a file and does it exist? Winter 2018 CISC101 - Prof. McLeod

12 os Module, Cont. Demo program – see LargeFileSearch.py
Uses a recursive directory search – don’t worry about this technique – you are not responsible for knowing how to use recursion. Winter 2018 CISC101 - Prof. McLeod

13 os Module, Cont. Python already has an exec() BIF that allows you to run external python code from within your program. The os module also has many commands that allow you to run other non-python programs from within your program. For example, os.system() allows you to run a system command (a DOS command, for example). Winter 2018 CISC101 - Prof. McLeod

14 sys Module See section 28.1 in the Python Library Reference.
CISC101 sys Module See section 28.1 in the Python Library Reference. More system functions and attributes. Here is a small sampling: argv – is a list of all command line parameters sent to the interpreter. builtin_module_names – a list of all built-in modules. exit(0) – immediately exits a python program giving a zero exit status code. getwindowsversion() – returns a tuple consisting of major, minor, build, platform, and service pack status. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

15 sys Module, Cont. path – a list of the module search paths used by python. platform – the current OS platform. prefix – the folder where python is located. version – the version of python being used. See sysModuleDemo.py Winter 2018 CISC101 - Prof. McLeod

16 sys Module, Cont. Not so much about functionality.
Mostly about getting information about how Python is working, along with more information about your OS. Winter 2018 CISC101 - Prof. McLeod

17 CISC101 The exec() BIF You can use the os module to execute code in many other languages from a python program. But there is already an easy-to-use BIF, exec(), that can execute python code if it is supplied to the BIF as a string. The string could come from a file, for example. Winter 2018 CISC101 - Prof. McLeod Prof. Alan McLeod

18 The exec() BIF, Cont. See RemoteFileExecution.py which uses the Message.py file. You must supply the output of the BIF globals() to the exec() BIF – this supplies a dictionary of all the global variables currently in use and their values. Winter 2018 CISC101 - Prof. McLeod


Download ppt "Winter 2018 CISC101 12/5/2018 CISC101 Reminders"

Similar presentations


Ads by Google