By Ryan Smith The Standard Library In Python. Python’s “Batteries Included” Philosophy Python’s standard library was designed to be able to handle as.

Slides:



Advertisements
Similar presentations
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
Advertisements

Intro to Python Welcome to the Wonderful world of GIS programing!
10/1/2014BCHB Edwards Python Modules and Basic File Parsing BCHB Lecture 10.
Writing Tcl Scripts (cont.) Outline –Variable Scoping –Strings –Eval –File/Channel I/O –Processes –System Info –Errors –Reflection/Debugging –Libraries.
Guide To UNIX Using Linux Third Edition
Unix Shell Scripts. What are scripts ? Text files in certain format that are run by another program Examples: –Perl –Javascript –Shell scripts (we learn.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Python Mini-Course University of Oklahoma Department of Psychology Day 1 – Lesson 2 Fundamentals of Programming Languages 4/5/09 Python Mini-Course: Day.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 17 Reading and Writing Files 5/10/09 Python Mini-Course: Lesson 17 1.
Selecting and Combining Tools F. Duveau 02/03/12 F. Duveau 02/03/12 Chapter 14.
Pattern matching with regular expressions A common file processing requirement is to match strings within the file to a standard form, e.g. address.
By Zeng Sheng Liu. os - provides dozens of functions for interacting with the operating system >>> import os >>> os.system('time 0:02') 0 >>> os.getcwd()
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
Guide To UNIX Using Linux Fourth Edition
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
By: Joshua O’Donoghue. Operating System Interface In order to interact with the operating system in python you will want to become familiar with the OS.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of.
K. Harrison CERN, 20th April 2004 AJDL interface and LCG submission - Overview of AJDL - Using AJDL from Python - LCG submission.
Python’s Standard Library - Part I Josh Lawrence.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
Introduction to Python September 26, /10/ Bioinformatics Languages Low-level, compiled languages: C, C++, Java… Pros: performance Cons:
Linux+ Guide to Linux Certification, Third Edition
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
LINUX programming 1. INDEX UNIT-III PPT SLIDES Srl. No. Module as per Session planner Lecture No. PPT Slide No. 1.Problem solving approaches in Unix,Using.
Intro Python: Variables, Indexing, Numbers, Strings.
Overview Intro to functions What are functions? Why use functions? Defining functions Calling functions Documenting functions Top-down design Variable.
Manipulating Files Refresher. The touch Command touch is used to create a new, empty file. If the file already exists, touch updates the time and date.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
16. Python Files I/O Printing to the Screen: The simplest way to produce output is using the print statement where you can pass zero or more expressions,
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
File Systems cs550 Operating Systems David Monismith.
Python’s Standard Library Part I Joe Houpert CS265.
Python – May 16 Recap lab Simple string tokenizing Random numbers Tomorrow: –multidimensional array (list of list) –Exceptions.
Lecture 4 Python Basics Part 3.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 2 in MATLAB Topics Covered: 1.Functions in Script Files Inline Functions.
Productive Laziness with Python Programmability in SPSS Albert-Jan Roskam, PhD. Statistics Netherlands (CBS) ASSESS – York UK - October.
By: Aradhya Malhotra.  To interact with the OS in python you will want to become familiar with the OS module  The command “import os” is used for this.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Hank Childs, University of Oregon April 15 th, 2016 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / /
Introduction to Python for System Administrators Toshio Kuratomi May 2011.
CIRC Summer School 2016 Baowei Liu
Lesson 06: Functions Class Participation: Class Chat:
Input from STDIN STDIN, standard input, comes from the keyboard.
CIRC Summer School 2017 Baowei Liu
CSC 458– Predictive Analytics I, Fall 2017, Intro. To Python
CIRC Winter Boot Camp 2017 Baowei Liu
Python Modules and Basic File Parsing
IST256 : Applications Programming for Information Systems
Python Modules and Basic File Parsing
CSC1018F: Functional Programming
Writing Shell Scripts ─ part 3
Engineering Innovation Center
Hank Childs, University of Oregon
Python I/O.
CptS 561 Computer Architecture
CSC 458– Predictive Analytics I, Fall 2018, Intro. To Python
Chapter Four UNIX File Processing.
Python’s Standard library part I
Last Class We Covered What makes “good code” good Top down design
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Lab 4: Introduction to Scripting
Python Modules and Basic File Parsing
Python Modules.
CST8177 Scripting 2: What?.
Executing Host Commands
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Using Modules.
Presentation transcript:

By Ryan Smith The Standard Library In Python

Python’s “Batteries Included” Philosophy Python’s standard library was designed to be able to handle as many situations as possible. Has a module for just about anything Can do , access the internet, work with csv and xml packages, and more. Chances are if you want to do it, Python has an easier way to get it done.

The OS Module Python contains a number of functions that can interact with the operating system The os module allows us to access them Some examples of os commands: os.getcwd() – Returns the current working directory os.chdir(‘/cs265/Lab1’) – Change current directory os.system(‘mkdir Lab2’) – Runs from the command line Must use import os to gain access to the commands

Wildcards The glob module allows us to make file lists from wildcard searches To use the command, we must use import glob Example code: import glob glob.glob(‘*.py’) Returns: [‘hello.py’, ‘world.py’, ‘duh.py’]

Getting Command Line Arguments Accessing command line arguments can be done using the sys module. The argv attribute allows us to manipulate the arguments. For example, take the file tootsie.py: import sys print(sys.argv) Running python tootsie.py one two three from the command line would produce: [‘tootsie.py’, ‘one’, ‘two’, ‘three’] We can do more sophisticated processing with argparse

Error Output & Redirection The sys module contains the stderr attribute. We can use it to give warnings and error messages when stdout will not work. For example: sys.stderr.write(‘Dat not gonna work’) To terminate a script, we can use sys.exit()

Regular Expressions In Python, the re module allows us to use regular expressions For example, the findall command: import re re.findall(r’\bf[a-z]*’, ‘fee fi fo fum, you smell’) This would return: [‘fee’, ‘fi’, ‘fo’, ‘fum’] Command found all words that started with f Can also use string methods: ‘tea for too’.replace(‘too’, ‘two’) Returns ‘tea for two’

The math Module Python’s math module allows us to access a number of complicated operations math.cos(), math.log() Also, the random module allows us to create random numbers. random.choice() – Chooses 1 value from an inputted list random.sample(range(100), 10) – Picks a set of numbers from a specified range random.random() – Creates a random float random.randrange(6) – Chooses a random number from the inputted range

Accessing the Internet We can retrieve data and send s using python. This requires the urllib.request module and the smtplib module respectively

Dates and Times The datetime module allows the manipulation of dates and times. For example: from datetime import date now = date.today() now This would return the current date We can also format now with strftime now.strftime(“%m-%d-%y) Returns

Data Compression We can compress data in python using a multitude of modules: zlib, gzip, bz2, lzma, zipfile, and tarfile For instance: import zlib s = b’witch which has which witches wrist watch’ len(s) 41 t = zlib.compress(s) len(t) 37

Performance Measurement The timeit module allows us to track the performance of a program For instance: from timeit import Timer Timer(‘t=a; a=b; b=t’, ‘a=1; b=2’).timeit() Returns

Quality Control The doctest module allows us to test other modules For instance, doctest.testmod() allows us to automatically validate any embedded tests. Using the unittest module allows us to create a set of tests that we can keep in a separate file.

Sources Python Course resources #2, Part 10: