Modules and Decomposition UW CSE 190p Summer 2012 download examples from the calendar.

Slides:



Advertisements
Similar presentations
"The State of the Python Union" Python10 - Alexandria, VA - February 7, 2002 Guido van Rossum Director, PythonLabs at Zope Corporation
Advertisements

Why I Invented Python EuroPython – June 27, 2005 Guido van Rossum Elemental Security, Inc.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Methods Java 5.1 A quick overview of methods
15. Python - Modules A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand.
Chapter Modules CSC1310 Fall Modules Modules Modules are the highest level program organization unit, usually correspond to source files and.
Setting the PYTHONPATH PYTHONPATH is where Python looks for modules it is told to import List of paths Add new path to the end with: setenv PYTHONPATH.
This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
Perkovic, Chapter 7 Functions revisited Python Namespaces
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Lecture 5.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 13 Fall 2010.
Introduction to Computing Using Python Namespaces and Exceptions, revisited  Encapsulation in Functions  Global versus Local Namespaces  Exceptional.
Programming for Engineers in Python
Tcl and Otcl Tutorial Part I Internet Computing KUT Youn-Hee Han.
Concepts when Python retrieves a variable’s value Namespaces – Namespaces store information about identifiers and the values to which they are bound –
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Outline 4.1 Introduction 4.2 Program Components in Python 4.3 Functions 4.4Module.
 2002 Prentice Hall. All rights reserved. 1 Functions Purpose –Building blocks for the program (separate functionality in independant parts) –Avoid code.
FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language.
1 Python Chapter 2 © Samuel Marateck, After you install the compiler, an icon labeled IDLE (Python GUI) will appear on the screen. If you click.
Jonathan Huelman CSC 415 – Programming Languages
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.
Introduction to Computing Using Python Dictionary container class + modules  Container Class dict  Modules, revisited.
Intro to Python Programming (Resources) Pamela A. Moore Zenia C. Bahorski Eastern Michigan University March 7, 2012 A language to swear by, not at. 1.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
CSC 110 Numeric data types [Reading: chapter 3] CSC 110 D 1.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
Python: Classes By Matt Wufsus. Scopes and Namespaces A namespace is a mapping from names to objects. ◦Examples: the set of built-in names, such as the.
Python Modules An Introduction. Introduction A module is a file containing Python definitions and statements. The file name is the module name with the.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Data Abstraction UW CSE 140 Winter What is a program? – A sequence of instructions to achieve some particular purpose What is a library? – A collection.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
The Java Programming Language
© David Kirk/NVIDIA and Wen-mei W. Hwu Taiwan, June 30-July 2, Taiwan 2008 CUDA Course Programming Massively Parallel Processors: the CUDA experience.
© David Kirk/NVIDIA and Wen-mei W. Hwu, ECE498AL, University of Illinois, Urbana-Champaign 1 Programming Massively Parallel Processors CUDA Threads.
© David Kirk/NVIDIA and Wen-mei W. Hwu Urbana, Illinois, August 10-14, VSCSE Summer School 2009 Many-core processors for Science and Engineering.
1 CSC 221: Introduction to Programming Fall 2012 Python data, assignments & turtles  Scratch programming review  Python & IDLE  numbers & expressions.
1 Math Expressions and Operators. 2 Some C++ Operators Precedence OperatorDescription Higher ( )Function call +Positive - Negative *Multiplication / Division.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
Libraries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
MA/CS 375 Fall 2002 Lecture 3. Example 2 A is a matrix with 3 rows and 2 columns.
Compsci 06/101, Spring Compsci 6/101: PFTW, Feb 28-March 4 l Algorithms and Data Structures  Sets and how they are used in Python (data structure)
© David Kirk/NVIDIA and Wen-mei W. Hwu, ECE498AL, University of Illinois, Urbana-Champaign 1 ECE498AL Lecture 4: CUDA Threads – Part 2.
CSCE 121: Introduction to Program Design and Concepts, Honors J. Michael Moore Spring 2015 Set 14: Plotting Functions and Data 1.
Introduction to Computing Using Python Namespaces – Local and Global  The Purpose of Functions  Global versus Local Namespaces  The Program Stack 
PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.
Mathematics in WebCT III Bill Moss Clemson University Clemson, SC USA.
Parallel Programming Basics  Things we need to consider:  Control  Synchronization  Communication  Parallel programming languages offer different.
MA/CS 375 Fall 2003 Lecture 3. .* Multiplication We can use the.* operator to perform multiplication entry by entry of two matrices:
Python  Monty or Snake?. Monty?  Spam, spam, spam and eggs  Dead parrots  Eric Idle, John Cleese, Michael Palin, etc.
MA/CS 375 Fall 2002 Lecture 2. Motivation for Suffering All This Math and Stuff Try the Actor demo from
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
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 I/O Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Classic file reading 1 infile = open(’filename.txt’, ’r’) for line.
CSE 110: Programming Language I Afroza Sultana UB 1230.
Victoria Manfredi September 27, 2016.
Chapter 6 - Functions modular programming general function format
Python’s Modules Noah Black.
G. Pullaiah College of Engineering and Technology
Matlab Introduction.
CSC/ECE 517 Introduction to Ruby.
Namespaces – Local and Global
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
ECE498AL Spring 2010 Lecture 4: CUDA Threads – Part 2
Terminal-Based Programs
Python Modules.
Namespaces – Local and Global
Python Modules.
Presentation transcript:

