How unit 5 is examined
This unit covers Python modules and packages and the standard library: file I/O, sys, logging, re, datetime, sockets and threads. No topic was asked in the supplied papers, so learn the definitions and the one-line examples.
Modules and Packages
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>A module is a single .py file of Python definitions that can be reused with import, and a package is a directory of modules that contains an init.py file.</mark>
Key points.
-
import mathloads a whole module, whilefrom math import sqrtbrings in only the named item. -
import numpy as npgives the module a short alias. -
A package folder holds modules and an
__init__.py, which runs when the package is imported. -
Modules are searched in the directories listed in
sys.path. -
dir(module)lists the names a module defines, and__name__ == "__main__"is true only when the file is run directly. -
Nested packages are imported with dots, as in
import pkg.sub.mod.
shop/
__init__.py
cart.py
pay.py
from shop import cart # uses shop/cart.py
Standard Libraries: File I/O
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>File handling uses the built-in open(name, mode) function, which returns a file object for reading or writing.</mark>
Key points.
- Modes are
rread,wwrite (erases old content),aappend, andbbinary. read(),readline()andreadlines()read the whole file, one line and all lines as a list.write()puts a string into the file, andclose()releases it.with open(...) as f:closes the file automatically, even after an error.
with open("a.txt", "w") as f:
f.write("hi")
- Reading line by line with
for line in f:uses little memory even for a large file. - The
osmodule complements file handling withos.remove(),os.rename()andos.path.exists().
| Mode | Meaning | If the file is missing |
|---|---|---|
r |
read only, the default | error |
w |
write, erases old content | created |
a |
append at the end | created |
r+ |
read and write | error |
Sys
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>The sys module gives access to interpreter variables and functions.</mark>
Key points.
-
sys.argvis the list of command-line arguments, andargv[0]is the script name. -
sys.exit()stops the program. -
sys.pathis the module search path, andsys.versiongives the Python version. -
sys.stdin,sys.stdoutandsys.stderrare the standard streams. -
sys.platformnames the operating system, andsys.moduleslists the modules already loaded.
import sys
print(sys.argv) # run as: python a.py 5 7 -> ['a.py', '5', '7']
print(int(sys.argv[1]) + int(sys.argv[2])) # 12
Arguments arrive as strings, so convert them with int() before doing arithmetic.
logging
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>The logging module records messages about a running program with a severity level.</mark>
Key points.
- The levels in increasing order are DEBUG, INFO, WARNING, ERROR and CRITICAL.
- The default level is WARNING, so lower messages are hidden until it is changed.
logging.basicConfig(level=..., filename=...)sets the level and the output file.- Logging is better than print because levels can be switched off and output can go to a file.
import logging
logging.basicConfig(level=logging.INFO)
logging.info("started") # INFO:root:started
- A logger can send output to several places through handlers such as
StreamHandler(console) andFileHandler(file). - A formatter such as
"%(asctime)s %(levelname)s %(message)s"controls the layout of each line.
| Level | Value | Use |
|---|---|---|
| DEBUG | 10 | detailed diagnosis |
| INFO | 20 | normal progress |
| WARNING | 30 | something unexpected |
| ERROR | 40 | an operation failed |
| CRITICAL | 50 | the program may stop |
Regular expression
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>A regular expression is a pattern used to search, match or replace text, and Python provides it through the re module.</mark>
Key points.
re.match()checks only at the start of the string, whilere.search()looks anywhere.re.findall()returns all matches as a list, andre.sub()replaces matches.\dis a digit,\wa word character,.any character, and^and$anchor the start and end.*,+and?mean zero or more, one or more, and zero or one.
import re
re.findall(r"\d+", "a12b345") # ['12', '345']
re.compile()builds a pattern once so it can be reused, and[abc]matches any one of the listed characters.- Parentheses make a group, and
m.group(1)returns the text that the first group matched.
import re
m = re.match(r"(\w+)@(\w+)\.com", "[email protected]")
print(m.group(1), m.group(2)) # ab x
print(re.sub(r"\s+", " ", "a b c")) # a b c
| Symbol | Meaning |
|---|---|
\d |
any digit |
\w |
letter, digit or underscore |
\s |
whitespace |
{n} |
exactly n repeats |
[^a] |
any character except a |
Date and Time
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>The datetime module supplies the classes date, time, datetime and timedelta for working with dates and times.</mark>
Key points.
-
datetime.now()returns the current date and time. -
strftime()formats a datetime as text, for example"%d-%m-%Y". -
strptime()parses text into a datetime. -
Subtracting two dates gives a
timedelta, such as 29 days between 1 Feb and 1 Mar 2024. -
date.today()gives only the date, andtimedelta(days=7)can be added to a date to move it forward. -
The module
timeofferstime.time()for seconds since 1970 andtime.sleep(n)to pause n seconds.
from datetime import datetime, date
print(datetime(2024, 1, 15, 10, 30).strftime("%d-%m-%Y %H:%M")) # 15-01-2024 10:30
print(date(2024, 3, 1) - date(2024, 2, 1)) # 29 days, 0:00:00
| Code | Meaning |
|---|---|
%Y |
four-digit year |
%m |
month number |
%d |
day of month |
%H |
hour, 24-hour clock |
%M |
minute |
Network programming
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>Network programming lets programs on different machines communicate, and Python does it with the socket module.</mark>
Key points.
-
A socket is one end of a two-way link, identified by an IP address and a port.
-
socket.socket(AF_INET, SOCK_STREAM)makes a TCP socket, andSOCK_DGRAMmakes a UDP one. -
A server calls
bind(),listen()andaccept(). -
A client calls
connect(), and both sides usesend(),recv()andclose(). -
TCP is connection-oriented and reliable, while UDP is connectionless and faster but may lose data.
-
Data is sent as bytes, so a string must be encoded, as in
s.send("hi".encode()).
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("localhost", 9000))
s.listen(1)
c, addr = s.accept()
c.send(b"hello")
The client side is c = socket.socket(); c.connect(("localhost", 9000)); print(c.recv(1024)).
multi-processing and multi-threading
<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>
Definition. <mark>Multithreading runs several threads inside one process, while multiprocessing runs several separate processes.</mark>
Key points.
-
The
threadingmodule creates threads withThread(target=f), started bystart()and waited for withjoin(). -
Threads share memory, so they are light but need a
Lockto avoid clashes. -
Because of the GIL, threads do not run Python code in parallel, so they suit I/O-bound tasks.
-
The
multiprocessingmodule uses separate processes on several CPU cores, which suits CPU-bound tasks. -
Process(target=f)frommultiprocessingis started and joined just like a thread. -
Processes do not share memory, so they exchange data through a
QueueorPipe.
from threading import Thread
def hi(n): print("thread", n)
ts = [Thread(target=hi, args=(i,)) for i in range(2)]
for t in ts: t.start()
for t in ts: t.join()
| Point | Thread | Process |
|---|---|---|
| Memory | shared | separate |
| Start-up cost | low | high |
| Parallel Python code | no, because of the GIL | yes |
| Best for | I/O-bound work | CPU-bound work |
Last-minute revision
- A module is a .py file, and a package is a folder with init.py.
open()modes are r, w, a and b, andwithcloses the file automatically.sys.argvholds the command-line arguments.- Logging levels run DEBUG, INFO, WARNING, ERROR, CRITICAL, and the default is WARNING.
re.matchchecks the start only, andre.searchscans the whole string.strftimeformats a date, andstrptimeparses one.- A server socket calls bind, listen and accept, and a client calls connect.
- Threads share memory and are limited by the GIL, and processes are independent.
Memory hooks
- Server sockets go B-L-A: bind, listen, accept.
strFtimeFormats,strPtimeParses.- Log levels: "Dogs Invite Wolves, Eat Carrots".
- Threads share, processes separate.
Coverage checklist
- Modules and Packages: no past questions.
- Standard Libraries: File I/0: no past questions.
- Sys: no past questions.
- logging: no past questions.
- Regular expression: no past questions.
- Date and Time: no past questions.
- Network programming: no past questions.
- multi-processing and multi-threading: no past questions.