22A 2-dimensional vector class
33
44 >>> v1 = Vector2d(3, 4)
5- >>> x, y = v1 #<1>
5+ >>> print(v1.x, v1.y)
6+ 3.0 4.0
7+ >>> x, y = v1
68 >>> x, y
79 (3.0, 4.0)
8- >>> v1 #<2>
10+ >>> v1
911 Vector2d(3.0, 4.0)
10- >>> v1_clone = eval(repr(v1)) #<3>
12+ >>> v1_clone = eval(repr(v1))
1113 >>> v1 == v1_clone
1214 True
13- >>> print(v1) #<4>
15+ >>> print(v1)
1416 (3.0, 4.0)
15- >>> octets = bytes(v1) #<5>
17+ >>> octets = bytes(v1)
1618 >>> octets
17- b'\\ x00\\ x00\\ x00\\ x00\\ x00\\ x00\\ x08@\\ x00\\ x00\\ x00\\ x00\\ x00\\ x00\\ x10@'
18- >>> abs(v1) #<6>
19+ b'd \\ x00\\ x00\\ x00\\ x00\\ x00\\ x00\\ x08@\\ x00\\ x00\\ x00\\ x00\\ x00\\ x00\\ x10@'
20+ >>> abs(v1)
1921 5.0
20- >>> bool(v1), bool(Vector2d(0, 0)) #<7>
22+ >>> bool(v1), bool(Vector2d(0, 0))
2123 (True, False)
2224
2325
24- Test of .frombytes() class method:
26+ Test of `` .frombytes()`` class method:
2527
2628 >>> v1_clone = Vector2d.frombytes(bytes(v1))
2729 >>> v1_clone
6264 >>> format(Vector2d(1, 1), '0.5fp')
6365 '<1.41421, 0.78540>'
6466
65- # BEGIN VECTOR2D_V3_DEMO
66- Test of `x` and `y` read-only properties:
67+
68+ Tests of `x` and `y` read-only properties:
6769
6870 >>> v1.x, v1.y
6971 (3.0, 4.0)
7274 ...
7375 AttributeError: can't set attribute
7476
75- # END VECTOR2D_V3_HASH_DEMO
7677
77- # BEGIN VECTOR2D_V3_HASH_DEMO
78+ Tests of hashing:
7879
7980 >>> v1 = Vector2d(3, 4)
8081 >>> v2 = Vector2d(3.1, 4.2)
8384 >>> len(set([v1, v2]))
8485 2
8586
86- # END VECTOR2D_V3_DEMO
87-
88-
8987"""
9088
9189from array import array
9290import math
9391
94- # BEGIN VECTOR2D_V3
9592class Vector2d :
9693 typecode = 'd'
9794
9895 def __init__ (self , x , y ):
99- self .__x = float (x ) # <1>
96+ self .__x = float (x )
10097 self .__y = float (y )
10198
102- @property # <2>
103- def x (self ): # <3>
104- return self .__x # <4>
99+ @property
100+ def x (self ):
101+ return self .__x
105102
106- @property # <5>
103+ @property
107104 def y (self ):
108105 return self .__y
109106
110107 def __iter__ (self ):
111- return (i for i in (self .x , self .y )) # <6>
112-
113- # remaining methods follow (omitted in book listing)
114- # END VECTOR2D_V3
108+ return (i for i in (self .x , self .y ))
115109
116110 def __repr__ (self ):
117- return 'Vector2d({!r}, {!r})' .format (* self )
111+ class_name = type (self ).__name__
112+ return '{}({!r}, {!r})' .format (class_name , * self )
118113
119114 def __str__ (self ):
120115 return str (tuple (self ))
121116
122117 def __bytes__ (self ):
123- return bytes (array (Vector2d .typecode , self ))
118+ return (bytes ([ord (self .typecode )]) +
119+ bytes (array (self .typecode , self )))
124120
125121 def __eq__ (self , other ):
126122 return tuple (self ) == tuple (other )
127123
128- # BEGIN VECTOR_V3_HASH
129124 def __hash__ (self ):
130125 return hash (self .x ) ^ hash (self .y )
131- # END VECTOR_V3_HASH
132126
133127 def __abs__ (self ):
134128 return math .hypot (self .x , self .y )
@@ -152,5 +146,6 @@ def __format__(self, fmt_spec=''):
152146
153147 @classmethod
154148 def frombytes (cls , octets ):
155- memv = memoryview (octets ).cast (cls .typecode )
149+ typecode = chr (octets [0 ])
150+ memv = memoryview (octets [1 :]).cast (typecode )
156151 return cls (* memv )
0 commit comments