def _get_radius(r, d): """Given either a radius or a diameter, determine the radius. Either r or d, but not both, must be given (non-None). Otherwise, an exception will be raised. The given value is also converted to a number. """ if both(r, d): raise TypeError("radius and diameter cannot be specified together") elif r is not None: return number.convert(r, "radius") elif d is not None: return number.convert(d, "diameter") / 2 else: raise TypeError("radius or diameter must be specified")
def test_both(self): # Both non-None, even if they are falsey self.assertTrue(util.both(1, 1)) self.assertTrue(util.both(0, 0)) self.assertTrue(util.both(False, False)) # One None self.assertFalse(util.both(None, 1)) self.assertFalse(util.both(1, None)) # Both None self.assertFalse(util.both(None, None))
def _get_radii(r, d): """Like _get_radius, but the given value for r or d must either be a tuple or a list and must have exactly 2 elements. """ if both(r, d): raise TypeError("radii and diameters cannot be specified together") elif r is not None: if not isinstance(r, (tuple, list)): raise TypeError("r must be a tuple or a list") elif len(r) != 2: raise ValueError("r must have two values") else: return tuple(r) elif d is not None: if not isinstance(d, (tuple, list)): raise TypeError("d must be a tuple or a list") elif len(d) != 2: raise ValueError("d must have two values") else: return tuple(x / 2 for x in d) else: raise TypeError("radii or diameters must be specified")
def cuboid(size_or_x_or_xyz, y=None, z=None): """Generate a cuboid. The cuboid will have one corner at the origin, will be aligned with the axes, and extend in the positive axis directions. Signatures (convenience forms only): * cuboid(size) * cuboid(x, y, z) * cuboid(xyz) xzy can be Vector, list, or tuple. Note that for convenience, a Vector can be used for xyz even though xyz is not strictly a vector. """ if both(y, z): return Cuboid(size_or_x_or_xyz, y, z) elif neither(y, z) and number.valid(size_or_x_or_xyz): return Cuboid(size_or_x_or_xyz, size_or_x_or_xyz, size_or_x_or_xyz) elif neither(y, z) and Vector.valid_type(size_or_x_or_xyz): return Cuboid(*size_or_x_or_xyz) else: raise TypeError("y and z can only be specified together")