updated contents from Atlas repo

This commit is contained in:
Luciano Ramalho
2014-10-14 14:26:55 -03:00
parent 40688c038d
commit 981d5bc473
157 changed files with 71134 additions and 1 deletions

24
classes/mem_test.py Normal file
View File

@@ -0,0 +1,24 @@
import importlib
import sys
import resource
NUM_VECTORS = 10**7
if len(sys.argv) == 2:
module_name = sys.argv[1].replace('.py', '')
module = importlib.import_module(module_name)
else:
print('Usage: {} <vector-module-to-test>'.format())
sys.exit(1)
fmt = 'Selected Vector type: {.__name__}.{.__name__}'
print(fmt.format(module, module.Vector))
mem_init = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
print('Creating {:,} Vector instances'.format(NUM_VECTORS))
vectors = [module.Vector(3.0, 4.0) for i in range(NUM_VECTORS)]
mem_final = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
print('Initial RAM usage: {:14,}'.format(mem_init))
print(' Final RAM usage: {:14,}'.format(mem_final))

60
classes/vector_v0.py Normal file
View File

@@ -0,0 +1,60 @@
"""
A 2-dimensional vector class
# BEGIN VECTOR_V0_DEMO
>>> v1 = Vector(3, 4)
>>> x, y = v1 #<1>
>>> x, y
(3.0, 4.0)
>>> v1 #<2>
Vector(3.0, 4.0)
>>> v1_clone = eval(repr(v1)) #<3>
>>> v1 == v1_clone
True
>>> print(v1) #<4>
(3.0, 4.0)
>>> octets = bytes(v1) #<5>
>>> octets
b'\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@'
>>> abs(v1) #<6>
5.0
>>> bool(v1), bool(Vector(0, 0)) #<7>
(True, False)
# END VECTOR_V0_DEMO
"""
# BEGIN VECTOR_V0
from array import array
import math
class Vector:
typecode = 'd' # <1>
def __init__(self, x, y):
self.x = float(x) # <2>
self.y = float(y)
def __iter__(self):
return (i for i in (self.x, self.y)) # <3>
def __repr__(self):
return 'Vector({!r}, {!r})'.format(*self) # <4>
def __str__(self):
return str(tuple(self)) # <5>
def __bytes__(self):
return bytes(array(Vector.typecode, self)) # <6>
def __eq__(self, other):
return tuple(self) == tuple(other) # <7>
def __abs__(self):
return math.hypot(self.x, self.y)
def __bool__(self):
return bool(abs(self)) # <8>
# END VECTOR_V0

86
classes/vector_v1.py Normal file
View File

@@ -0,0 +1,86 @@
"""
A 2-dimensional vector class
>>> v1 = Vector(3, 4)
>>> x, y = v1 #<1>
>>> x, y
(3.0, 4.0)
>>> v1 #<2>
Vector(3.0, 4.0)
>>> v1_clone = eval(repr(v1)) #<3>
>>> v1 == v1_clone
True
>>> print(v1) #<4>
(3.0, 4.0)
>>> octets = bytes(v1) #<5>
>>> octets
b'\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@'
>>> abs(v1) #<6>
5.0
>>> bool(v1), bool(Vector(0, 0)) #<7>
(True, False)
Test of .frombytes() class method:
>>> v1_clone = Vector.frombytes(bytes(v1))
>>> v1_clone
Vector(3.0, 4.0)
>>> v1 == v1_clone
True
So far, Vector instances are unhashable:
# BEGIN VECTOR_V1_UNHASHABLE_DEMO
>>> v1 = Vector(3, 4)
>>> hash(v1)
Traceback (most recent call last):
...
TypeError: unhashable type: 'Vector'
>>> set([v1])
Traceback (most recent call last):
...
TypeError: unhashable type: 'Vector'
# END VECTOR_V1_UNHASHABLE_DEMO
"""
from array import array
import math
class Vector:
typecode = 'd'
def __init__(self, x, y):
self.x = float(x)
self.y = float(y)
def __iter__(self):
return (i for i in (self.x, self.y))
def __repr__(self):
return 'Vector({!r}, {!r})'.format(*self)
def __str__(self):
return str(tuple(self))
def __bytes__(self):
return bytes(array(Vector.typecode, self))
def __eq__(self, other):
return tuple(self) == tuple(other)
def __abs__(self):
return math.hypot(self.x, self.y)
def __bool__(self):
return bool(abs(self))
# BEGIN VECTOR_V1
@classmethod # <1>
def frombytes(cls, octets): # <2>
arr = array(Vector.typecode) # <3>
arr.frombytes(octets) # <4>
return cls(*arr) # <5>
# END VECTOR_V1

