Skip to content

Commit 4b903d4

Browse files
authored
Merge branch 'master' into duckdb
2 parents 5b429b7 + 73024dd commit 4b903d4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+26261
-7
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Python's Instance, Class, and Static Methods Demystified
2+
3+
This folder contains code related to the tutorial on [Python's instance, class, and static methods](https://realpython.com/instance-class-and-static-methods-demystified/).
4+
5+
## About the Author
6+
7+
Martin Breuss - Email: [email protected]
8+
9+
## License
10+
11+
Distributed under the MIT license. See `LICENSE` for more information.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class DemoClass:
2+
def instance_method(self):
3+
return ("instance method called", self)
4+
5+
@classmethod
6+
def class_method(cls):
7+
return ("class method called", cls)
8+
9+
@staticmethod
10+
def static_method():
11+
return ("static method called",)
12+
13+
14+
if __name__ == "__main__":
15+
obj = DemoClass()
16+
print(obj.instance_method())
17+
print(obj.class_method())
18+
print(obj.static_method())
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Pizza:
2+
def __init__(self, toppings):
3+
self.toppings = list(toppings)
4+
5+
def __repr__(self):
6+
return f"Pizza({self.toppings})"
7+
8+
def add_topping(self, topping):
9+
self.toppings.append(topping)
10+
11+
def remove_topping(self, topping):
12+
if topping in self.toppings:
13+
self.toppings.remove(topping)
14+
15+
@classmethod
16+
def margherita(cls):
17+
return cls(["mozzarella", "tomatoes"])
18+
19+
@classmethod
20+
def prosciutto(cls):
21+
return cls(["mozzarella", "tomatoes", "ham"])
22+
23+
@staticmethod
24+
def get_size_in_inches(size):
25+
"""Returns an approximate diameter in inches for common sizes."""
26+
size_map = {"small": 8, "medium": 12, "large": 16}
27+
return size_map.get(size, "Unknown size")
28+
29+
30+
if __name__ == "__main__":
31+
a_pizza = Pizza.margherita()
32+
print(a_pizza, "😋🍕")

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)

0 commit comments

Comments
 (0)