Skip to content

Commit bdccc32

Browse files
committed
update from Atlas
1 parent cf96836 commit bdccc32

25 files changed

+393
-46
lines changed
File renamed without changes.

classes/vector_v2.py renamed to 10-seq-hacking/vector_v2.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
from array import array
113113
import reprlib
114114
import math
115+
import numbers
115116

116117

117118
class Vector:
@@ -150,13 +151,13 @@ def __len__(self):
150151

151152
def __getitem__(self, index):
152153
cls = type(self) # <1>
153-
if isinstance(index, slice):
154-
return cls(self._components[index]) # <2>
155-
elif isinstance(index, int):
156-
return self._components[index] # <3>
154+
if isinstance(index, slice): # <2>
155+
return cls(self._components[index]) # <3>
156+
elif isinstance(index, numbers.Integral): # <4>
157+
return self._components[index] # <5>
157158
else:
158159
msg = '{cls.__name__} indices must be integers'
159-
raise TypeError(msg.format(cls=cls)) # <4>
160+
raise TypeError(msg.format(cls=cls)) # <6>
160161
# END VECTOR_V2
161162

162163
@classmethod

classes/vector_v3.py renamed to 10-seq-hacking/vector_v3.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
from array import array
156156
import reprlib
157157
import math
158+
import numbers
158159

159160

160161
class Vector:
@@ -194,7 +195,7 @@ def __getitem__(self, index):
194195
cls = type(self)
195196
if isinstance(index, slice):
196197
return cls(self._components[index])
197-
elif isinstance(index, int):
198+
elif isinstance(index, numbers.Integral):
198199
return self._components[index]
199200
else:
200201
msg = '{.__name__} indices must be integers'