113
classes/vector_v2.py Normal file
View File

@@ -0,0 +1,113 @@
"""
A 2-dimensional vector class
>>> v1 = Vector(3, 4)
>>> x, y = v1
>>> x, y
(3.0, 4.0)
>>> v1
Vector(3.0, 4.0)
>>> v1_clone = eval(repr(v1))
>>> v1 == v1_clone
True
>>> print(v1)
(3.0, 4.0)
>>> octets = bytes(v1)
>>> octets
b'\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@'
>>> abs(v1)
5.0
>>> bool(v1), bool(Vector(0, 0))
(True, False)
Test of ``.frombytes()`` class method:
>>> v1_clone = Vector.frombytes(bytes(v1))
>>> v1_clone
Vector(3.0, 4.0)
>>> v1 == v1_clone
True
Tests of ``format()`` with rectangular coordinates:
>>> format(v1)
'(3.0, 4.0)'
>>> format(v1, '.2f')
'(3.00, 4.00)'
>>> format(v1, '.3e')
'(3.000e+00, 4.000e+00)'
Tests of the ``angle`` method::
>>> Vector(0, 0).angle()
0.0
>>> Vector(1, 0).angle()
0.0
>>> epsilon = 10**-8
>>> abs(Vector(0, 1).angle() - math.pi/2) < epsilon
True
>>> abs(Vector(1, 1).angle() - math.pi/4) < epsilon
True
Tests of ``format()`` with polar coordinates:
>>> format(Vector(1, 1), 'p') # doctest:+ELLIPSIS
'<1.414213..., 0.785398...>'
>>> format(Vector(1, 1), '.3ep')
'<1.414e+00, 7.854e-01>'
>>> format(Vector(1, 1), '0.5fp')
'<1.41421, 0.78540>'
"""
from array import array
import math
class Vector:
typecode = 'd'
def __init__(self, x, y):
self.x = float(x)
self.y = float(y)
def __iter__(self):
return (i for i in (self.x, self.y))
def __repr__(self):
return 'Vector({!r}, {!r})'.format(*self)
def __str__(self):
return str(tuple(self))
def __bytes__(self):
return bytes(array(Vector.typecode, self))
def __eq__(self, other):
return tuple(self) == tuple(other)
def __abs__(self):
return math.hypot(self.x, self.y)
def __bool__(self):
return bool(abs(self))
def angle(self):
return math.atan2(self.y, self.x)
def __format__(self, fmt_spec=''):
if fmt_spec.endswith('p'):
fmt_spec = fmt_spec[:-1]
coords = (abs(self), self.angle())
outer_fmt = '<{}, {}>'
else:
coords = self
outer_fmt = '({}, {})'
components = (format(c, fmt_spec) for c in coords)
return outer_fmt.format(*components)
@classmethod
def frombytes(cls, octets):
arr = array(Vector.typecode)
arr.frombytes(octets)
return cls(*arr)

View File

