Permalink
Browse files

Add doctests and minor fixes to units and geometry

  • Loading branch information...
1 parent a7f7f51 commit e9772c2b9ebc0d8171811811e7fe53aced8f35f0 @kalgynirae kalgynirae committed Mar 18, 2012
Showing with 57 additions and 8 deletions.
  1. +21 −2 golfram/geometry.py
  2. +36 −6 golfram/units.py
View
@@ -5,7 +5,7 @@ class Rectangle:
__slots__ = ('height', 'nw', 'se', 'width')
- def __init__(self, width=None, height=None, offset=None)
+ def __init__(self, width=None, height=None, offset=None):
"""Create a Rectangle
offset is a Vector describing the position of the rectangle's top left
@@ -97,13 +97,32 @@ def __getitem__(self, key):
@property
def magnitude(self):
+ """Calculate the magnitude of the vector
+
+ >>> Vector(-12, 0).magnitude
+ 12.0
+ >>> Vector(3, 4).magnitude
+ 5.0
+
+ """
return math.sqrt(self.x ** 2 + self.y ** 2)
def normalize(self):
- """Return a unit vector in the direction of self"""
+ """Return a unit vector in the direction of the vector
+
+ >>> Vector(-12, 0).normalize()
+ Vector(-1.0, 0.0)
+ >>> Vector(2, -7).normalize().magnitude
+ 1.0
+ """
return self / self.magnitude
def project(self, other):
"""Calculates the vector projection onto other"""
joseph = other.normalize()
return self * joseph * joseph
+
+
+if __name__ == '__main__':
+ import doctest
+ doctest.testmod()
View
@@ -1,11 +1,34 @@
px = 1/187. # m
class Unit:
+ """Store conversion information for units of measure
+ First, define a few Unit instances:
+ >>> px = Unit('px', integral=True)
+ >>> m = Unit('m', px=187)
+ >>> m
+ Unit('m')
+ >>> 5*m
+ 5*m
+ >>> 5*m + 3
+ Traceback (most recent call last):
+ ...
+ AttributeError: 'int' object has no attribute 'unit_name'
+ >>> 5*m + 3*px # doctest:+ELLIPSIS
+ 5.0160...*m
+ >>> px(5*m + 3*px)
+ 937
+ >>> m(64*px) # doctest:+ELLIPSIS
+ 0.3422...
+ >>> m(5*m)
+ 5
+
+ """
__slots__ = ('conversions', 'value', 'unit_name')
- def __init__(self, name, **conversions):
+ def __init__(self, name, integral=False, **conversions):
self.unit_name = name
+ self.integral = integral
self.conversions = conversions
self.value = None
@@ -18,14 +41,17 @@ def __repr__(self):
def __call__(self, quantity):
"""Convert the quantity to this unit"""
if quantity.unit_name == self.unit_name:
- return quantity.value
+ result = quantity.value
elif quantity.unit_name in self.conversions:
- return quantity.value / self.conversions[quantity.unit_name]
+ result = quantity.value / float(self.conversions[quantity.unit_name])
elif self.unit_name in quantity.conversions:
- return quantity.value * quantity.conversions[self.unit_name]
+ result = quantity.value * quantity.conversions[self.unit_name]
else:
raise ValueError("No conversion info for {0} to {1}".format(
- quantity.unit_name, self.unit_name))
+ quantity.unit_name, self.unit_name))
+ if self.integral:
+ result = int(result)
+ return result
def __add__(self, other):
x = self.__copy__()
@@ -62,6 +88,10 @@ def __copy__(self):
x.value = self.value
return x
-px = Unit('px')
+px = Unit('px', integral=True)
m = Unit('m', px=187)
+
+if __name__ == '__main__':
+ import doctest
+ doctest.testmod()

0 comments on commit e9772c2

Please sign in to comment.