Download presentation
Presentation is loading. Please wait.
1
Modules and Packages
2
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
3
Package basics
4
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.
5
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()
6
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.
7
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
8
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()
9
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.
10
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.
11
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).
12
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.
13
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
14
Python Standard Library
15
Useful standard packages
16
Useful libraries (most give useful recipies)
difflib – for comparing text documents; can for example generate a webpages detailing the differences. Unicodedata – for dealing with complex character sets. See also “Fluent Python” regex
17
math decimal — Does for floating points what ints do; makes them exact fractions — Rational numbers (For dealing with numbers as fractions .
18
Serial ports https://docs. python. org/3/faq/library
Serial ports rs232-port argparse — Parser for command-line options, arguments and sub- commands datetime
19
Binary See especially struct: bisect — Array bisection algorithm (efficient large sorted arrays for finding stuff)
20
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})>
21
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)
22
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.