classes/vector_v4.py renamed to 10-seq-hacking/vector_v4.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,22 @@
135135
>>> v2 = Vector([3.1, 4.2])
136136
>>> v3 = Vector([3, 4, 5])
137137
>>> v6 = Vector(range(6))
138-
>>> hash(v1), hash(v2), hash(v3), hash(v6)
139-
(7, 384307168202284039, 2, 1)
140-
>>> len(set([v1, v2, v3, v6]))
141-
4
138+
>>> hash(v1), hash(v3), hash(v6)
139+
(7, 2, 1)
142140
143141
142+
Most hash values of non-integers vary from a 32-bit to 64-bit CPython build::
143+
144+
>>> import sys
145+
>>> hash(v2) == (384307168202284039 if sys.maxsize > 2**32 else 357915986)
146+
True
147+
144148
"""
145149

146150
from array import array
147151
import reprlib
148152
import math
153+
import numbers
149154
import functools
150155
import operator
151156

@@ -192,11 +197,11 @@ def __getitem__(self, index):
192197
cls = type(self)
193198
if isinstance(index, slice):
194199
return cls(self._components[index])
195-
elif isinstance(index, int):
200+
elif isinstance(index, numbers.Integral):
196201
return self._components[index]
197202
else:
198-
msg = '{.__name__} indices must be integers'
199-
raise TypeError(msg.format(cls))
203+
msg = '{cls.__name__} indices must be integers'
204+
raise TypeError(msg.format(cls=cls))
200205

201206
shortcut_names = 'xyzt'
202207

classes/vector_v5.py renamed to 10-seq-hacking/vector_v5.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,15 @@
136136
>>> v2 = Vector([3.1, 4.2])
137137
>>> v3 = Vector([3, 4, 5])
138138
>>> v6 = Vector(range(6))
139-
>>> hash(v1), hash(v2), hash(v3), hash(v6)
140-
(7, 384307168202284039, 2, 1)
141-
>>> len(set([v1, v2, v3, v6]))
142-
4
139+
>>> hash(v1), hash(v3), hash(v6)
140+
(7, 2, 1)
141+
142+
143+
Most hash values of non-integers vary from a 32-bit to 64-bit CPython build::
144+
145+
>>> import sys
146+
>>> hash(v2) == (384307168202284039 if sys.maxsize > 2**32 else 357915986)
147+
True
143148
144149
145150
Tests of ``format()`` with Cartesian coordinates in 2D::
@@ -187,6 +192,7 @@
187192
from array import array
188193
import reprlib
189194
import math
195+
import numbers
190196
import functools
191197
import operator
192198
import itertools # <1>
@@ -234,7 +240,7 @@ def __getitem__(self, index):
234240
cls = type(self)
235241
if isinstance(index, slice):
236242
return cls(self._components[index])
237-
elif isinstance(index, int):
243+
elif isinstance(index, numbers.Integral):
238244
return self._components[index]
239245
else:
240246
msg = '{.__name__} indices must be integers'
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

interfaces/tombola_tests.rst renamed to 11-iface-abc/tombola_tests.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ thrown when the device is empty::
5050
OK
5151

5252

53-
Load and pick 100 balls to verify that they are all come out::
53+
Load and pick 100 balls to verify that they all come out::
5454

5555
>>> balls = list(range(100))
5656
>>> globe = ConcreteTombola(balls)
@@ -63,17 +63,17 @@ Load and pick 100 balls to verify that they are all come out::
6363
True
6464

6565

66-
Check that the order has changed is not simply reversed either::
66+
Check that the order has changed and is not simply reversed::
6767

6868
>>> picks != balls
6969
True
7070
>>> picks[::-1] != balls
7171
True
7272

7373
Note: the previous 2 tests have a *very* small chance of failing
74-
even if the implementation is OK. The probability of the 100
74+
even if the implementation is OK. The probability of the 100
7575
balls coming out, by chance, in the order they were loaded is
76-
1/100!, or approximately 1.07e-158. It's much easier to win the
76+
1/100!, or approximately 1.07e-158. It's much easier to win the
7777
Lotto or to become a billionaire working as a programmer.
7878

7979
THE END

interfaces/tombolist.py renamed to 11-iface-abc/tombolist.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class TomboList(list): # <2>
88
def pick(self):
99
if self: # <3>
1010
position = randrange(len(self))
11-
return super().pop(position) # <4>
11+
return self.pop(position) # <4>
1212
else:
1313
raise LookupError('pop from empty TomboList')
1414

interfaces/diamond.py renamed to 12-inheritance/diamond.py

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ def pong(self):
1414

1515

1616
class D(B, C):
17+
18+
def ping(self):
19+
super().ping()
20+
print('post-ping:', self)
21+
1722
def pingpong(self):
23+
self.ping()
1824
super().ping()
25+
self.pong()
1926
super().pong()
27+
C.pong(self)
File renamed without changes.
File renamed without changes.
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
# BEGIN UNARY_PLUS_DECIMAL
3+
4+
>>> import decimal
5+
>>> ctx = decimal.getcontext() # <1>
6+
>>> ctx.prec = 40 # <2>
7+
>>> one_third = decimal.Decimal('1') / decimal.Decimal('3') # <3>
8+
>>> one_third # <4>
9+
Decimal('0.3333333333333333333333333333333333333333')
10+
>>> one_third == +one_third # <5>
11+
True
12+
>>> ctx.prec = 28 # <6>
13+
>>> one_third == +one_third # <7>
14+
False
15+
>>> +one_third # <8>
16+
Decimal('0.3333333333333333333333333333')
17+
18+
# END UNARY_PLUS_DECIMAL
19+
20+
"""
21+
22+
import decimal
23+
24+
if __name__ == '__main__':
25+
26+
with decimal.localcontext() as ctx:
27+
ctx.prec = 40
28+
print('precision:', ctx.prec)
29+
one_third = decimal.Decimal('1') / decimal.Decimal('3')
30+
print(' one_third:', one_third)
31+
print(' +one_third:', +one_third)
32+
33+
print('precision:', decimal.getcontext().prec)
34+
print(' one_third:', one_third)
35+
print(' +one_third:', +one_third)

