Modules and Packages.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter Modules CSC1310 Fall Modules Modules Modules are the highest level program organization unit, usually correspond to source files and.
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.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Guide to Programming with Python Chapter Nine Working with/Creating Modules.
Task Farming on HPCx David Henty HPCx Applications Support
Functions & Objects IDIA 618 Spring 2012 Bridget M. Blodgett.
Introduction to Python
Agenda Sed Utility - Advanced –Using Script-files / Example Awk Utility - Advanced –Using Script-files –Math calculations / Operators / Functions –Floating.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
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,
By James Braunsberg. What are Modules? Modules are files containing Python definitions and statements (ex. name.py) A module’s definitions can be imported.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Python Functions.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
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.
Python’s Standard Library Part II Dennis Tran. Output Formatting The repr module provides a version of repr() customized for abbreviated displays of large.
Python Programming Module 1 Computers and Programs Python Programming, 2/e1.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CMSC201 Computer Science I for Majors Lecture 20 – Classes and Modules Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from the.
PH2150 Scientific Computing Skills
Python’s Modules Noah Black.
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
Modules and Packages Damian Gordon.
Introduction to Python
Formatting Output.
C++ coding standard suggestion… Separate reasoning from action, in every block. Hi, this talk is to suggest a rule (or guideline) to simplify C++ code.
Shell Scripting March 1st, 2004 Class Meeting 7.
Python’s Modules by E. Esin GOKGOZ.
CSE 374 Programming Concepts & Tools
Variables, Expressions, and IO
Programming for Geographical Information Analysis: Core Skills
Core libraries Scripts, by default only import sys (various system services/functions) and builtins (built-in functions, exceptions and special objects.
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
Style Generally style is built into Python, as it demands indents. However, good Python style is set out in PEP8:
Formatting Output.
Functions CIS 40 – Introduction to Programming in Python
PH2150 Scientific Computing Skills
One-Dimensional Array Introduction Lesson xx
Topics Introduction to File Input and Output
OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.
CMSC201 Computer Science I for Majors Lecture 16 – Classes and Modules
Functions and Procedures
Programming in Python – Lecture#2
Java Programming Arrays
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
ARRAYS 1 GCSE COMPUTER SCIENCE.
Coding Concepts (Data- Types)
Rocky K. C. Chang 15 November 2018 (Based on Dierbach)
Building Java Programs
Intro to PHP.
Programming for Geographical Information Analysis: Core Skills
Stata Basic Course Lab 2.
Topics Introduction to File Input and Output
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Lab 4: Introduction to Scripting
Python Modules.
Topics Introduction to File Input and Output
Control Flow statements
Methods/Functions.
Declarative Languages
Basic shell scripting CS 2204 Class meeting 7
ITM 352 Functions.
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.
Running & Testing Programs :: Translators
Presentation transcript:

Modules and Packages

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, and then use the module name to refer to it. import module1 a = module1.ClassName() We can import specific components to shorten this: from module1 import ClassName a = ClassName

Package basics

Packages The Standard Python Library comes with a number of other packages which are not imported automatically. We'll look at some of these later in the course, and talk about the detailed difference between: modules: usually single files to do some set of jobs packages: modules with a namespace, that is, a unique way of referring to them libraries: a generic name for a collection of code you can use to get specific types of job done. https://docs.python.org/3/tutorial/modules.html

Running module code Although we've concentrated on classes, you can import and run module-level functions, and access variables. import module1 print(module1.module_variable) module1.module_function() a = module1.ClassName()

Modules that run You have to be slightly careful when importing modules. Modules and the classes in them will run on import. # module print ("module loading") # Runs def m1(): print ("method loading") class cls: print ("class loading") # Runs def m2(): print("instance method loading") Modules run incase there's anything that needs setting up (variables etc.) prior to functions or classes.

Modules that run Note also that in general, code accessing a class or method has to be after if is defined: c = A() c.b() class A: def b (__self__) : print ("hello world") Doesn’t work, but Does

Modules that run Yet this works fine because the files has been scanned down to c= A() before it runs, so all the methods are recognised. class A: def __init__ (self): self.b() def b (self) : print ("hello world") c = A()

Modules that run However, generally having large chunks of unnecessary code running is bad. Setting up variables is usually ok, as assignment generally doesn't cause issues. Under the philosophy of encapsulation, however, we don't really want code slooping around ouside of methods/functions. The core encapsulation level for Python are the function and objects (with self; not the class). It is therefore generally worth minimising this code.

When importing, Python will import parent packages (but not other subpackages) If hasn’t been used before, will search import path, which is usually (but not exclusively) the system path.

If you're importing a package, don't have files with the same name (i If you're importing a package, don't have files with the same name (i.e. package_name.py) in the directory you're in, or they'll be imported rather than the package (even if you're inside them).

Interpreter To reload a module: import importlib; importlib.reload(modulename) “import sound.effects.echo This loads the submodule sound.effects.echo. It must be referenced with its full name. sound.effects.echo.echofilter(input, output, delay=0.7, atten=4) An alternative way of importing the submodule is: from sound.effects import echo This also loads the submodule echo, and makes it available without its package prefix, so it can be used as follows: echo.echofilter(input, output, delay=0.7, atten=4) Yet another variation is to import the desired function or variable directly: from sound.effects.echo import echofilter Again, this loads the submodule echo, but this makes its function echofilter() directly available: echofilter(input, output, delay=0.7, atten=4) “ You can write a text file as .py and then import it (if in current directory easiest. You can then: module.function() module.variable Local variables don’t clash if used this way. Note that module variables are used within modules appropriately (eg as global) even if local variables declared in the environment. If “from x import var” then var can be locally overridden, but module.var stays same (though the module will need separately importing – though the module will run). from fibo import * Not advised, as you don’t know which local variables may be overridden.

Running a package We saw that we can separate off code to run from a module if it runs as a script with: if __name__ == "__main__": function_name() The same thing can be done for a package by placing the code in a file called __main__.py This will run if the package is run in this form: python -m packagename

Python Standard Library https://docs.python.org/3/py-modindex.html https://docs.python.org/3/library/index.html https://docs.python.org/3/tutorial/stdlib.html

Useful standard packages

Useful libraries (most give useful recipies) difflib – for comparing text documents; can for example generate a webpages detailing the differences. https://docs.python.org/3/library/difflib.html Unicodedata – for dealing with complex character sets. See also “Fluent Python” https://docs.python.org/3/library/unicodedata.html regex https://docs.python.org/3/library/re.html

math https://docs.python.org/3/library/math.html decimal — Does for floating points what ints do; makes them exact https://docs.python.org/3/library/decimal.html fractions — Rational numbers (For dealing with numbers as fractions https://docs.python.org/3/library/fractions.html .

Serial ports https://docs. python. org/3/faq/library Serial ports https://docs.python.org/3/faq/library.html#how-do-i-access-the-serial- rs232-port argparse — Parser for command-line options, arguments and sub- commands https://docs.python.org/3/library/argparse.html datetime https://docs.python.org/3/library/datetime.html

Binary https://docs.python.org/3/library/binary.html See especially struct: https://docs.python.org/3/library/struct.html bisect — Array bisection algorithm (efficient large sorted arrays for finding stuff) https://docs.python.org/3/library/bisect.html

Collections https://docs.python.org/3/library/collections.html # Tally occurrences of words in a list c = Counter() for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']: c[word] += 1 print(c) <Counter({'blue': 3, 'red': 2, 'green': 1})>

Collections https://docs.python.org/3/library/collections.html # Find the ten most common words in Hamlet import re words = re.findall(r'\w+', open('hamlet.txt').read().lower()) Counter(words).most_common(10) [('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631), ('you', 554), ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451) https://docs.python.org/3/library/collections.html#collections.Counter

Statistics mean() Arithmetic mean (“average”) of data. harmonic_mean() Harmonic mean of data. median() Median (middle value) of data. median_low() Low median of data. median_high() High median of data. median_grouped() Median, or 50th percentile, of grouped data. mode() Mode (most common value) of discrete data. These functions calculate an average or typical value from a population or sample. These functions calculate a measure of how much the population or sample tends to deviate from the typical or average values. pstdev() Population standard deviation of data. pvariance() Population variance of data. stdev() Sample standard deviation of data. variance() Sample variance of data.