def normalize(self, bounds, units, inplace=False): """ Normalize the angle to be within the bounds specified. If inplace==True, this replace the internal values, otherwise it returns a new Angle object. Parameters ---------- bounds : tuple A tuple with 2 values (low, hi) where this is the range you would like to normalize the angle to. For example, if you have an angle value of 752.1834 but you want it to be within the range -180 -> +180, you can specifiy `Angle.normalize((-180, 180), units="degrees")`. """ # Validate the units lowUnits = units.lower() convert.parseDegrees(bounds[1]) if lowUnits == "degrees": radianBounds = (math.radians(convert.parseDegrees(bounds[0])), math.radians(convert.parseDegrees(bounds[1]))) elif lowUnits == "radians": radianBounds = (float(bounds[0]), float(bounds[1])) elif lowUnits == "hours": radianBounds = (convert.hoursToRadians(convert.parseHours(bounds[0])), convert.hoursToRadians(convert.parseHours(bounds[1]))) if inplace: obj = self else: obj = copy.copy(self) if obj.radians < radianBounds[0]: obj.radians = obj.radians % radianBounds[1] elif obj.radians >= radianBounds[1]: if radianBounds[0] == 0: obj.radians = obj.radians % radianBounds[1] else: obj.radians = obj.radians % radianBounds[0] return obj
def __init__(self, angle, units): # Make the `units` string lower case, and validate the `units` lowUnits = units.lower() try: if lowUnits == "degrees": self.radians = math.radians(convert.parseDegrees(angle)) elif lowUnits == "radians": self.radians = float(angle) elif lowUnits == "hours": # can accept string or float in any valid format self.radians = convert.hoursToRadians(convert.parseHours(angle)) else: raise IllegalUnitsError(units) except ValueError: raise ValueError("{1}: the angle value given couldn't be parsed (was of type {0})".format(type(angle).__name__, type(self).__name__))