Skip to content

Commit f4fcb62

Browse files
authored
Merge branch 'master' into python-bytearray
2 parents c5abce8 + 62c140f commit f4fcb62

17 files changed

+24693
-7
lines changed

polars-lazyframe/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# How to Work With Polars LazyFrames
22

3-
This folder contains completed notebooks and other files used in the Real Python tutorial [How to Work With Polars LazyFrames](https://realpython.com/how-to-work-with-polars-lazyframe/).
3+
This folder contains completed notebooks and other files used in the Real Python tutorial [How to Work With Polars LazyFrames](https://realpython.com/polars-lazyframe/).
44

55
**The following files are included:**
66

polars-lazyframe/tutorial_code.ipynb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
},
8282
{
8383
"cell_type": "code",
84-
"execution_count": 2,
84+
"execution_count": null,
8585
"id": "784b7616-643a-46bc-b449-0fbc5f390178",
8686
"metadata": {},
8787
"outputs": [
@@ -133,13 +133,12 @@
133133
" \"year\": [1970, 1973, 1985, 1991, 1995],\n",
134134
"}\n",
135135
"\n",
136-
"lf = pl.LazyFrame(programming_languages)\n",
137-
"lf.collect()"
136+
"pl.LazyFrame(programming_languages).collect()"
138137
]
139138
},
140139
{
141140
"cell_type": "code",
142-
"execution_count": 3,
141+
"execution_count": null,
143142
"id": "168d4db9-87c0-4ce2-aaab-35f128a03788",
144143
"metadata": {},
145144
"outputs": [
@@ -198,8 +197,8 @@
198197
" \"year\": pl.Int32,\n",
199198
"}\n",
200199
"\n",
201-
"lf = pl.LazyFrame(programming_languages, schema=d_types)\n",
202-
"lf.collect()"
200+
"languages = pl.LazyFrame(programming_languages, schema=d_types)\n",
201+
"languages.collect()"
203202
]
204203
},
205204
{

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.

0 commit comments

Comments
 (0)