Skip to content

Commit 981d5bc

Browse files
committed
updated contents from Atlas repo
1 parent 40688c0 commit 981d5bc

File tree

157 files changed

+71134
-1
lines changed

Some content is hidden

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

157 files changed

+71134
-1
lines changed

classes/mem_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import importlib
2+
import sys
3+
import resource
4+
5+
NUM_VECTORS = 10**7
6+
7+
if len(sys.argv) == 2:
8+
module_name = sys.argv[1].replace('.py', '')
9+
module = importlib.import_module(module_name)
10+
else:
11+
print('Usage: {} <vector-module-to-test>'.format())
12+
sys.exit(1)
13+
14+
fmt = 'Selected Vector type: {.__name__}.{.__name__}'
15+
print(fmt.format(module, module.Vector))
16+
17+
mem_init = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
18+
print('Creating {:,} Vector instances'.format(NUM_VECTORS))
19+
20+
vectors = [module.Vector(3.0, 4.0) for i in range(NUM_VECTORS)]
21+
22+
mem_final = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
23+
print('Initial RAM usage: {:14,}'.format(mem_init))
24+
print(' Final RAM usage: {:14,}'.format(mem_final))

classes/vector_v0.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
A 2-dimensional vector class
3+
4+
# BEGIN VECTOR_V0_DEMO
5+
6+
>>> v1 = Vector(3, 4)
7+
>>> x, y = v1 #<1>
8+
>>> x, y
9+
(3.0, 4.0)
10+
>>> v1 #<2>
11+
Vector(3.0, 4.0)
12+
>>> v1_clone = eval(repr(v1)) #<3>
13+
>>> v1 == v1_clone
14+
True
15+
>>> print(v1) #<4>
16+
(3.0, 4.0)
17+
>>> octets = bytes(v1) #<5>
18+
>>> octets
19+
b'\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@'
20+
>>> abs(v1) #<6>
21+
5.0
22+
>>> bool(v1), bool(Vector(0, 0)) #<7>
23+
(True, False)
24+
25+
# END VECTOR_V0_DEMO
26+
"""
27+
28+
# BEGIN VECTOR_V0
29+
from array import array
30+
import math
31+
32+
33+
class Vector:
34+
typecode = 'd' # <1>
35+
36+
def __init__(self, x, y):
37+
self.x = float(x) # <2>
38+
self.y = float(y)
39+
40+
def __iter__(self):
41+
return (i for i in (self.x, self.y)) # <3>
42+
43+
def __repr__(self):
44+
return 'Vector({!r}, {!r})'.format(*self) # <4>
45+
46+
def __str__(self):
47+
return str(tuple(self)) # <5>
48+
49+
def __bytes__(self):
50+
return bytes(array(Vector.typecode, self)) # <6>
51+
52+
def __eq__(self, other):
53+
return tuple(self) == tuple(other) # <7>
54+
55+
def __abs__(self):
56+
return math.hypot(self.x, self.y)
57+
58+
def __bool__(self):
59+
return bool(abs(self)) # <8>
60+
# END VECTOR_V0

classes/vector_v1.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
"""
2+
A 2-dimensional vector class
3+
4+
>>> v1 = Vector(3, 4)
5+
>>> x, y = v1 #<1>
6+
>>> x, y
7+
(3.0, 4.0)
8+
>>> v1 #<2>
9+
Vector(3.0, 4.0)
10+
>>> v1_clone = eval(repr(v1)) #<3>
11+
>>> v1 == v1_clone
12+
True
13+
>>> print(v1) #<4>
14+
(3.0, 4.0)
15+
>>> octets = bytes(v1) #<5>
16+
>>> octets
17+
b'\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@'
18+
>>> abs(v1) #<6>
19+
5.0
20+
>>> bool(v1), bool(Vector(0, 0)) #<7>
21+
(True, False)
22+
23+
Test of .frombytes() class method:
24+
25+
>>> v1_clone = Vector.frombytes(bytes(v1))
26+
>>> v1_clone
27+
Vector(3.0, 4.0)
28+
>>> v1 == v1_clone
29+
True
30+
31+
So far, Vector instances are unhashable:
32+
33+
# BEGIN VECTOR_V1_UNHASHABLE_DEMO
34+
>>> v1 = Vector(3, 4)
35+
>>> hash(v1)
36+
Traceback (most recent call last):
37+
...
38+
TypeError: unhashable type: 'Vector'
39+
>>> set([v1])
40+
Traceback (most recent call last):
41+
...
42+
TypeError: unhashable type: 'Vector'
43+
44+
# END VECTOR_V1_UNHASHABLE_DEMO
45+
46+
"""
47+
48+
from array import array
49+
import math
50+
51+
52+
class Vector:
53+
typecode = 'd'
54+
55+
def __init__(self, x, y):
56+
self.x = float(x)
57+
self.y = float(y)
58+
59+
def __iter__(self):
60+
return (i for i in (self.x, self.y))
61+
62+
def __repr__(self):
63+
return 'Vector({!r}, {!r})'.format(*self)
64+
65+
def __str__(self):
66+
return str(tuple(self))
67+
68+
def __bytes__(self):
69+
return bytes(array(Vector.typecode, self))
70+
71+
def __eq__(self, other):
72+
return tuple(self) == tuple(other)
73+
74+
def __abs__(self):
75+
return math.hypot(self.x, self.y)
76+
77+
def __bool__(self):
78+
return bool(abs(self))
79+
80+
# BEGIN VECTOR_V1
81+
@classmethod # <1>
82+
def frombytes(cls, octets): # <2>
83+
arr = array(Vector.typecode) # <3>
84+
arr.frombytes(octets) # <4>
85+
return cls(*arr) # <5>
86+
# END VECTOR_V1

classes/vector_v2.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
"""
2+
A 2-dimensional vector class
3+
4+
>>> v1 = Vector(3, 4)
5+
>>> x, y = v1
6+
>>> x, y
7+
(3.0, 4.0)
8+
>>> v1
9+
Vector(3.0, 4.0)
10+
>>> v1_clone = eval(repr(v1))
11+
>>> v1 == v1_clone
12+
True
13+
>>> print(v1)
14+
(3.0, 4.0)
15+
>>> octets = bytes(v1)
16+
>>> octets
17+
b'\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@'
18+
>>> abs(v1)
19+
5.0
20+
>>> bool(v1), bool(Vector(0, 0))
21+
(True, False)
22+
23+
Test of ``.frombytes()`` class method:
24+
25+
>>> v1_clone = Vector.frombytes(bytes(v1))
26+
>>> v1_clone
27+
Vector(3.0, 4.0)
28+
>>> v1 == v1_clone
29+
True
30+
31+
Tests of ``format()`` with rectangular coordinates:
32+
33+
>>> format(v1)
34+
'(3.0, 4.0)'
35+
>>> format(v1, '.2f')
36+
'(3.00, 4.00)'
37+
>>> format(v1, '.3e')
38+
'(3.000e+00, 4.000e+00)'
39+
40+
Tests of the ``angle`` method::
41+
42+
>>> Vector(0, 0).angle()
43+
0.0
44+
>>> Vector(1, 0).angle()
45+
0.0
46+
>>> epsilon = 10**-8
47+
>>> abs(Vector(0, 1).angle() - math.pi/2) < epsilon
48+
True
49+
>>> abs(Vector(1, 1).angle() - math.pi/4) < epsilon
50+
True
51+
52+
Tests of ``format()`` with polar coordinates:
53+
54+
>>> format(Vector(1, 1), 'p') # doctest:+ELLIPSIS
55+
'<1.414213..., 0.785398...>'
56+
>>> format(Vector(1, 1), '.3ep')
57+
'<1.414e+00, 7.854e-01>'
58+
>>> format(Vector(1, 1), '0.5fp')
59+
'<1.41421, 0.78540>'
60+
61+
"""
62+
63+
from array import array
64+
import math
65+
66+
67+
class Vector:
68+
typecode = 'd'
69+
70+
def __init__(self, x, y):
71+
self.x = float(x)
72+
self.y = float(y)
73+
74+
def __iter__(self):
75+
return (i for i in (self.x, self.y))
76+
77+
def __repr__(self):
78+
return 'Vector({!r}, {!r})'.format(*self)
79+
80+
def __str__(self):
81+
return str(tuple(self))
82+
83+
def __bytes__(self):
84+
return bytes(array(Vector.typecode, self))
85+
86+
def __eq__(self, other):
87+
return tuple(self) == tuple(other)
88+
89+
def __abs__(self):
90+
return math.hypot(self.x, self.y)
91+
92+
def __bool__(self):
93+
return bool(abs(self))
94+
95+
def angle(self):
96+
return math.atan2(self.y, self.x)
97+
98+
def __format__(self, fmt_spec=''):
99+
if fmt_spec.endswith('p'):
100+
fmt_spec = fmt_spec[:-1]
101+
coords = (abs(self), self.angle())
102+
outer_fmt = '<{}, {}>'
103+
else:
104+
coords = self
105+
outer_fmt = '({}, {})'
106+
components = (format(c, fmt_spec) for c in coords)
107+
return outer_fmt.format(*components)
108+
109+
@classmethod
110+
def frombytes(cls, octets):
111+
arr = array(Vector.typecode)
112+
arr.frombytes(octets)
113+
return cls(*arr)

0 commit comments

Comments
 (0)