Modules and Decomposition UW CSE 190p Summer 2012 download examples from the calendar

1.Beautiful is better than ugly. 2.Explicit is better than implicit. 3.Simple is better than complex. 4.Complex is better than complicated. 5.Flat is better than nested. 6.Sparse is better than dense. 7.Readability counts. 8.Special cases aren't special enough to break the rules. 1.Although practicality beats purity. 9.Errors should never pass silently. 1.Unless explicitly silenced. 10.In the face of ambiguity, refuse the temptation to guess. 11.There should be one-- and preferably only one --obvious way to do it. 1.Although that way may not be obvious at first unless you're Dutch. 12.Now is better than never. 1.Although never is often better than right now. 13.If the implementation is hard to explain, it's a bad idea. 14.If the implementation is easy to explain, it may be a good idea. 15.NameSpaces are one honking great idea -- let's do more of those! by Tim Peters

Namespaces A container for a set of identifiers used to disambiguate duplicate names Example:

Modules in Python import math import sys import networkx as nx Working definition for our purposes: a set of related functions, stored together in a python file module name alias from networkx import Graph, DiGraph g = Graph() Graph and DiGraph are now available in the global namespace Two motivations 1) provides a namespace 2) provides a unit of abstraction

Writing a Module def search(query): “””return a list of tweets associated with the given query”””... Step 1: Write your definitions in a file named “twitter.py” Step 2: Use your new module by writing import twitter That’s it!

The dir() function You can inspect the functions in a module using the dir function. >>> dir(math) ['__doc__', '__file__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc'] This actually works on any object.

The dir() function >>> import tweet >>> dir(tweet) ['__builtins__', '__doc__', '__file__', '__name__', '__package__', '_makeurl', 'json', 'search']

Writing Modules (2) What about all the code not inside a function? – test cases, etc. – It would be nice if we had a way to determine whether we were running as a module or as a script! print __name__

Modules: Public and Private Functions Some of your functions may not be intended for public use. Possible example: The read_scores function in homework 5 In Python, and unlike many languages, there’s no guaranteed way to protect them. There is a convention you can use: def _read_scores(filename): “““return the words and scores contained in filename””” A leading underscore indicates “this function is intended for internal use only,” but has no real effect.

What happens on import 1.(line 1) Python checks to see if twitter is already imported as a module. It isn’t. 2.(line 1) Python looks for a file named twitter.py in the search path. Aside: The search path can be accessed and modified using the sys module 3.(line 1) If the file is found, the code in the file is executed as usual. The variable twitter is assigned a module object. 4.(line 2) Python checks to see if if twitter is already imported as a module. It is, so nothing happens. Python does not re-read the file, even if it has changed! import twitter