Skip to content

Commit 4605c10

Browse files
Merge branch 'master' into python-selenium
2 parents 67f6d93 + a0f3307 commit 4605c10

28 files changed

+482
-0
lines changed

python-bytearray/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python's Bytearray: A Mutable Sequence of Bytes
2+
3+
This folder contains code associated with the Real Python tutorial [Python's Bytearray: A Mutable Sequence of Bytes](https://realpython.com/python-bytearray/).

python-bytearray/creation.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
WIDTH = 71
2+
3+
print(" Empty ".center(WIDTH, "="))
4+
print(f"{bytearray() = }")
5+
print(f"{bytearray(0) = }")
6+
print(f"{bytearray([]) = }")
7+
print(f"{bytearray(b"") = }\n")
8+
9+
print(" Zero-filled ".center(WIDTH, "="))
10+
print(f"{bytearray(5) = }\n")
11+
12+
print(" From an iterable of integers ".center(WIDTH, "="))
13+
print(f"{bytearray([65, 66, 67]) = }")
14+
print(f"{bytearray(range(3)) = }\n")
15+
16+
print(" From a bytes-like object ".center(WIDTH, "="))
17+
print(f"{bytearray(b"Espa\xc3\xb1ol") = }\n")
18+
19+
print(" From a string ".center(WIDTH, "="))
20+
print(f"{bytearray("Español", "utf-8") = }")
21+
print(f"{bytearray("Español", "ascii", errors="ignore") = }")
22+
print(f"{bytearray("Español", "ascii", errors="replace") = }\n")
23+
24+
print(" From hexadecimal digits".center(WIDTH, "="))
25+
print(f"{bytearray.fromhex("30 8C C9 FF") = }")

python-bytearray/introspection.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
def main():
2+
print(
3+
"Mutator methods in bytearray:",
4+
sorted(public_members(bytearray) - public_members(bytes)),
5+
)
6+
print(
7+
"Special methods in bytearray:",
8+
sorted(magic_members(bytearray) - magic_members(bytes)),
9+
)
10+
show_string_members()
11+
12+
13+
def public_members(cls):
14+
return {name for name in dir(cls) if not name.startswith("_")}
15+
16+
17+
def magic_members(cls):
18+
return {name for name in dir(cls) if name.startswith("__")}
19+
20+
21+
def show_string_members():
22+
print("String members in bytearray:")
23+
for i, name in enumerate(
24+
sorted(
25+
name
26+
for name in set(dir(bytearray)) & set(dir(str))
27+
if not name.startswith("_")
28+
),
29+
start=1,
30+
):
31+
print(f"({i:>2}) .{name}()")
32+
33+
34+
if __name__ == "__main__":
35+
main()

python-bytearray/mutation.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
WIDTH = 71
2+
3+
print(">>> pixels = bytearray([48, 140, 201, 252, 186, 3, 37, 186, 52])")
4+
print(">>> list(pixels)")
5+
pixels = bytearray([48, 140, 201, 252, 186, 3, 37, 186, 52])
6+
print(list(pixels))
7+
8+
print("\n" + " Item assignment ".center(WIDTH, "="))
9+
print(
10+
"""\
11+
>>> for i in range(len(pixels)):
12+
... pixels[i] = 255 - pixels[i]
13+
..."""
14+
)
15+
for i in range(len(pixels)):
16+
pixels[i] = 255 - pixels[i]
17+
print(">>> list(pixels)")
18+
print(list(pixels))
19+
20+
print("\n" + " Slice assignment ".center(WIDTH, "="))
21+
print(">>> pixels[3:6] = (0, 0, 0)")
22+
pixels[3:6] = (0, 0, 0)
23+
print(">>> list(pixels)")
24+
print(list(pixels))
25+
26+
print("\n" + " Slice deletion ".center(WIDTH, "="))
27+
del pixels[3:6]
28+
print(">>> del pixels[3:6]")
29+
print(">>> list(pixels)")
30+
print(list(pixels))
31+
32+
print("\n" + " Item deletion ".center(WIDTH, "="))
33+
del pixels[3]
34+
print(">>> del pixels[3]")
35+
print(">>> list(pixels)")
36+
print(list(pixels))
37+
38+
print("\n" + " Item popping ".center(WIDTH, "="))
39+
print(">>> fourth_byte = pixels.pop(3)")
40+
print(">>> last_byte = pixels.pop()")
41+
fourth_byte = pixels.pop(3)
42+
last_byte = pixels.pop()
43+
print(">>> list(pixels)")
44+
print(list(pixels))
45+
46+
print("\n" + " Value removal ".center(WIDTH, "="))
47+
print(">>> pixels.remove(115)")
48+
pixels.remove(115)
49+
print(">>> list(pixels)")
50+
print(list(pixels))
51+
52+
print("\n" + " Clearing all items ".center(WIDTH, "="))
53+
print(">>> pixels.clear()")
54+
pixels.clear()
55+
print(">>> list(pixels)")
56+
print(list(pixels))
57+
58+
print("\n" + " Appending an item ".center(WIDTH, "="))
59+
print(">>> pixels.append(65)")
60+
print(">>> pixels.append(67)")
61+
pixels.append(65)
62+
pixels.append(67)
63+
print(">>> list(pixels)")
64+
print(list(pixels))
65+
66+
print("\n" + " Inserting an item ".center(WIDTH, "="))
67+
print(">>> pixels.insert(1, 66)")
68+
pixels.insert(1, 66)
69+
print(">>> list(pixels)")
70+
print(list(pixels))
71+
72+
print("\n" + " Extending the bytearray ".center(WIDTH, "="))
73+
print(">>> pixels.extend((1, 2, 3))")
74+
pixels.extend((1, 2, 3))
75+
print(">>> list(pixels)")
76+
print(list(pixels))
77+
78+
print("\n" + " Reversal ".center(WIDTH, "="))
79+
print(">>> pixels.reverse()")
80+
pixels.reverse()
81+
print(">>> list(pixels)")
82+
print(list(pixels))
83+
84+
print("\n" + " Making a copy ".center(WIDTH, "="))
85+
print(">>> pixels_copy = pixels.copy()")
86+
pixels_copy = pixels.copy()
87+
print(">>> list(pixels)")
88+
print(list(pixels))

python-dict-attribute/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Using Python's `.__dict__` to Work With Attributes
2+
3+
This folder provides the code examples for the Real Python tutorial [Using Python's `.__dict__` to Work With Attributes](https://realpython.com/python-dict-attribute/).
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Parent:
2+
def __init__(self):
3+
self.parent_attr = "parent"
4+
5+
6+
class Child(Parent):
7+
def __init__(self):
8+
super().__init__()
9+
self.child_attr = "child"
10+
11+
12+
parent = Parent()
13+
print(parent.__dict__)
14+
15+
child = Child()
16+
print(child.__dict__)

python-dict-attribute/config_v1.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Config:
2+
def __init__(self, **kwargs):
3+
self.__dict__.update(kwargs)
4+
5+
update = __init__
6+
7+
def __str__(self):
8+
return str(self.__dict__)
9+
10+
11+
config = Config(theme="light", font_size=12, language="English")
12+
print(config)
13+
14+
user = {"theme": "dark", "font_size": 14, "language": "Spanish"}
15+
config.update(**user)
16+
print(config)

python-dict-attribute/config_v2.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Config:
2+
def __init__(self, name):
3+
self.name = name
4+
5+
def set_option(self, key, value):
6+
self.__dict__[key] = value
7+
8+
def get_option(self, key):
9+
return self.__dict__.get(key, None)
10+
11+
def remove_option(self, key):
12+
if key in self.__dict__:
13+
del self.__dict__[key]
14+
# self.__dict__.pop(key)
15+
print(f"'{key}' removed!")
16+
else:
17+
print(f"'{key}' does not exist.")
18+
19+
def clear(self):
20+
self.__dict__.clear()
21+
print("All options removed!")
22+
23+
24+
conf = Config("GUI App")
25+
conf.set_option("theme", "dark")
26+
conf.set_option("size", "200x400")
27+
print(conf.__dict__)
28+
conf.remove_option("size")
29+
print(conf.__dict__)
30+
conf.remove_option("autosave")
31+
print(conf.__dict__)
32+
conf.clear()
33+
print(conf.__dict__)

python-dict-attribute/config_v3.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Config:
2+
def __init__(self, name):
3+
self.name = name
4+
5+
def set_option(self, key, value):
6+
setattr(self, key, value)
7+
8+
def get_option(self, key):
9+
return getattr(self, key, None)
10+
11+
def remove_option(self, key):
12+
if hasattr(self, key):
13+
delattr(self, key)
14+
print(f"'{key}' removed!")
15+
else:
16+
print(f"'{key}' does not exist.")
17+
18+
def clear(self):
19+
for key in list(self.__dict__.keys()):
20+
delattr(self, key)
21+
print("All options removed!")
22+
23+
24+
conf = Config("GUI App")
25+
conf.set_option("theme", "dark")
26+
conf.set_option("size", "200x400")
27+
print(conf.__dict__)
28+
conf.remove_option("size")
29+
print(conf.__dict__)
30+
conf.remove_option("autosave") # Raises KeyError
31+
print(conf.__dict__)
32+
conf.clear()
33+
print(conf.__dict__)

python-dict-attribute/demo.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class DemoClass:
2+
class_attr = "This is a class attribute"
3+
4+
def __init__(self):
5+
self.instance_attr = "This is an instance attribute"
6+
7+
def method(self):
8+
return "This is a method"
9+
10+
11+
print(DemoClass.__dict__)
12+
demo_object = DemoClass()
13+
print(demo_object.__dict__)

0 commit comments

Comments
 (0)