Skip to content
CY-306 · Computer Workshop (Python Programming)/Quick Revision Short Notes

Computer Workshop (Python Programming) (CY-306) - Unit 5 Short Notes

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.

  1. import math loads a whole module, while from math import sqrt brings in only the named item.

  2. import numpy as np gives the module a short alias.

  3. A package folder holds modules and an __init__.py, which runs when the package is imported.

  4. Modules are searched in the directories listed in sys.path.

  5. dir(module) lists the names a module defines, and __name__ == "__main__" is true only when the file is run directly.

  6. 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.

  1. Modes are r read, w write (erases old content), a append, and b binary.
  2. read(), readline() and readlines() read the whole file, one line and all lines as a list.
  3. write() puts a string into the file, and close() releases it.
  4. with open(...) as f: closes the file automatically, even after an error.
with open("a.txt", "w") as f:
    f.write("hi")
  1. Reading line by line with for line in f: uses little memory even for a large file.
  2. The os module complements file handling with os.remove(), os.rename() and os.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.

  1. sys.argv is the list of command-line arguments, and argv[0] is the script name.

  2. sys.exit() stops the program.

  3. sys.path is the module search path, and sys.version gives the Python version.

  4. sys.stdin, sys.stdout and sys.stderr are the standard streams.

  5. sys.platform names the operating system, and sys.modules lists 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.

  1. The levels in increasing order are DEBUG, INFO, WARNING, ERROR and CRITICAL.
  2. The default level is WARNING, so lower messages are hidden until it is changed.
  3. logging.basicConfig(level=..., filename=...) sets the level and the output file.
  4. 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
  1. A logger can send output to several places through handlers such as StreamHandler (console) and FileHandler (file).
  2. 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.

  1. re.match() checks only at the start of the string, while re.search() looks anywhere.
  2. re.findall() returns all matches as a list, and re.sub() replaces matches.
  3. \d is a digit, \w a word character, . any character, and ^ and $ anchor the start and end.
  4. *, + and ? mean zero or more, one or more, and zero or one.
import re
re.findall(r"\d+", "a12b345")  # ['12', '345']
  1. re.compile() builds a pattern once so it can be reused, and [abc] matches any one of the listed characters.
  2. 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.

  1. datetime.now() returns the current date and time.

  2. strftime() formats a datetime as text, for example "%d-%m-%Y".

  3. strptime() parses text into a datetime.

  4. Subtracting two dates gives a timedelta, such as 29 days between 1 Feb and 1 Mar 2024.

  5. date.today() gives only the date, and timedelta(days=7) can be added to a date to move it forward.

  6. The module time offers time.time() for seconds since 1970 and time.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.

  1. A socket is one end of a two-way link, identified by an IP address and a port.

  2. socket.socket(AF_INET, SOCK_STREAM) makes a TCP socket, and SOCK_DGRAM makes a UDP one.

  3. A server calls bind(), listen() and accept().

  4. A client calls connect(), and both sides use send(), recv() and close().

  5. TCP is connection-oriented and reliable, while UDP is connectionless and faster but may lose data.

  6. 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.

  1. The threading module creates threads with Thread(target=f), started by start() and waited for with join().

  2. Threads share memory, so they are light but need a Lock to avoid clashes.

  3. Because of the GIL, threads do not run Python code in parallel, so they suit I/O-bound tasks.

  4. The multiprocessing module uses separate processes on several CPU cores, which suits CPU-bound tasks.

  5. Process(target=f) from multiprocessing is started and joined just like a thread.

  6. Processes do not share memory, so they exchange data through a Queue or Pipe.

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, and with closes the file automatically.
  • sys.argv holds the command-line arguments.
  • Logging levels run DEBUG, INFO, WARNING, ERROR, CRITICAL, and the default is WARNING.
  • re.match checks the start only, and re.search scans the whole string.
  • strftime formats a date, and strptime parses 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.
  • strFtime Formats, strPtime Parses.
  • 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.
Go to where you left off?

Quick Add to Notes

Save questions, your own notes and screenshots into notes filed by unit. It takes a free account.

Create free account

Have an account? Log in