@@ -0,0 +1,115 @@
"""
A 2-dimensional vector class
>>> v1 = Vector(3, 4)
>>> x, y = v1
>>> x, y
(3.0, 4.0)
>>> v1
Vector(3.0, 4.0)
>>> v1_clone = eval(repr(v1))
>>> v1 == v1_clone
True
>>> print(v1)
(3.0, 4.0)
>>> octets = bytes(v1)
>>> octets
b'\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@'
>>> abs(v1)
5.0
>>> bool(v1), bool(Vector(0, 0))
(True, False)
Test of ``.frombytes()`` class method:
>>> v1_clone = Vector.frombytes(bytes(v1))
>>> v1_clone
Vector(3.0, 4.0)
>>> v1 == v1_clone
True
Tests of ``format()`` with rectangular coordinates:
>>> format(v1)
'(3.0, 4.0)'
>>> format(v1, '.2f')
'(3.00, 4.00)'
>>> format(v1, '.3e')
'(3.000e+00, 4.000e+00)'
Tests of the ``angle`` method::
>>> Vector(0, 0).angle()
0.0
>>> Vector(1, 0).angle()
0.0
>>> epsilon = 10**-8
>>> abs(Vector(0, 1).angle() - math.pi/2) < epsilon
True
>>> abs(Vector(1, 1).angle() - math.pi/4) < epsilon
True
Tests of ``format()`` with polar coordinates:
>>> format(Vector(1, 1), 'p') # doctest:+ELLIPSIS
'<1.414213..., 0.785398...>'
>>> format(Vector(1, 1), '.3ep')
'<1.414e+00, 7.854e-01>'
>>> format(Vector(1, 1), '0.5fp')
'<1.41421, 0.78540>'
"""
from array import array
import math
class Vector:
typecode = 'd'
def __init__(self, x, y):
self.x = float(x)
self.y = float(y)
def __iter__(self):
return (i for i in (self.x, self.y))
def __repr__(self):
return 'Vector({!r}, {!r})'.format(*self)
def __str__(self):
return str(tuple(self))
def __bytes__(self):
return bytes(array(Vector.typecode, self))
def __eq__(self, other):
return tuple(self) == tuple(other)
def __abs__(self):
return math.hypot(self.x, self.y)
def __bool__(self):
return bool(abs(self))
def angle(self):
return math.atan2(self.y, self.x)
# BEGIN VECTOR_V2_FORMAT
def __format__(self, fmt_spec=''):
if fmt_spec.endswith('p'): # <1>
fmt_spec = fmt_spec[:-1] # <2>
coords = (abs(self), self.angle()) # <3>
outer_fmt = '<{}, {}>' # <4>
else:
coords = self # <5>
outer_fmt = '({}, {})' # <6>
components = (format(c, fmt_spec) for c in coords) # <7>
return outer_fmt.format(*components) # <8>
# END VECTOR_V2_FORMAT
@classmethod
def frombytes(cls, octets):
arr = array(Vector.typecode)
arr.frombytes(octets)
return cls(*arr)

123
classes/vector_v3.py Normal file
View File

@@ -0,0 +1,123 @@
"""
A 2-dimensional vector class
>>> v1 = Vector(3, 4)
>>> x, y = v1 #<1>
>>> x, y
(3.0, 4.0)
>>> v1 #<2>
Vector(3.0, 4.0)
>>> v1_clone = eval(repr(v1)) #<3>
>>> v1 == v1_clone
True
>>> print(v1) #<4>
(3.0, 4.0)
>>> octets = bytes(v1) #<5>
>>> octets
b'\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@'
>>> abs(v1) #<6>
5.0
>>> bool(v1), bool(Vector(0, 0)) #<7>
(True, False)
Test of .frombytes() class method:
>>> v1_clone = Vector.frombytes(bytes(v1))
>>> v1_clone
Vector(3.0, 4.0)
>>> v1 == v1_clone
True
# BEGIN VECTOR_V3_DEMO
Test of `x` and `y` read-only properties:
>>> v1.x, v1.y
(3.0, 4.0)
>>> v1.x = 123
Traceback (most recent call last):
...
AttributeError: can't set attribute
# END VECTOR_V3_HASH_DEMO
# BEGIN VECTOR_V3_HASH_DEMO
>>> v1 = Vector(3, 4)
>>> v2 = Vector(3.1, 4.2)
>>> hash(v1), hash(v2)
(7, 384307168202284039)
>>> set([v1, v2])
{Vector(3.1, 4.2), Vector(3.0, 4.0)}
# END VECTOR_V3_DEMO
"""
from array import array
import math
# BEGIN VECTOR_V3
class Vector:
typecode = 'd'
def __init__(self, x, y):
self.__x = float(x) # <1>
self.__y = float(y)
@property # <2>
def x(self): # <3>
return self.__x # <4>
@property # <5>
def y(self):
return self.__y
def __iter__(self):
return (i for i in (self.x, self.y)) # <6>
# remaining methods follow (omitted in book listing)
# END VECTOR_V3
def __repr__(self):
return 'Vector({!r}, {!r})'.format(*self)
def __str__(self):
return str(tuple(self))
def __bytes__(self):
return bytes(array(Vector.typecode, self))
def __eq__(self, other):
return tuple(self) == tuple(other)
# BEGIN VECTOR_V3_HASH
def __hash__(self):
return hash(self.x) ^ hash(self.y)
# END VECTOR_V3_HASH
def __abs__(self):
return math.hypot(self.x, self.y)
def __bool__(self):
return bool(abs(self))
def angle(self):
return math.atan2(self.y, self.x)
def __format__(self, fmt_spec=''):
if fmt_spec.endswith('p'):
fmt_spec = fmt_spec[:-1]
coords = (abs(self), self.angle())
outer_fmt = '<{}, {}>'
else:
coords = self
outer_fmt = '({}, {})'
components = (format(c, fmt_spec) for c in coords)
return outer_fmt.format(*components)
@classmethod
def frombytes(cls, octets):
arr = array(Vector.typecode)
arr.frombytes(octets)
return cls(*arr)

