Modules. Modules Modules are the highest level program organization unit, usually correspond to source files and serve as libraries of tools. Each file.

Slides:



Advertisements
Similar presentations
Introduction to C++ An object-oriented language Unit - 01.
Advertisements

Cognos Web Services Business Intelligence. SOA SOA (Service Oriented Architecture) The SOA approach involves seven key principles: -- Coarse -grained.
C Language.
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.
Perkovic, Chapter 7 Functions revisited Python Namespaces
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Exercise Session 1: Eiffel Introduction.
An Introduction to Python – Part IV Dr. Nancy Warter-Perez May 19, 2005.
An Introduction to Python – Part IV Dr. Nancy Warter-Perez June 23, 2005.
Geography 465 Modules and Functions Writing Custom Functions.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
An Introduction to Python – Part IV Dr. Nancy Warter-Perez.
Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
Chapter 2: Java Fundamentals Java Program Structure.
1 Outline 7.1 Introduction 7.2 Implementing a Time Abstract Data Type with a Class 7.3 Special Attributes 7.4Controlling Access to Attributes 7.4.1Get.
Web Applications Basics. Introduction to Web Web features Clent/Server HTTP HyperText Markup Language URL addresses Web server - a computer program that.
PYTHON: LESSON 1 Catherine and Annie. WHAT IS PYTHON ANYWAY?  Python is a programming language.  But what’s a programming language?  It’s a language.
Using Ant to build J2EE Applications Kumar
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
Guide to Programming with Python Chapter Nine Working with/Creating Modules.
Python programs How can I run a program? Input and output.
Java 程序设计 Java Programming Fall, Contents for Today Java Program Structure  How to Compile a Java Program  How to Run a Java Program Environment.
11 Getting Started with C# Chapter Objectives You will be able to: 1. Say in general terms how C# differs from C. 2. Create, compile, and run a.
Understanding the CORBA Model. What is CORBA?  The Common Object Request Broker Architecture (CORBA) allows distributed applications to interoperate.
EJB Framework.  As we know, EJB is the center of the J2EE architecture that provides a sturdy framework for building enterprise applications. The major.
Introduction to Python 2 Dr. Bernard Chen University of Central Arkansas PyArkansas 2011.
Internet of Things with Intel Edison Compiling and running Pierre Collet Intel Software.
Python Modules An Introduction. Introduction A module is a file containing Python definitions and statements. The file name is the module name with the.
Java Spring PImage Let’s look at the PImage class in ProcessingPImage –What are the fields (i.e., variables)? –What methods are available? –What.
Python – Part 1 Python Programming Language 1. What is Python? High-level language Interpreted – easy to test and use interactively Object-oriented Open-source.
Ch 1. A Python Q&A Session Spring Why do people use Python? Software quality Developer productivity Program portability Support libraries Component.
Introduction to Eclipse CSC 216 Lecture 3 Ed Gehringer Using (with permission) slides developed by— Dwight Deugo Nesa Matic
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
By James Braunsberg. What are Modules? Modules are files containing Python definitions and statements (ex. name.py) A module’s definitions can be imported.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
4/1/05F-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Packages and Components in Java and UML.
For loops in programming Assumes you have seen assignment statements and print statements.
How to write & use Perl Modules. What is a Module? A separate Namespace in a separate file with related functions/variables.
Overview Intro to functions What are functions? Why use functions? Defining functions Calling functions Documenting functions Top-down design Variable.
Final Review Dr. Bernard Chen University of Central Arkansas Spring 2012.
Ch 1. A Python Q&A Session. Why do people use Python? Software Quality Developer productivity Program portability Support Libraries Component integration.
Objective You will be able to define the basic concepts of object-oriented programming with emphasis on objects and classes by taking notes, seeing examples,
Chapter 15. Modules Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
CS 368 – Intro to Scripting Languages Summer 2009 Cartwright, De Smet, LeRoy Object Oriented Programming Programming Perl Chapter 12: "Objects"
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.
Introduction to UML and Rational Rose UML - Unified Modeling Language Rational Rose 98 - a GUI tool to systematically develop software through the following.
Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder.
An Introduction to Ant. What is Ant? How do you use it? Why would you want to?
Installation and environment variables. Simple Installation 1 The latest sources can always be obtained from the CDAT website:
Python’s Modules Noah Black.
G. Pullaiah College of Engineering and Technology
Modules and Packages Damian Gordon.
Ch 1. A Python Q&A Session Bernard Chen 2007.
Python’s Modules by E. Esin GOKGOZ.
Functions CIS 40 – Introduction to Programming in Python
File service architecture
Java Intro.
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
Lab 4: Introduction to Scripting
Python Modules.
Chapter 2: Java Fundamentals
Classes 5/5 May 14, 2019 ICS102: Classes 5/5.
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,
 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.
Python Modules.
Learning Python 5th Edition
Presentation transcript:

Modules

Modules Modules are the highest level program organization unit, usually correspond to source files and serve as libraries of tools. Each file is a module and modules import other modules to use the name they define. import import: lets a client fetch a module as a whole from from: allows to fetch particular names from module reload reload: provides a way to reload code without stopping Python

Why Use Modules? Modules provide an easy way to organize components into a system, by serving as packages of names Code reuse: code in module is persistent: can be reloaded and run as many times as needed System namespace partitioning: Everything “lives” in a module: code and objects are always implicitly enclosed by a module Implementing shared services or data

Structure a Program General architecture General architecture: Multiple text files with Python statements One top-level file with zero or more modules (imports) Top-level file contains the main control flow import A file imports a module to gain access to the tools it defines

Structure a Program Attributes Attributes are tools defined by a module (functions). in b.py: def spam(t): print t, in c.py: import b object.attribute b.spam(“hi”) # object.attribute

How Imports Work module search path: Find the module using module search path: Home directory of top-level file PYTHONPATH directories Standard directory libraries The contents of any *.pth (path configuration) files. It checks: source file(*.py), byte-code file(*.pyc), a directory for package imports, C extension module, in-memory image (frozen executables), java class, zip file(zipimport module)

How Imports Work Compile it to byte-code (if needed) *.pyc is saved only for imported modules Run its code to build the objects it defines Future imports skip all three steps and reuse already-loaded module

Modules Usage Clients can use the module file we just wrote by running “import” statement >>> import math >>> import random >>> import module1 # assume this is the file name we saved Clients can use the module file by running from statement >>>from fib import fib, fibTopN, list without module The copied names are used directly without going through module

Modules examples We create a file name module1: def print_out(aa): print aa*3 x=1 y=2

Modules Example now we write another file named module2 import module1 module1.print_out(“Hello World!! ”) # Use module1’s function print module1.x, module1.y # Reference module1’s variable x=10 y=20 print x, y The result of execute this program is: Hello World!! Hello World!! Hello World!!

Modules Example You may import as many modules as you like, for example: import module1 import module2 print module1.x print module2.x The result of execute this program is: 1 10