Juancho Datu. What is a Module? File containing Python definitions and statements with the suffix ‘.py’ in the current directory For example file name:

Slides:



Advertisements
Similar presentations
Web编程技术 Web Programming Technology
Advertisements

Introduction to C++ An object-oriented language Unit - 01.
CPSC 441 TUTORIAL – JANUARY 16, 2012 TA: MARYAM ELAHI INTRODUCTION TO C.
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.
Lecture Materials for the John Wiley & Sons book: Cyber Security: Managing Networks, Conducting Tests, and Investigating Intrusions April 14, 2015 DRAFT1.
An Introduction to Python and Its Use in Bioinformatics
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Geography 465 Modules and Functions Writing Custom Functions.
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
25-Jun-15Advanced Programming Spring 2002 Python Henning Schulzrinne Department of Computer Science Columbia University (based on tutorial by Guido van.
Guide To UNIX Using Linux Third Edition
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.
Unix Shell Scripts. What are scripts ? Text files in certain format that are run by another program Examples: –Perl –Javascript –Shell scripts (we learn.
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.
Lecture 3  Shell Variables  Shell Command History  Job / Process Control  Directory Control.
1 Introduction to Tool chains. 2 Tool chain for the Sitara Family (but it is true for other ARM based devices as well) A tool chain is a collection of.
Guide to Programming with Python Chapter Nine Working with/Creating Modules.
Sydney Opera House. Week Three Agenda Administrative Issues Link of the week Review week two lab assignment This week’s expected outcomes Next lab assignment.
7-Sep-15 Python structure  modules: Python source files or C extensions  import, top-level via from, reload  statements  control flow  create objects.
CIS 218 Python CIS 218 Oakton Community College. CIS 218 Python features no compiling or linkingrapid development cycle no type declarationssimpler, shorter,
Unix Talk #2 (sed). 2 You have learned…  Regular expressions, grep, & egrep  grep & egrep are tools used to search for text in a file  AWK -- powerful.
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
An Introduction to Unix Shell Scripting
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 8 Shell.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
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,
GNU Compiler Collection (GCC) and GNU C compiler (gcc) tools used to compile programs in Linux.
Writing C-shell scripts #!/bin/csh # Author: Ken Berman # Date: # Purpose: display command and parameters echo $0 echo $argv[*]
Linux Operations and Administration
1 Homework / Exam HW7 is due next class Starting Glass chapter 4 and parts of 7 Exam 3 – Class 26 –Open Book / Open Notes –Up through End of K&R Chapter.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
By James Braunsberg. What are Modules? Modules are files containing Python definitions and statements (ex. name.py) A module’s definitions can be imported.
Overview Intro to functions What are functions? Why use functions? Defining functions Calling functions Documenting functions Top-down design Variable.
Shell (Addendum). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Eclipse 24-Apr-17.
CS2021 Week 2 Off and Running with Python. Two ways to run Python The Python interpreter – You type one expression at a time – The interpreter evaluates.
Executable scripts. So far We have made scripts echo hello #for example And called it hello.sh Run it as sh hello.sh This only works from current directory?
Week Two Agenda Announcements Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Next lab assignments.
Building Packages BCIS 3680 Enterprise Programming.
CS2021 Python Programming Week 3 Systems Programming PP-Part II.
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.
Modules. Modules Modules are the highest level program organization unit, usually correspond to source files and serve as libraries of tools. Each file.
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.
1 Lecture 7 Introduction to Shell Scripts COP 3353 Introduction to UNIX.
PYTHON FOR HIGH PERFORMANCE COMPUTING. OUTLINE  Compiling for performance  Native ways for performance  Generator  Examples.
Matlab Programming for Engineers
Search in Python Chapter 3.
Week 3-4 Control flow (review) Function definition Program Structures
Python’s Modules Noah Black.
G. Pullaiah College of Engineering and Technology
Lecture 2 Python Basics.
Python’s Modules by E. Esin GOKGOZ.
The Linux Operating System
Perl Modules.
INTRODUCTION TO UNIX: The Shell Command Interface
CHAPTER FOUR Functions.
Modules and Packages.
Programming in Python – Lecture#2
CSCI The UNIX System Shell Startup and Variables
Winter 2018 CISC101 12/5/2018 CISC101 Reminders
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
CISC101 Reminders Assn 3 sample solution is posted.
Lab 4: Introduction to Scripting
Python Modules.
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.
SPL – PS1 Introduction to C++.
Python Modules.
Presentation transcript:

Juancho Datu

What is a Module? File containing Python definitions and statements with the suffix ‘.py’ in the current directory For example file name: markov.py Name becomes the global variable __name__ Import into Python interpreter with: import markov Access functions in the module with: markov.mca(alice30.txt)

More about Modules Global variables in module don’t affect the user’s global variables Module variables can be accessed modulename.itemname Modules can import other modules from markov import * Placed at the beginning of importing module * imports all names

Executing as Scripts Can run a Python module with: python markov.py Sets ‘__name__’ to ‘__main__’ if __name__ == "__main__": import sys markov(str(sys.argv[1])) Can be run as a script if above is placed at end of module $ python markov.py alice30.txt

Module Search Path Can be modified in Unix /usr/local/lib/python Can be listed in: Environment variable ‘PYTHONPATH’

‘Compiled’ Python Files.pyc file created when.py is complied ‘byte-compiled’ version of the.py module Can optimize code with –O or –OO flag Creates.pyo files Program doesn’t run faster with.pyc or.pyo The modules are just loaded faster

Standard Modules Comes with library of standard modules ‘sys’ module sys.path can be used to modify PYTHONPATH >>> import sys >>> sys.path.append(‘[Path to be added]')

dir() Function Returns sorted list of all the names defined by the module >>> import sys >>> dir(sys) ['__displayhook__', '__doc__', '__excepthook__', '__name__', '__stderr__', '__stdin__', '__stdout__', '_getframe', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'callstats', 'copyright', 'displayhook', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'getdefaultencoding', 'getdlopenflags', 'getrecursionlimit', 'getrefcount', 'hexversion', 'maxint', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'version', 'version_info', 'warnoptions']

Packages Collection of modules __init__.py starts the list of modules in a package

Packages cont. Import individual modules import sound.effects.echo If ‘__init__.py’ includes __all__ = ["echo", "surround", "reverse"] Then the following statement would import those listed above from sound.effects import * ‘__path__’ attribute is initialized with the list containing name of the directory holding package’s ‘__init__.py’ Can be modified to extend set of modules in a package

Intra-package References Sometimes modules in different subpackages need to reference a module in another subpackage If ‘sound.filters.vocoder’ module needed to use ‘echo’ in the ‘sound.effects’ package: from sound.effects import echo Can also use implicit relative imports from. import echo from..filters import equalizer

Questions?