CS2021 Python Programming Week 3 Systems Programming PP-Part II.

Slides:



Advertisements
Similar presentations
Chapter Modules CSC1310 Fall Modules Modules Modules are the highest level program organization unit, usually correspond to source files and.
Advertisements

I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
Introduction to Python. Python is a high-level programming language Open source and community driven “Batteries Included” – a standard distribution includes.
Computer Science 111 Fundamentals of Programming I Files.
Geography 465 Modules and Functions Writing Custom Functions.
1 Drawing phylogenies We've seen several tree data structures but we still can't draw a tree  In the tree drawing exercise we write a drawtree function.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
Python Control of Flow.
Guide to Programming with Python Chapter Nine Working with/Creating Modules.
Introduction to Shell Script Programming
Introduction to Python John Reiser May 5 th, 2010.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Python 0 Some material adapted from Upenn cmpe391 slides and other sources.
Functions Reading/writing files Catching exceptions
Python Modules An Introduction. Introduction A module is a file containing Python definitions and statements. The file name is the module name with the.
GNU Compiler Collection (GCC) and GNU C compiler (gcc) tools used to compile programs in Linux.
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
PHP - Basic Language Constructs CSCI 297 Scripting Languages - Day Two.
Agenda Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Review next lab assignments Break Out Problems.
By James Braunsberg. What are Modules? Modules are files containing Python definitions and statements (ex. name.py) A module’s definitions can be imported.
Libraries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Module 6: Geoprocessing Scripts. Processing loops and decisions.
Getting Started with Python: Constructs and Pitfalls Sean Deitz Advanced Programming Seminar September 13, 2013.
Overview Intro to functions What are functions? Why use functions? Defining functions Calling functions Documenting functions Top-down design Variable.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Python Functions.
Dictionaries.   Review on for loops – nested for loops  Dictionaries (p.79 Learning Python)  Sys Module for system arguments  Reverse complementing.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
CS105 Computer Programming PYTHON (based on CS 11 Python track: lecture 1, CALTECH)
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
GE3M25: Computer Programming for Biologists Python, Class 5
Python’s Standard Library Part I Joe Houpert CS265.
Python  Monty or Snake?. Monty?  Spam, spam, spam and eggs  Dead parrots  Eric Idle, John Cleese, Michael Palin, etc.
PC204 Lecture 5 Conrad Huang Genentech Hall, N453A
Lecture 4 Python Basics Part 3.
Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.
LECTURE 2 Python Basics. MODULES So, we just put together our first real Python program. Let’s say we store this program in a file called fib.py. We have.
Python Files and Lists. Files  Chapter 9 actually introduces you to opening up files for reading  Chapter 14 has more on file I/O  Python can read.
Python Documentation Fran Fitzpatrick. Overview  Comments  Documentation Strings  Pydoc  Comments  Documentation Strings  Pydoc.
Today… Strings: –String Methods Demo. Raising Exceptions. os Module Winter 2016CISC101 - Prof. McLeod1.
Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Tips. Iteration On a list: o group = ["Paul","Duncan","Jessica"] for person in group: print(group) On a dictionary: o stock = {'eggs':15, 'milk':3, 'sugar':28}
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Lecture III Syntax ● Statements ● Output ● Variables ● Conditions ● Loops ● List Comprehension ● Function Calls ● Modules.
Python’s Modules Noah Black.
CSC 458– Predictive Analytics I, Fall 2017, Intro. To Python
Intro to Python Programming – Part II
CSC1018F: Functional Programming
Think What will be the output?
Python Lesson 12 Mr. Kalmes.
CHAPTER FOUR Functions.
Topics Introduction to File Input and Output
Fundamentals of Programming I Files
Winter 2018 CISC101 12/5/2018 CISC101 Reminders
CSC 458– Predictive Analytics I, Fall 2018, Intro. To Python
Notes about Homework #4 Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming:
Python programming exercise
CISC101 Reminders Assn 3 sample solution is posted.
CSCE 590 Web Scraping – Scrapy II
Python Modules.
Bryan Burlingame 17 April 2019
Topics Introduction to File Input and Output
Lists Like tuples, but mutable. Formed with brackets: Assignment: >>> a = [1,2,3] Or with a constructor function: a = list(some_other_container) Subscription.
Review We've seen that a module is a file that can contain classes as well as its own variables. We've seen that you need to import it to access the code,
Python Reserved Words Poster
Presentation transcript:

CS2021 Python Programming Week 3 Systems Programming PP-Part II

Python Documentation $open /Library/Frameworks/Python.framework/Versions/3.4/Resourc es/English.lproj/Documentation/index.html

Searching and Finding Documents If you cannot locate docs on local machine use web address or Search for docs on sys.getrecursionlimit()

Exploring Sys Module Run the following >>> import sys >>> dir(sys) >>> help(sys) >>>help(sys.getrecursionlimit)

Experiment with recursion limit >>> sys.setrecursionlimit(50) >>> sys.getrecursionlimit() =>50 >>>def countdownrec(n): if n >0: print(n) countdownrec(n-1) >>>countdown(52)

