Skip to content

Commit c55e2d9

Browse files
authored
Merge pull request #637 from realpython/python-bytes
Python Bytes Materials
2 parents 4ecedac + e337b3e commit c55e2d9

File tree

15 files changed

+24687
-0
lines changed

15 files changed

+24687
-0
lines changed

python-bytes/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Bytes Objects: Handling Binary Data in Python
2+
3+
This folder contains code associated with the Real Python tutorial [Bytes Objects: Handling Binary Data in Python](https://realpython.com/python-bytes/).
4+
5+
## Prerequisites
6+
7+
Install the requirements into your virtual environment:
8+
9+
```shell
10+
(venv) $ python -m pip install -r requirements.txt
11+
```
12+
13+
Start the Redis server in a Docker container:
14+
15+
```sh
16+
$ docker run --rm -d -p 6379:6379 redis
17+
```

python-bytes/checksum.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import hashlib
2+
import sys
3+
from pathlib import Path
4+
5+
python_path = Path(sys.executable)
6+
checksum_path = python_path.with_suffix(".md5")
7+
8+
machine_code = python_path.read_bytes()
9+
checksum_path.write_bytes(hashlib.md5(machine_code).digest())
10+
11+
print("Saved", checksum_path)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import hashlib
2+
import sys
3+
from pathlib import Path
4+
5+
6+
def calculate_checksum(path: Path, chunk_size: int = 4096) -> bytes:
7+
checksum = hashlib.md5()
8+
with path.open(mode="rb") as file:
9+
while chunk := file.read(chunk_size):
10+
checksum.update(chunk)
11+
return checksum.digest()
12+
13+
14+
if __name__ == "__main__":
15+
print(calculate_checksum(Path(sys.executable)))

python-bytes/embed.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from base64 import b64encode
2+
from pathlib import Path
3+
4+
5+
def embed_image(path: Path, label: str = "") -> str:
6+
ascii_string = b64encode(path.read_bytes()).decode("ascii")
7+
print(len(path.read_bytes()))
8+
print(len(ascii_string))
9+
return f"![{label}](data:image/jpeg;base64,{ascii_string})"
10+
11+
12+
if __name__ == "__main__":
13+
output_path = Path("picture.md")
14+
output_path.write_text(embed_image(Path("picture.jpg")))
15+
print("Saved", output_path)

python-bytes/find_timestamps.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import re
2+
from pathlib import Path
3+
4+
binary_data = Path("picture.jpg").read_bytes()
5+
pattern = rb"\d{4}:\d{2}:\d{2} \d{2}:\d{2}:\d{2}"
6+
7+
for match in re.finditer(pattern, binary_data):
8+
print(match)

python-bytes/generate_image.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import numpy as np
2+
from PIL import Image
3+
4+
pixels = np.random.default_rng().random((1080, 1920, 3)) * 255
5+
image = Image.fromarray(pixels.astype("uint8"))
6+
image.save("image.png")

python-bytes/image.png

5.94 MB
Loading

python-bytes/map.osm

Lines changed: 24408 additions & 0 deletions
Large diffs are not rendered by default.

python-bytes/osm_mmap.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from mmap import ACCESS_READ, mmap
2+
from pathlib import Path
3+
from typing import Iterator, Self
4+
5+
6+
class OpenStreetMap:
7+
def __init__(self, path: Path) -> None:
8+
self.file = path.open(mode="rb")
9+
self.stream = mmap(self.file.fileno(), 0, access=ACCESS_READ)
10+
11+
def __enter__(self) -> Self:
12+
return self
13+
14+
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
15+
self.stream.close()
16+
self.file.close()
17+
18+
def __iter__(self) -> Iterator[bytes]:
19+
end = 0
20+
while (begin := self.stream.find(b"<way", end)) != -1:
21+
end = self.stream.find(b"</way>", begin)
22+
yield self.stream[begin : end + len(b"</way>")]
23+
24+
25+
if __name__ == "__main__":
26+
with OpenStreetMap(Path("map.osm")) as osm:
27+
for way_tag in osm:
28+
print(way_tag)

python-bytes/picture.jpg

143 KB
Loading

0 commit comments

Comments
 (0)