<class 'module'>
PHOT 110: Introduction to programming
LECTURE 07: Modules (Ch. 4.9)
Michaël Barbier, Spring semester (2023-2024)
import
keywordExample: math
is a module
math
module contains functions and other objectsnumpy
is a moduleobject.method()
An example object is a list:
['apple', 1, True, 'five']
object.method()
Another example object is a string:
object.method()
Another example object is a string:
Example:
tranformations.py
import numpy as np
def rotate(xys, angle):
""" Rotates points around the origin """
rot_matrix = np.array(
[[np.cos(angle), -np.sin(angle)],
[np.sin(angle), np.cos(angle)]])
return np.dot(rot_matrix, xys)
def translate(xys, displacement):
""" Translates points with a displacement vector """
return xys + displacement
def scale(xys, scale):
""" Scale the point coordinates """
return xys * scale
script.py
But … any code that prints, changes a variable, plots, or outputs something would be executed on import !
Solution: using the __name__
variable
__name__
variable__name__
is always defined__name__
contains:
"__main__"
if run as a script__name__
variable__name__
is always defined__name__
contains:
"__main__"
if run as a script__name__ == "__main__"
tranformations.py
import numpy as np
import matplotlib.pyplot as plt
def scale(xys, scale):
""" scales points by scale """
return xys * scale
# Testing the scale function
shape = np.array([[1, 2, 1, -1, 0, 1], [0, 1, 2, 2, -3, 0]])
scaled_shape = scale(shape, 3)
plt.plot(shape[0,:], shape[1,:])
plt.plot(scaled_shape[0,:], scaled_shape[1,:])
plt.show()
__name__ == "__main__"
__name__ == "__main__"
Not doing anything except printing this !
tranformations.py
import numpy as np
import matplotlib.pyplot as plt
def scale(xys, scale):
""" scales points by scale """
return xys * scale
# Only execute tests if run as script
if __name__ == "__main__":
shape = np.array([[1, 2, 1, -1, 0, 1], [0, 1, 2, 2, -3, 0]])
scaled_shape = scale(shape, 3)
plt.plot(shape[0,:], shape[1,:])
plt.plot(scaled_shape[0,:], scaled_shape[1,:])
plt.show()
transformations.py
""" Geometrical transformations on points in 2D.
Provides functionality for rotation, translation,
or scaling of (x, y)-coordinates. Coordinates
should be given as (2 x N) Numpy arrays where N
is the number of points.
Typical usage example:
points = np.array([[1, 4, 6],[2, 5, 3]])
scaled_points = scale(points, 3)
"""
import numpy as np
# ... code of the module ...
Pyinstaller works for (our) Python version 3.12.
Follow the instructions below to create an executable:
setup.py as advised in the book is the old way, new way:
Write a pyproject.toml file to package a Python project
Easiest way: use Package and Dependency Managers:
pyproject.toml
[build-system]
requires = ["setuptools", "setuptools-scm"]
build-backend = "setuptools.build_meta"
[project]
name = "my_package"
authors = [{name = "Michael Barbier"},]
description = "My package description"
readme = "README.md"
license = {text = "BSD-3-Clause"}
dependencies = ["numpy",]
[project.scripts]
my-script = "my_package.module:function"
Lecture 07: Modules, script documentation, and executables (Ch. 4.9)