Z is for zip file mocking - Python A to Z Dark _ □ ✕
  • Posts
  • About
  • Debug
Post

Z is for zip file mocking - Python A to Z

Published
2026-08-29 00:00 UTC (precision: second)
Source feed
hamatti.org [exclude this blog]
Link
https://hamatti.org/posts/z-is-for-zip-file-mocking-python-a-to-z/
Canonical
https://hamatti.org/posts/z-is-for-zip-file-mocking-python-a-to-z
Length
2964 runes
Extracted text
Python A-Z is a blog series about Python. Each day, I share insights, ideas and examples for different parts of Python development that match with the letter of the day. Blaugust is an annual blogging festival in August where the goal is to write a blog post every day of the month. Welcome to Z. The last day of our journey through Python from A to Z. I initially considered writing about zip but I already wrote about zip in Batteries included two years ago and I don’t have anything new to say about it. A while ago in a project, I had to test a function that downloaded a zip file, read an XML file from it and returned some value. I needed to figure out a way to mock an HTTP GET request to return a zip file. Setup In our project, there was a third party library that downloaded a zip file from a hard-coded URL, read a file inside it and returned some data for us. I couldn’t change the function but needed to write a test to keep us safe from changes in that function that would cause hard to catch bugs in our system. For this example, I’ve created a simplification of that function because what it does is not important for this blog post’s main point. Here are the required dependencies to run this: dependencies = [ "beautifulsoup4>=4.15.0", "lxml>=6.1.2", "pytest>=9.1.1", "requests>=2.34.2", "requests-mock>=1.12.1", ] If you want to run this thing for yourself, here’s a function that can be used: # put this in a file: zip_handler.py import io import zipfile from bs4 import BeautifulSoup import requests # also needs lxml installed but not imported # for the xml parser def handle(): resp = requests.get("https://example.com/example.zip") with io.BytesIO(resp.content) as f: zf = zipfile.ZipFile(f) for filename in zf.namelist(): if filename == "timestamps.xml": break out_path = "." zf.extract(filename, out_path) with open(filename, "r") as xmlfile: soup = BeautifulSoup(xmlfile, "xml") # xml parser comes from package lxml return soup.css.select("timeStamp")[0].text Mock it To mock calls to URLs, I used requests-mock that provides a clean interface to deal with this. First, let’s create our mock data. Create a file timestamps.xml with the following <?xml version="1.0" encoding="iso-8859-1"?> <timeStamp>10:00:00</timeStamp> and create a zip file mock.zip with timestamps.xml inside it zip mock.zip timestamps.xml Then create a test file test_zip_handler.py from zip_handler import handle from requests_mock import Mocker def test_timestamp_read_correctly(): with Mocker() as m: with open("mock.zip", "br") as zipfile: m.get("https://example.com/example.zip", body=zipfile) timestamp = handle() assert timestamp == "10:00:00" Here we tell our mocker that we want any call to the specific URL to instead return the zipfile as the response body. Now, the handle function will read our data instead of going to the Interwebs. Now you can run this test with pytest uv run pytest # or however you've installed pytest You should get a green test.
Stored topics

Every stored assignment is listed, including rows below the current cutoff (0.75), so a missing tag can be explained.

