def __init__(self, color=None): if color is None: #FIXME: if it has no color it's white light? raise NotImplementedError, "white light is not yet implemented" elif isinstance(color, Color): self.color = color.color self.wavelength = color.wavelength elif isinstance(color, str): #first try treating it as a unit try: if color.replace(" ", "").isalnum(): #FIXME: what about "nm" (which should be interpreted as "1nm")? color = Unit(color) if color.compatible("nm"): #then it is, in fact, a wavelength self.wavelength = color self.color = color #return point else: raise UnitError, "Color.__init__: supplied unit was not compatible with wavelength (nm)" else: raise UnitError, "was not alpha numeric and was likely not a unit" #return point except UnitError, ue: #first check if it looks like a unit if ue.message.count("was not compatible") == 1: raise UnitError, "Color.__init__: incompatible units on %s" % (color) #was not a valid unit, maybe it's "green" #try: if color in self.colors.keys(): color = self.colors[color] self.wavelength = color self.color = color #return point else: raise ValueError, "Color.__init__: unable to parse color %s\n%s" % (color, ue) #return point
def instructions(self, tool): '''generate instructions for using this technique with a particular tool''' #technically you should be able to use more than one tool steps = Instructions() #it's essentially a list step1 = tool.align(self.interface1, self.interface2) #with the tool "tool", align interface1 and interface2 (with their vectors) steps.append(step1) step2 = tool.push(object=self.interface1, force=Unit("10 N"), vector=Vector(0,1,0)) #normal_to(self.interface2))) steps.append(step2) return steps
def __eq__(self, other): try: if other.wavelength == self.wavelength: return True return False except AttributeError, error: #maybe it's a color if other in self.colors: return (Color(other)==self) else: return Unit.__eq__(self, other)
def test_apple_init(self): green_apple = Apple("green") self.assertTrue(green_apple.name == "green") green_apple = Apple("530 nm") self.assertTrue(green_apple.name == "green") green_apple = Apple(Color("green")) self.assertTrue(green_apple.name == "green") green_apple = Apple(Color("530 nm")) self.assertTrue(green_apple.name == "green") green_apple = Apple(Color(Unit("530 nm"))) self.assertTrue(green_apple.name == "green") green_apple = Apple(Unit("530 nm")) self.assertTrue(green_apple.name == "green")
def test_color_init(self): #yay all of this passed on the first go :) green = Color("green") self.assertTrue(str(green) == "Color(\"green\")" or str(green)=="green") self.assertTrue(green.wavelength.compatible("m")) self.assertTrue(str(green.wavelength) == "530 nm") self.assertTrue(green.wavelength == "530 nm") self.assertTrue(green.wavelength == Unit("530 nm")) self.assertTrue(green == Color("green")) self.assertTrue(green == Color("530 nm")) self.assertTrue(green == Color(Unit("530 nm"))) self.assertTrue(green.compatible("m")) self.assertTrue(green.compatible("500 m")) #compatibility doesn't care about magnitude self.assertTrue(green.compatible(Unit("m"))) self.assertFalse(green.compatible(Unit("kg"))) self.assertTrue(green == "530 nm") self.assertTrue(green == "green") self.assertTrue(green == Unit("530 nm")) green = Color(Color("green")) self.assertTrue(green == Color("530 nm"))
class Color(Unit): '''will eventually implement CIE 1931 XYZ color space''' yaml_tag = "!color" colors = { "red": Unit("700 nm"), "orange": Unit("620 nm"), "yellow": Unit("580 nm"), "green": Unit("530 nm"), "blue": Unit("470 nm"), "violet": Unit("420 nm"), } def __init__(self, color=None): if color is None: #FIXME: if it has no color it's white light? raise NotImplementedError, "white light is not yet implemented" elif isinstance(color, Color): self.color = color.color self.wavelength = color.wavelength elif isinstance(color, str): #first try treating it as a unit try: if color.replace(" ", "").isalnum(): #FIXME: what about "nm" (which should be interpreted as "1nm")? color = Unit(color) if color.compatible("nm"): #then it is, in fact, a wavelength self.wavelength = color self.color = color #return point else: raise UnitError, "Color.__init__: supplied unit was not compatible with wavelength (nm)" else: raise UnitError, "was not alpha numeric and was likely not a unit" #return point except UnitError, ue: #first check if it looks like a unit if ue.message.count("was not compatible") == 1: raise UnitError, "Color.__init__: incompatible units on %s" % (color) #was not a valid unit, maybe it's "green" #try: if color in self.colors.keys(): color = self.colors[color] self.wavelength = color self.color = color #return point else: raise ValueError, "Color.__init__: unable to parse color %s\n%s" % (color, ue) #return point #except UnitError, ue: elif isinstance(color, Unit): if color.compatible("nm"): self.wavelength = color
def test_apple_eq(self): apple1 = Apple("530 nm") apple2 = Apple(Color(Unit("530 nm"))) self.assertTrue(apple1 == apple2)
#first check if it looks like a unit if ue.message.count("was not compatible") == 1: raise UnitError, "Color.__init__: incompatible units on %s" % (color) #was not a valid unit, maybe it's "green" #try: if color in self.colors.keys(): color = self.colors[color] self.wavelength = color self.color = color #return point else: raise ValueError, "Color.__init__: unable to parse color %s\n%s" % (color, ue) #return point #except UnitError, ue: elif isinstance(color, Unit): if color.compatible("nm"): self.wavelength = color else: raise NotImplementedError, bryan_message Unit.__init__(self, self.wavelength) def _get_name(self): #figure out the name of this color from self.wavelength items = self.colors.items() for item in items: if item[1] == self.wavelength: return item[0] #since we don't know a name, just give it return self.wavelength name = property(fget=_get_name, doc="figures out the name of the color") def __repr__(self): return "Color(\"%s\")" % (self.name) def __eq__(self, other): try: if other.wavelength == self.wavelength: return True return False