Class: Diameter
The Diameter class is a subclass of Variable designed to represent the length of a diameter. It ensures accurate calculations by storing all values internally in its base SI unit, meters (m).
Supported Units
The following units are supported for initialization and conversion.
| Unit | str |
Conversion Factor to Meters (m) |
|---|---|---|
| meters | m | 1 |
| centimeters | cm | 0.01 |
| millimeters | mm | 0.001 |
| inches | in | 0.0254 |
| feet | ft | 0.3048 |
Class Reference
class Diameter(value, units='m')
A class for handling diameter measurements with automatic unit conversion.
Parameters:
value:floatorint
The numeric value of the diameter. Must be a non-negative number.units:str, default='m'
The unit of the provided value. Must be one of the supported units.
Raises:
ValueError: Ifvalueis negative.ypeError: Ifunitsis not a valid unit.
Examples:
# Create a Diameter object of 10 meters
>>> d1 = Diameter(10)
# Create a Diameter object of 12 inches
>>> d2 = Diameter(12, "in")
Properties
| Property | Type | Description |
|---|---|---|
.value |
float |
The numeric value of the diameter, always in meters (m). This is the internal representation used for all calculations. |
.original_value |
float |
The numeric value as provided during initialization. |
.original_unit |
str |
The unit as provided during initialization. |
Methods
to(target_unit)
Returns a new Diameter object converted to the target_unit. The original object remains unchanged.
Parameters:
target_unit:str
The unit to convert to. Must be one of the supported units.
Returns:
Diameter
A newDiameterobject with the samevalue, represented in the target unit.
Raises:
TypeError: Iftarget_unitis not a valid unit.
Examples:
# Initialize a diameter of 5 meters
>>> pipe_diameter_m = Diameter(5)
# Convert to feet
>>> pipe_diameter_ft = pipe_diameter_m.to("ft")
>>> print(pipe_diameter_ft)
16.4042 ft
String Representation
__str__(self)
Returns a human-readable string representation of the diameter, rounded to six decimal places, using its original value and unit.__repr__(self)
Returns an unambiguous string representation of the object, suitable for debugging.