125
classes/vector_v3_slots.py Normal file
View File

@@ -0,0 +1,125 @@
"""
A 2-dimensional vector class
>>> v1 = Vector(3, 4)
>>> x, y = v1 #<1>
>>> x, y
(3.0, 4.0)
>>> v1 #<2>
Vector(3.0, 4.0)
>>> v1_clone = eval(repr(v1)) #<3>
>>> v1 == v1_clone
True
>>> print(v1) #<4>
(3.0, 4.0)
>>> octets = bytes(v1) #<5>
>>> octets
b'\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@'
>>> abs(v1) #<6>
5.0
>>> bool(v1), bool(Vector(0, 0)) #<7>
(True, False)
Test of .frombytes() class method:
>>> v1_clone = Vector.frombytes(bytes(v1))
>>> v1_clone
Vector(3.0, 4.0)
>>> v1 == v1_clone
True
# BEGIN VECTOR_V3_DEMO
Test of `x` and `y` read-only properties:
>>> v1.x, v1.y
(3.0, 4.0)
>>> v1.x = 123
Traceback (most recent call last):
...
AttributeError: can't set attribute
# END VECTOR_V3_HASH_DEMO
# BEGIN VECTOR_V3_HASH_DEMO
>>> v1 = Vector(3, 4)
>>> v2 = Vector(3.1, 4.2)
>>> hash(v1), hash(v2)
(7, 384307168202284039)
>>> set([v1, v2])
{Vector(3.1, 4.2), Vector(3.0, 4.0)}
# END VECTOR_V3_DEMO
"""
from array import array
import math
# BEGIN VECTOR_V3_SLOTS
class Vector:
__slots__ = ('__x', '__y')
typecode = 'd'
# methods follow (omitted in book listing)
# END VECTOR_V3_SLOTS
def __init__(self, x, y):
self.__x = float(x)
self.__y = float(y)
@property
def x(self):
return self.__x
@property
def y(self):
return self.__y
def __iter__(self):
return (i for i in (self.x, self.y)) # <6>
def __repr__(self):
return 'Vector({!r}, {!r})'.format(*self)
def __str__(self):
return str(tuple(self))
def __bytes__(self):
return bytes(array(Vector.typecode, self))
def __eq__(self, other):
return tuple(self) == tuple(other)
# BEGIN VECTOR_V3_HASH
def __hash__(self):
return hash(self.x) ^ hash(self.y)
# END VECTOR_V3_HASH
def __abs__(self):
return math.hypot(self.x, self.y)
def __bool__(self):
return bool(abs(self))
def angle(self):
return math.atan2(self.y, self.x)
def __format__(self, fmt_spec=''):
if fmt_spec.endswith('p'):
fmt_spec = fmt_spec[:-1]
coords = (abs(self), self.angle())
outer_fmt = '<{}, {}>'
else:
coords = self
outer_fmt = '({}, {})'
components = (format(c, fmt_spec) for c in coords)
return outer_fmt.format(*components)
@classmethod
def frombytes(cls, octets):
arr = array(Vector.typecode)
arr.frombytes(octets)
return cls(*arr)