Concept Code Score Meter Margin Origin Path
economy, business and finance economy-business-and-finance 0.99 4.89 ↑derived from local:software-development economy, business and finance
products and services products-and-services 0.99 4.89 ↑derived from local:software-development economy, business and finance > products and services
computing and information technology computing-and-information-technology 0.99 4.89 ↑derived from local:software-development economy, business and finance > products and services > computing and information technology
software and applications software-and-applications 0.99 4.89 ↑derived from local:software-development economy, business and finance > products and services > computing and information technology > software and applications
Software development software-development 0.99 4.89 direct economy, business and finance > products and services > computing and information technology > software and applications > Software development
science and technology science-and-technology 0.99 4.61 ↑derived from 20000763 science and technology
technology and engineering technology-and-engineering 0.99 4.61 ↑derived from 20000763 science and technology > technology and engineering
information technology and computer science information-technology-and-computer 0.99 4.61 direct science and technology > technology and engineering > information technology and computer science
arts, culture, entertainment and media arts-culture-entertainment-and-media 0.86 1.85 ↑derived from local:blogging arts, culture, entertainment and media
mass media mass-media 0.86 1.85 ↑derived from local:blogging arts, culture, entertainment and media > mass media
social media social-media 0.86 1.85 ↑derived from local:blogging arts, culture, entertainment and media > mass media > social media
Blogging blogging 0.86 1.85 direct arts, culture, entertainment and media > mass media > social media > Blogging
Information security information-security 0.81 1.47 direct science and technology > technology and engineering > information technology and computer science > Information security
Open source open-source 0.69 0.80 direct economy, business and finance > products and services > computing and information technology > software and applications > Open source
artificial intelligence artificial-intelligence 0.41 -0.37 direct science and technology > technology and engineering > information technology and computer science > artificial intelligence
arts and entertainment arts-and-entertainment 0.17 -1.56 ↑derived from 20000013 arts, culture, entertainment and media > arts and entertainment
literature literature 0.17 -1.56 direct arts, culture, entertainment and media > arts and entertainment > literature
environment environment 0.14 -1.80 direct environment
lifestyle and leisure lifestyle-and-leisure 0.10 -2.18 ↑derived from local:mmorpg lifestyle and leisure
leisure leisure 0.10 -2.18 ↑derived from local:mmorpg lifestyle and leisure > leisure
game game 0.10 -2.18 ↑derived from local:mmorpg lifestyle and leisure > leisure > game
video game video-game 0.10 -2.18 ↑derived from local:mmorpg lifestyle and leisure > leisure > game > video game
MMORPG mmorpg 0.10 -2.18 direct lifestyle and leisure > leisure > game > video game > MMORPG
society society 0.10 -2.18 ↑derived from 20000780 society
family family 0.10 -2.18 direct society > family
crime, law and justice crime-law-and-justice 0.09 -2.30 ↑derived from 20000086 crime, law and justice
crime crime 0.09 -2.30 ↑derived from 20000086 crime, law and justice > crime
cyber crime cyber-crime 0.09 -2.30 direct crime, law and justice > crime > cyber crime
Self-hosting self-hosting 0.08 -2.49 direct economy, business and finance > products and services > computing and information technology > Self-hosting
media and entertainment industry media-and-entertainment-industry 0.07 -2.54 ↑derived from 20000306 economy, business and finance > products and services > media and entertainment industry
books and publishing books-and-publishing 0.07 -2.54 direct economy, business and finance > products and services > media and entertainment industry > books and publishing
hobby hobby 0.07 -2.56 direct lifestyle and leisure > leisure > hobby
Cryptography cryptography 0.06 -2.69 direct science and technology > technology and engineering > information technology and computer science > Cryptography
film industry film-industry 0.06 -2.71 direct economy, business and finance > products and services > media and entertainment industry > film industry
sport sport 0.06 -2.82 ↑derived from 20001183 sport
competition discipline competition-discipline 0.06 -2.82 ↑derived from 20001183 sport > competition discipline
eSports esports 0.06 -2.82 direct sport > competition discipline > eSports
scientific research scientific-research 0.05 -2.85 ↑derived from 20000739 science and technology > scientific research
scientific exploration scientific-exploration 0.05 -2.85 ↑derived from 20000739 science and technology > scientific research > scientific exploration
space exploration space-exploration 0.05 -2.85 direct science and technology > scientific research > scientific exploration > space exploration
education education 0.05 -2.96 direct education
social sciences social-sciences 0.05 -3.03 direct science and technology > social sciences
streaming service streaming-service 0.04 -3.08 direct economy, business and finance > products and services > media and entertainment industry > streaming service
natural science natural-science 0.04 -3.14 direct science and technology > natural science
Tabletop gaming tabletop-gaming 0.04 -3.21 direct lifestyle and leisure > leisure > game > Tabletop gaming
consumer goods consumer-goods 0.04 -3.26 ↑derived from 20001160 economy, business and finance > products and services > consumer goods
handicrafts handicrafts 0.04 -3.26 direct economy, business and finance > products and services > consumer goods > handicrafts
religion religion 0.03 -3.50 direct religion
health health 0.02 -3.69 ↑derived from 20000464 health
health treatment and procedure health-treatment-and-procedure 0.02 -3.69 direct health > health treatment and procedure
human interest human-interest 0.02 -3.76 ↑derived from 20000498 human interest
award and prize award-and-prize 0.02 -3.76 direct human interest > award and prize
fundamental rights fundamental-rights 0.02 -3.95 ↑derived from 20001300 society > fundamental rights
privacy privacy 0.02 -3.95 direct society > fundamental rights > privacy
television television 0.02 -3.96 direct arts, culture, entertainment and media > mass media > television
disease and condition disease-and-condition 0.02 -4.02 ↑derived from 20000458 health > disease and condition
mental health and disorder mental-health-and-disorder 0.02 -4.02 direct health > disease and condition > mental health and disorder
politics and government politics-and-government 0.02 -4.03 direct politics and government
labour labour 0.02 -4.04 direct labour
history history 0.02 -4.09 direct science and technology > social sciences > history
food and drink enthusiasm food-and-drink-enthusiasm 0.01 -4.26 direct lifestyle and leisure > leisure > hobby > food and drink enthusiasm
biology biology 0.01 -4.30 direct science and technology > natural science > biology
visual arts visual-arts 0.01 -4.31 ↑derived from 20000036 arts, culture, entertainment and media > arts and entertainment > visual arts
photography photography 0.01 -4.31 direct arts, culture, entertainment and media > arts and entertainment > visual arts > photography
exercise and fitness exercise-and-fitness 0.01 -4.33 direct lifestyle and leisure > wellness > exercise and fitness
wellness wellness 0.01 -4.33 ↑derived from 20001239 lifestyle and leisure > wellness
conflict, war and peace conflict-war-and-peace 0.01 -4.35 direct conflict, war and peace
board game board-game 0.01 -4.36 direct lifestyle and leisure > leisure > game > board game
card game card-game 0.01 -4.44 direct lifestyle and leisure > leisure > game > card game
disaster, accident and emergency incident disaster-accident-and-emergency-incident 0.01 -4.65 direct disaster, accident and emergency incident
culture culture 0.01 -4.67 direct arts, culture, entertainment and media > culture
climate change climate-change 0.01 -4.68 direct environment > climate change
public health public-health 0.01 -4.78 direct health > public health
weather weather 0.01 -4.79 direct weather
animation animation 0.01 -4.87 direct arts, culture, entertainment and media > arts and entertainment > animation
weather forecast weather-forecast 0.01 -5.07 direct weather > weather forecast
lifestyle lifestyle 0.01 -5.18 ↑derived from 20001257 lifestyle and leisure > lifestyle
house and home house-and-home 0.01 -5.18 ↑derived from 20001257 lifestyle and leisure > lifestyle > house and home
gardening gardening 0.01 -5.18 direct lifestyle and leisure > lifestyle > house and home > gardening
diet diet 0.01 -5.24 direct health > health treatment and procedure > diet
music music 0.00 -5.49 direct arts, culture, entertainment and media > arts and entertainment > music
travel and tourism travel-and-tourism 0.00 -5.81 direct lifestyle and leisure > leisure > travel and tourism
podcast podcast 0.00 -5.88 direct economy, business and finance > products and services > media and entertainment industry > podcast

← Back to posts

feedtagger 0.1.0-dev model: modernbert-base-zeroshot-v2.0/int8@all-s256 2115 posts 0 unclassified min score 0.75