Skip to content

Commit 4a9d72f

Browse files
committed
add more example of book
1 parent ab89505 commit 4a9d72f

File tree

6 files changed

+94
-6
lines changed

6 files changed

+94
-6
lines changed

chapter6.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Repeater:
2+
def __init__(self,value):
3+
self.value = value
4+
def __iter__(self):
5+
return self
6+
def __next__(self):
7+
return self.value

class_instance.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import struct
2+
3+
# Define the format string for the data (unsigned integer and a float)
4+
format_string = "I f"
5+
6+
# Create a Struct object with the format string
7+
data_struct = struct.Struct(format_string)
8+
9+
# Data to pack
10+
unsigned_int_value = 42
11+
float_value = 3.14
12+
13+
# Pack the data into binary format
14+
packed_data = data_struct.pack(unsigned_int_value, float_value)
15+
16+
# Print the packed binary data
17+
print("Packed data:", packed_data)
18+
19+
# Unpack the data back into Python values
20+
unpacked_data = data_struct.unpack(packed_data)
21+
print("Unpacked data:", unpacked_data)

copylist.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
a = [1, 2, 3, 4, 5]
2-
b = a
3-
print(b is a)
4-
c = list(a)
1+
class Car:
2+
def __init__(self, color, mileage):
3+
self.color = color
4+
self.mileage = mileage
5+
def __repr__(self):
6+
return (f'{self.__class__.__name__}('f'{self.color!r}, {self.mileage!r})')
57

68

7-
print(a==c)
8-
print(c is a)
9+
10+
car1 = Car("red", 1000)
11+
print(car1)

itreator_generator.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Repeater:
2+
def __init__(self,value):
3+
self.value = value
4+
def __iter__(self):
5+
return self
6+
def __next__(self):
7+
return self.value
8+
9+
10+
#implement above code with generator
11+
12+
13+
14+
def repeater(value):
15+
while True:
16+
yield value
17+
18+
19+
for x in repeater("hi"):
20+
print(x)

test.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import copy
2+
class Point:
3+
def __init__(self, x, y):
4+
self.x = x
5+
self.y = y
6+
def __repr__(self):
7+
return f'Point({self.x!r}, {self.y!r})'
8+
9+
class Rectangle:
10+
def __init__(self, topleft, bottomright):
11+
self.topleft = topleft
12+
self.bottomright = bottomright
13+
def __repr__(self):
14+
return (f'Rectangle({self.topleft!r}, '
15+
f'{self.bottomright!r})')
16+
17+
rect = Rectangle(Point(0, 1), Point(5, 6))
18+
srect = copy.copy(rect)

validate_code.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
3+
class NameShortError(ValueError):
4+
pass
5+
6+
7+
8+
def validate(name):
9+
if len(name) <10:
10+
raise NameShortError(name)
11+
12+
13+
class BaseValidationError(ValueError):
14+
pass
15+
16+
try:
17+
validate(name)
18+
except BaseValidationError as err:
19+
handle_validation_error(err)

0 commit comments

Comments
 (0)