Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python-with-statement/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Python's with Statement: Manage External Resources Safely

This folder provides the code examples for the Real Python tutorial [Python's with Statement: Manage External Resources Safely](https://realpython.com/python-with-statement/).
19 changes: 19 additions & 0 deletions python-with-statement/exc_handling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class HelloContextManager:
def __enter__(self):
print("Entering the context...")
return "Hello, World!"

def __exit__(self, exc_type, exc_value, exc_tb):
print("Leaving the context...")
if isinstance(exc_value, IndexError):
# Handle IndexError here...
print(f"An exception occurred in your with block: {exc_type}")
print(f"Exception message: {exc_value}")
return True


with HelloContextManager() as hello:
print(hello)
hello[100]

print("Continue normally from here...")
19 changes: 19 additions & 0 deletions python-with-statement/hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class HelloContextManager:
def __enter__(self):
print("Entering the context...")
return "Hello, World!"

def __exit__(self, exc_type, exc_value, exc_tb):
print("Leaving the context...")
if isinstance(exc_value, IndexError):
print(f"An exception occurred in with block: {exc_type}")
print(f"Exception message: {exc_value}")
return True
return False


with HelloContextManager() as hello:
print(hello)
# hello[100]

print("Continue normally from here...")
13 changes: 13 additions & 0 deletions python-with-statement/indenter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Indenter:
def __init__(self):
self.level = -1

def __enter__(self):
self.level += 1
return self

def __exit__(self, *_):
self.level -= 1

def print(self, text):
print(" " * self.level + text)
7 changes: 7 additions & 0 deletions python-with-statement/precision.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from decimal import Decimal, localcontext

with localcontext(prec=42):
print(Decimal("1") / Decimal("42"))


print(Decimal("1") / Decimal("42"))
13 changes: 13 additions & 0 deletions python-with-statement/redirect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import sys


class StandardOutputRedirector:
def __init__(self, new_output):
self.new_output = new_output

def __enter__(self):
self.std_output = sys.stdout
sys.stdout = self.new_output

def __exit__(self, *_):
sys.stdout = self.std_output
5 changes: 5 additions & 0 deletions python-with-statement/scan_directory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import os

with os.scandir(".") as dir_entries:
for entry in dir_entries:
print(entry.name, "->", entry.stat().st_size, "bytes")
21 changes: 21 additions & 0 deletions python-with-statement/site_checker_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import asyncio

import aiohttp


async def check(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
print(f"{url}: status -> {response.status}")
html = await response.text()
print(f"{url}: type -> {html[:17].strip()}")


async def main():
await asyncio.gather(
check("https://realpython.com"),
check("https://pycoders.com/"),
)


asyncio.run(main())
33 changes: 33 additions & 0 deletions python-with-statement/site_checker_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import asyncio

import aiohttp


class AsyncSession:
def __init__(self, url):
self._url = url

async def __aenter__(self):
self.session = aiohttp.ClientSession()
response = await self.session.get(self._url)
return response

async def __aexit__(self, *_):
await self.session.close()


async def check(url):
async with AsyncSession(url) as response:
print(f"{url}: status -> {response.status}")
html = await response.text()
print(f"{url}: type -> {html[:17].strip()}")


async def main():
await asyncio.gather(
check("https://realpython.com"),
check("https://pycoders.com"),
)


asyncio.run(main())
15 changes: 15 additions & 0 deletions python-with-statement/timer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from time import perf_counter, sleep


class Timer:
def __enter__(self):
self.start = perf_counter()

def __exit__(self, *_):
end = perf_counter()
print(f"Elapsed time: {end - self.start:.4f} seconds")


with Timer():
# The code to measure goes here...
sleep(0.5)
15 changes: 15 additions & 0 deletions python-with-statement/writable_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class WritableFile:
def __init__(self, file_path):
self.file_path = file_path

def __enter__(self):
self.file_obj = open(self.file_path, mode="w")
return self.file_obj

def __exit__(self, *_):
if self.file_obj:
self.file_obj.close()


with WritableFile("hello.txt") as file:
file.write("Hello, World!\n")
12 changes: 12 additions & 0 deletions python-with-statement/writable_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from contextlib import contextmanager


@contextmanager
def writable_file(file_path):
file = open(file_path, mode="w")
yield file
file.close()


with writable_file("hello.txt") as file:
file.write("Hello, World!")