13-op-overloading/vector2d_v3.py

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
"""
2+
A 2-dimensional vector class
3+
4+
>>> v1 = Vector2d(3, 4)
5+
>>> print(v1.x, v1.y)
6+
3.0 4.0
7+
>>> x, y = v1
8+
>>> x, y
9+
(3.0, 4.0)
10+
>>> v1
11+
Vector2d(3.0, 4.0)
12+
>>> v1_clone = eval(repr(v1))
13+
>>> v1 == v1_clone
14+
True
15+
>>> print(v1)
16+
(3.0, 4.0)
17+
>>> octets = bytes(v1)
18+
>>> octets
19+
b'd\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@'
20+
>>> abs(v1)
21+
5.0
22+
>>> bool(v1), bool(Vector2d(0, 0))
23+
(True, False)
24+
25+
26+
Test of ``.frombytes()`` class method:
27+
28+
>>> v1_clone = Vector2d.frombytes(bytes(v1))
29+
>>> v1_clone
30+
Vector2d(3.0, 4.0)
31+
>>> v1 == v1_clone
32+
True
33+
34+
35+
Tests of ``format()`` with Cartesian coordinates:
36+
37+
>>> format(v1)
38+
'(3.0, 4.0)'
39+
>>> format(v1, '.2f')
40+
'(3.00, 4.00)'
41+
>>> format(v1, '.3e')
42+
'(3.000e+00, 4.000e+00)'
43+
44+
45+
Tests of the ``angle`` method::
46+
47+
>>> Vector2d(0, 0).angle()
48+
0.0
49+
>>> Vector2d(1, 0).angle()
50+
0.0
51+
>>> epsilon = 10**-8
52+
>>> abs(Vector2d(0, 1).angle() - math.pi/2) < epsilon
53+
True
54+
>>> abs(Vector2d(1, 1).angle() - math.pi/4) < epsilon
55+
True
56+
57+
58+
Tests of ``format()`` with polar coordinates:
59+
60+
>>> format(Vector2d(1, 1), 'p') # doctest:+ELLIPSIS
61+
'<1.414213..., 0.785398...>'
62+
>>> format(Vector2d(1, 1), '.3ep')
63+
'<1.414e+00, 7.854e-01>'
64+
>>> format(Vector2d(1, 1), '0.5fp')
65+
'<1.41421, 0.78540>'
66+
67+
68+
Tests of `x` and `y` read-only properties:
69+
70+
>>> v1.x, v1.y
71+
(3.0, 4.0)
72+
>>> v1.x = 123
73+
Traceback (most recent call last):
74+
...
75+
AttributeError: can't set attribute
76+
77+
78+
Tests of hashing:
79+
80+
>>> v1 = Vector2d(3, 4)
81+
>>> v2 = Vector2d(3.1, 4.2)
82+
>>> hash(v1), hash(v2)
83+
(7, 384307168202284039)
84+
>>> len(set([v1, v2]))
85+
2
86+
87+
"""
88+
89+
from array import array
90+
import math
91+
92+
class Vector2d:
93+
typecode = 'd'
94+
95+
def __init__(self, x, y):
96+
self.__x = float(x)
97+
self.__y = float(y)
98+
99+
@property
100+
def x(self):
101+
return self.__x
102+
103+
@property
104+
def y(self):
105+
return self.__y
106+
107+
def __iter__(self):
108+
return (i for i in (self.x, self.y))
109+
110+
def __repr__(self):
111+
class_name = type(self).__name__
112+
return '{}({!r}, {!r})'.format(class_name, *self)
113+
114+
def __str__(self):
115+
return str(tuple(self))
116+
117+
def __bytes__(self):
118+
return (bytes([ord(self.typecode)]) +
119+
bytes(array(self.typecode, self)))
120+
121+
def __eq__(self, other):
122+
return tuple(self) == tuple(other)
123+
124+
def __hash__(self):
125+
return hash(self.x) ^ hash(self.y)
126+
127+
def __abs__(self):
128+
return math.hypot(self.x, self.y)
129+
130+
def __bool__(self):
131+
return bool(abs(self))
132+
133+
def angle(self):
134+
return math.atan2(self.y, self.x)
135+
136+
def __format__(self, fmt_spec=''):
137+
if fmt_spec.endswith('p'):
138+
fmt_spec = fmt_spec[:-1]
139+
coords = (abs(self), self.angle())
140+
outer_fmt = '<{}, {}>'
141+
else:
142+
coords = self
143+
outer_fmt = '({}, {})'
144+
components = (format(c, fmt_spec) for c in coords)
145+
return outer_fmt.format(*components)
146+
147+
@classmethod
148+
def frombytes(cls, octets):
149+
typecode = chr(octets[0])
150+
memv = memoryview(octets[1:]).cast(typecode)
151+
return cls(*memv)

classes/vector_py3_5.py renamed to 13-op-overloading/vector_py3_5.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,15 @@
137137
>>> v2 = Vector([3.1, 4.2])
138138
>>> v3 = Vector([3, 4, 5])
139139
>>> v6 = Vector(range(6))
140-
>>> hash(v1), hash(v2), hash(v3), hash(v6)
141-
(7, 384307168202284039, 2, 1)
142-
>>> len(set([v1, v2, v3, v6]))
143-
4
140+
>>> hash(v1), hash(v3), hash(v6)
141+
(7, 2, 1)
142+
143+
144+
Most hash values of non-integers vary from a 32-bit to 64-bit Python build::
145+
146+
>>> import sys
147+
>>> hash(v2) == (384307168202284039 if sys.maxsize > 2**32 else 357915986)
148+
True
144149
145150
146151
Tests of ``format()`` with Cartesian coordinates in 2D::

0 commit comments

Comments
 (0)