More.py Load PP4E/System/more.py ___________________________________________________ def more(text, numlines=15): lines = text.splitlines() # like split('\n') but no '' at end while lines: chunk = lines[:numlines] lines = lines[numlines:] for line in chunk: print(line) if lines and input('More?') not in ['y', 'Y']: break if __name__ == '__main__’: import sys # when run, not imported more(open(sys.argv[1]).read(), 10) # page contents of file on cmdline

2 ways for running more.py $cd PP4E/System $python >>>from more import more >>>import sys >>>more(sys.__doc__) $cd PP4E/System $python more.py more.py

sys.argv #file arguments.py import sys # it's easy to print this list of course: print sys.argv # or it can be iterated via a for loop: for i in range(len(sys.argv)): if i == 0: print ("Function name: %s" % sys.argv[0]) else: print ("%d. argument: %s" % (i,sys.argv[i]))

Experiment with sys.argv $ python arguments.py arg1 arg2 ['arguments.py', 'arg1', 'arg2'] Function name: arguments.py 1. argument: arg1 2. argument: arg2 $

PP4E/System/testargv2.py "collect command-line options in a dictionary" def getopts(argv): opts = {} while argv: if argv[0][0] == '-': # find "-name value" pairs opts[argv[0]] = argv[1] # dict key is "-name" arg argv = argv[2:] else: argv = argv[1:] return opts if __name__ == '__main__': from sys import argv # example client code myargs = getopts(argv) if '-i' in myargs: print(myargs['-i']) print(myargs) $C:\...\PP2E\System>python testargv2.py {} $C:\...\PP2E\System>python testargv2.py -i data.txt -o results.txt {'-o': 'results.txt', '-i': 'data.txt'}

CWD and Import Paths #whereami.py import os, sys print('my os.getcwd =>', os.getcwd()) print('my sys.path =>', sys.path[:6]) # shows first 6 import paths input() Testing whereami.py $cd PP4E/System $python whereami.py $cd.. $python System/whereami.py $cd System/temp $python../whereami.py

Directory Tools (PP pg 163) A common system task is to process a set of files in a directory (a folder) Three ways to scan a directory – os.popen(‘ls’) – os.listdir(‘.’) – glob.glob(‘*’) >>> os.chdir('/Users/fredannexstein/Google Drive/PP4E') >>> os.popen('ls').readlines() ['Ai\n', 'Dbase\n', 'Dstruct\n', 'Gui\n', 'Integrate\n', 'Internet\n', 'Lang\n', 'LaunchBrowser.pyw\n', 'Launch_PyDemos.pyw\n', 'Launch_PyGadgets_bar.pyw\n', 'Launcher.py\n', 'Preview\n', 'PyDemos-pil-note.txt\n',…..

>>> os.listdir(‘.’) ['.DS_Store', '__init__.py', '__init__.pyc', '__pycache__', '_pymailerrlog.txt', 'Ai', 'Dbase', 'Dstruct', 'echo.py', 'Gui', 'Integrate', 'Internet', 'Lang', 'Launch_PyDemos.pyw', 'Launch_PyGadgets_bar.pyw', 'LaunchBrowser.pyw', 'Launcher.py', 'launchmodes.py', 'Preview', 'PyDemos-pil-note.txt', 'PyDemos.pyw', 'PyGadgets.py', 'PyGadgets_bar.pyw', 'README-PP4E.txt', 'System', 'TempParts', 'textConfig.py', 'Tools'] >>> import glob >>> glob.glob('*') ['__init__.py', '__init__.pyc', '__pycache__', '_pymailerrlog.txt', 'Ai', 'Dbase', 'Dstruct', 'echo.py', 'Gui', 'Integrate', 'Internet', 'Lang', 'Launch_PyDemos.pyw', 'Launch_PyGadgets_bar.pyw', 'LaunchBrowser.pyw', 'Launcher.py', 'launchmodes.py', 'Preview', 'PyDemos-pil-note.txt', 'PyDemos.pyw', 'PyGadgets.py', 'PyGadgets_bar.pyw', 'README-PP4E.txt', 'System', 'TempParts', 'textConfig.py', 'Tools']

glob Glob uses usual filename syntax of many shells for matching: ? –single char, * -any pattern, []- set of chars >>> for path in glob.glob(‘*.py'):... print(path) __init__.py echo.py Launcher.py launchmodes.py PyGadgets.py textConfig.py >>> for path in glob.glob('./*/*.py'):... print(path)./Ai/__init__.py./Dbase/__init__.py./Dbase/castdump.py./Dbase/castinit.py./Dbase/filepickle.py./Dbase/person-start.py./Dbase/person.py./Dbase/testdata.py./Dstruct/__init__.py./Gui/__init__.py./Integrate/__init__.py

Walking Directory Trees Application :Find all files with a global variable name os.walk yields a sequence of 3-tuples – Current directory – All subdirectories – All files within current directory – Recursively walks subdirectories import os for (dirname,subdirs,files) in os.walk ('/Users/fredannexstein/Google Drive/PP4E'): print ('[' + dirname +']') for f in files: print(os.path.join(dirname,f))

Exercise Using the str.endswith() function, write a python script that counts and prints all python files (ending with.py) in PP4E that reference name ‘tkinter’. You may run into the error: UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128) We will see later how to process such files, for now you will pass over errors by adding try-except handler try: if 'tkinter' in open(pname).read(): print(pname) count = count +1 except: pass