Py BoxesChap.5: Modules, Packages, and Programs
Soon-Hyung Yook
September 23, 2018
Soon-Hyung Yook Py Boxes September 23, 2018 1 / 33
Table of Contents I
1 Standalone Programs
2 Command-Line Arguments
3 Modules and Import StatementImport a ModulesImport a Module with Another NameImport Only What You Want from a ModuleModule Search Path
4 Packages
5 The Python Standard LibraryHandle Missing KeysCount ItemsOrder by KeyDequeIterate over Codes StructuresPrint Nicely: pprint()
Soon-Hyung Yook Py Boxes September 23, 2018 2 / 33
Standalone Programs
Standalone Program
Make a file and Run the program
Using a text editor, wirte the following program, and save as test1.py.
print("This standalone program works!")
Then run the program as
$ python3 t e s t 1 . py
Soon-Hyung Yook Py Boxes September 23, 2018 3 / 33
Command-Line Arguments
Command-Line Arguments
Make another one as:
import sys
print(’Program arguments:’, sys.argv)
Now run the program as:
$ python t e s t 2 . pyProgram arguments : [ ’ t e s t 2 . py ’ ]$ python t e s t 2 . py t r a l a l aProgram arguments : [ ’ t e s t 2 . py ’ , ’ t r a ’ , ’ l a ’ , ’ l a ’ ]
Soon-Hyung Yook Py Boxes September 23, 2018 4 / 33
Modules and Import Statement
Modules and Import Statement
Module:
- A module is just a file of Python code.
Hierarchy
data type → statement → function → module
Refer to code of other modules by using import statment.
Soon-Hyung Yook Py Boxes September 23, 2018 5 / 33
Modules and Import Statement Import a Modules
Import a Module
import
import module_name
Example:
import math
y=math.sin(math.pi)
print("y=",y)
Soon-Hyung Yook Py Boxes September 23, 2018 6 / 33
Modules and Import Statement Import a Modules
Make Your Own Module
Make a file report.py
def get_description (): # see the docstring below?
""" Return random weather , just like the pros """
from random import choice # <-- available only inside
the function get_description ()
possibilities = [’rain’, ’snow’, ’sleet ’, ’fog’, ’sun’, ’
who knows’]
return choice(possibilities)
In the same directory save the main programe weatherman.py
import report
description = report.get_description ()
print("Today’s weather:", description)
Run the program:
$ python weatherman . pyToday ’ s weather : sun
Soon-Hyung Yook Py Boxes September 23, 2018 7 / 33
Modules and Import Statement Import a Module with Another Name
Import a Module with Another Name
Alias: import...as...
import report as wr
description = wr.get_description ()
print("Today’s weather:", description)
Soon-Hyung Yook Py Boxes September 23, 2018 8 / 33
Modules and Import Statement Import Only What You Want from a Module
Import Only What You Want from a Module I
from...import...
from module_name import function_names
from math import cos , pi
y=cos(pi)
print("y=",y)
Another Example:
from report import get_description
description = get_description ()
print("Today’s weather:", description)
Soon-Hyung Yook Py Boxes September 23, 2018 9 / 33
Modules and Import Statement Import Only What You Want from a Module
Import Only What You Want from a Module II
Alisas: from...import...as...
from report import get_description as do_it
description = do_it()
print("Today’s weather:", description)
Soon-Hyung Yook Py Boxes September 23, 2018 10 / 33
Modules and Import Statement Module Search Path
Module Search Path
Module Search Path
Paths to look for files to import.
>>> import sys
>>> for place in sys.path:
... print(place)
...
# <--empty line here: the current directory.
/usr/lib/python36.zip
/usr/lib64/python3 .6
/usr/lib64/python3 .6/lib -dynload
/usr/lib64/python3 .6/site -packages
/usr/lib64/python3 .6/site -packages/PIL
/usr/lib64/python3 .6/ _import_failed
/usr/lib/python3 .6/site -packages
Use the first match condition.
Soon-Hyung Yook Py Boxes September 23, 2018 11 / 33
Packages
Packages I
Package is a collection of modules.
Package is a directory.
a file name init .py is needed by Python.init .py can be empty.
Python needs it to treat the directory containing it as a package.
./sources/daily.py
def forecast ():
’fake daily forecast ’
return ’like yesterday ’
./sources/weekly.py
def forecast ():
""" Fake weekly forecast """
return [’snow’, ’more snow’, ’sleet’,’freezing rain’, ’rain’, ’
fog’, ’hail’]
./weather.py
Soon-Hyung Yook Py Boxes September 23, 2018 12 / 33
Packages
Packages II
from sources import daily , weekly
print("Daily forecast:", daily.forecast ())
print("Weekly forecast:")
for number , outlook in enumerate(weekly.forecast (), 1):
print(number , outlook)
$ python weather . py
Soon-Hyung Yook Py Boxes September 23, 2018 13 / 33
The Python Standard Library
The Python Standard Library
One of Python’s prominent claims is that it has “batteries included”.
A large standard library of modules are included.
When you’re about to write some Python code, it’s often worthwhile to firstcheck whether there’s a standard module that already does what you want
Soon-Hyung Yook Py Boxes September 23, 2018 14 / 33
The Python Standard Library Handle Missing Keys
Handle Missing Keys I
get()
>>> periodic_table = {’Hydrogen ’: 1, ’Helium ’: 2}
>>> periodic_table.get(’Hydrogen ’)
1
>>> periodic_table.get(’Carbon ’) # <-- Nothing happens! But no
error!
setdefault()
If the key is in the dictionary:
Do the similar thing with get()
If the key is missing:
Assigns an item to the dictionary
If we try to assign a different default value to an existing key:
The original value is returned and nothing is changed.
Soon-Hyung Yook Py Boxes September 23, 2018 15 / 33
The Python Standard Library Handle Missing Keys
Handle Missing Keys II
>>> periodic_table = {’Hydrogen ’: 1, ’Helium ’: 2}
>>> print(periodic_table)
{’Helium ’: 2, ’Hydrogen ’: 1}
>>> carbon = periodic_table.setdefault(’Carbon ’, 12)
>>> carbon
12
>>> periodic_table
{’Helium ’: 2, ’Carbon ’: 12, ’Hydrogen ’: 1}
>>> helium = periodic_table.setdefault(’Helium ’, 947)
>>> helium
2
>>> periodic_table
{’Helium ’: 2, ’Carbon ’: 12, ’Hydrogen ’: 1}
defaultdict()
specifies the default value for any new key up front, when the dictionary iscreated.
argument is a function
Soon-Hyung Yook Py Boxes September 23, 2018 16 / 33
The Python Standard Library Handle Missing Keys
Handle Missing Keys III
>>> from collections import defaultdict
>>> periodic_table = defaultdict(int) # int returns 0
>>> periodic_table[’Hydrogen ’] = 1
>>> periodic_table[’Lead’]
0
>>> periodic_table
defaultdict(<class ’int’>, {’Lead’: 0, ’Hydrogen ’: 1})
Example 2:
>>> from collections import defaultdict
>>>
>>> def no_idea ():
... return ’Huh?’
...
>>> bestiary = defaultdict(no_idea)
>>> bestiary[’A’] = ’Abominable Snowman ’
>>> bestiary[’B’] = ’Basilisk ’
>>> bestiary[’A’]
’Abominable Snowman ’
>>> bestiary[’B’]
’Basilisk ’
Soon-Hyung Yook Py Boxes September 23, 2018 17 / 33
The Python Standard Library Handle Missing Keys
Handle Missing Keys IV
>>> bestiary[’C’]
’Huh?’
Combin with lambda
>>> bestiary = defaultdict(lambda: ’Huh?’)
>>> bestiary[’E’]
’Huh?’
Use int to make your own counter:
>>> from collections import defaultdict
>>> food_counter = defaultdict(int)
>>> for food in [’spam’, ’spam’, ’eggs’, ’spam’]:
... food_counter[food] += 1
...
>>> for food , count in food_counter.items():
... print(food , count)
For generic dictionary:
Soon-Hyung Yook Py Boxes September 23, 2018 18 / 33
The Python Standard Library Handle Missing Keys
Handle Missing Keys V
>>> dict_counter = {}
>>> for food in [’spam’, ’spam’, ’eggs’, ’spam’]:
... if not food in dict_counter: # if the key is not in the
... dict_counter[food] = 0 # dictionary , initialize it.
... dict_counter[food] += 1
...
>>> for food , count in dict_counter.items():
... print(food , count)
Soon-Hyung Yook Py Boxes September 23, 2018 19 / 33
The Python Standard Library Count Items
Count Items I
Functions in built-in libraries.
Counter()
Return type: class collections.Counter
>>> from collections import Counter
>>> breakfast = [’spam’, ’spam’, ’eggs’, ’spam’]
>>> breakfast_counter = Counter(breakfast)
>>> breakfast_counter
Counter ({’spam’: 3, ’eggs’: 1})
Soon-Hyung Yook Py Boxes September 23, 2018 20 / 33
The Python Standard Library Count Items
Count Items II
most common()
Returns all elements in descending order
Arguments: number of elements which are returned.
>>> breakfast_counter.most_common ()
[(’spam’, 3), (’eggs’, 1)]
>>> breakfast_counter.most_common (1)
[(’spam’, 3)]
Soon-Hyung Yook Py Boxes September 23, 2018 21 / 33
The Python Standard Library Count Items
Operation with Counter Class I
Combine counters: +
>>> breakfast_counter
>>> Counter ({’spam’: 3, ’eggs’: 1})
>>> lunch = [’eggs’, ’eggs’, ’bacon ’]
>>> lunch_counter = Counter(lunch)
>>> lunch_counter
Counter ({’eggs’: 2, ’bacon’: 1})
>>> breakfast_counter + lunch_counter
Counter ({’spam’: 3, ’eggs’: 3, ’bacon’: 1})
Subtract counters: -
>>> breakfast_counter - lunch_counter
Counter ({’spam’: 3})
>>> lunch_counter - breakfast_counter
Counter ({’bacon’: 1, ’eggs’: 1})
Soon-Hyung Yook Py Boxes September 23, 2018 22 / 33
The Python Standard Library Count Items
Operation with Counter Class II
Intersection and union : & and |
>>> breakfast_counter & lunch_counter
Counter ({’eggs’: 1})
>>> breakfast_counter | lunch_counter
Counter ({’spam’: 3, ’eggs’: 2, ’bacon’: 1})
Soon-Hyung Yook Py Boxes September 23, 2018 23 / 33
The Python Standard Library Order by Key
Order by Key
OrderDict()
>>> from collections import OrderedDict
>>> quotes = OrderedDict ([
... (’Moe’, ’A wise guy , huh?’),
... (’Larry ’, ’Ow!’),
... (’Curly ’, ’Nyuk nyuk!’),
... ])
>>>
>>> for stooge in quotes:
... print(stooge)
Soon-Hyung Yook Py Boxes September 23, 2018 24 / 33
The Python Standard Library Deque
Stack+Queue=Deque I
Stack: FIFO
Queue: FILO
Deque: Double ended-queue
Deque has features of both a stack and a queue.
popleft()
Stack feature:
Removes the leftmost item from the deque and returns it.
pop()
Queue feature:
Removes the rightmost item from the deque and returns it.
Soon-Hyung Yook Py Boxes September 23, 2018 25 / 33
The Python Standard Library Deque
Stack+Queue=Deque II
Example: palindrome
>>> def palindrome(word):
... from collections import deque
... dq = deque(word)
... while len(dq) > 1:
... if dq.popleft () != dq.pop():
... return False
... return True
...
...
>>> palindrome(’a’)
True
>>> palindrome(’racecar ’)
True
>>> palindrome(’’)
True
>>> palindrome(’radar ’)
True
>>> palindrome(’halibut ’)
False
Soon-Hyung Yook Py Boxes September 23, 2018 26 / 33
The Python Standard Library Deque
Palindrome: Another way
Using slice:
>>> def another_palindrome(word):
... return word == word [:: -1]
...
>>> another_palindrome(’radar’)
True
>>> another_palindrome(’halibut ’)
False
Soon-Hyung Yook Py Boxes September 23, 2018 27 / 33
The Python Standard Library Iterate over Codes Structures
Iterate over Codes Structures I
itertools module
It contains special-purpose iterator functions.
The itertools module has many more functions for combinations andpermutations that can be time savers when the need arises.
>>> import itertools
Soon-Hyung Yook Py Boxes September 23, 2018 28 / 33
The Python Standard Library Iterate over Codes Structures
Iterate over Codes Structures II
chain()
runs through its arguments as though they were a single iterable:
>>> import itertools
>>> for item in itertools.chain ([1, 2], [’a’, ’b’]):
... print(item)
...
1
2
a
b
Soon-Hyung Yook Py Boxes September 23, 2018 29 / 33
The Python Standard Library Iterate over Codes Structures
Iterate over Codes Structures III
cycle()
an infinite iterator, cycling through its arguments:
>>> import itertools
>>> for item in itertools.cycle ([1, 2]):
... print(item)
...
1
2
1
2
.
.
.
... and so on
Soon-Hyung Yook Py Boxes September 23, 2018 30 / 33
The Python Standard Library Iterate over Codes Structures
Iterate over Codes Structures IV
accumulate()
calculates accumulated values
By default, it calculates the sum
>>> import itertools
>>> for item in itertools.accumulate ([1, 2, 3, 4]):
...
print(item)
...
1
3
6
10
Soon-Hyung Yook Py Boxes September 23, 2018 31 / 33
The Python Standard Library Iterate over Codes Structures
Iterate over Codes Structures V
Multiplication with accumulate()
>>> import itertools
>>> def multiply(a, b):
... return a * b
...
>>> for item in itertools.accumulate ([1, 2, 3, 4], multiply):
... print(item)
...
1
2
6
24
Soon-Hyung Yook Py Boxes September 23, 2018 32 / 33
The Python Standard Library Print Nicely: pprint()
pprint()
Provide improved output to incrase readability:
>>> from pprint import pprint
>>> quotes = OrderedDict ([
... (’Moe’, ’A wise guy , huh?’),
... (’Larry ’, ’Ow!’),
... (’Curly ’, ’Nyuk nyuk!’),
... ])
>>>
>>> print(quotes)
OrderedDict ([(’Moe’, ’A wise guy , huh?’), (’Larry’, ’Ow!’), (’Curly
’, ’Nyuk nyuk!’)])
>>> pprint(quotes)
{’Moe’: ’A wise guy , huh?’,
’Larry’: ’Ow!’,
’Curly’: ’Nyuk nyuk!’}
Soon-Hyung Yook Py Boxes September 23, 2018 33 / 33