antupy.Var#
- class antupy.Var(_value: float | None | Var = None, _unit: str | Unit | None = None)[source]#
Class to represent parameters and variables in the system. It is used to store the values with their units. If you have a Var instance, you can obtain the value in different units with the gv([str]) method. In this way you make sure you are getting the value with the expected unit. “gv” internally converts unit if it is possible.
- Parameters:
value (float or None, optional) – The numerical value of the variable. If None, represents an undefined or uninitialized variable. Default is None.
_unit (str, Unit, or None, optional) – The unit of the variable. Can be a unit string (e.g., “kg”, “m/s2”), a Unit object, or None for dimensionless quantities. Default is None.
unit (Unit) – The Unit object representing the variable’s unit.
Examples
Creating variables with units:
>>> from antupy import Var >>> mass = Var(5.0, "kg") >>> velocity = Var(10, "m/s")
Arithmetic operations with automatic unit handling:
>>> v1 = Var(5.0, "kg") >>> v2 = Var(500, "g") >>> total_mass = v1 + v2 # Automatically converts g to kg >>> print(total_mass) 5.5 [kg]
Unit conversions:
>>> energy = Var(1000, "J") >>> energy_in_kj = energy.gv("kJ") >>> print(energy_in_kj) 1.0
Physical calculations:
>>> force = mass * Var(9.81, "m/s2") # F = ma >>> print(force) 49.05 [kg-m/s2]
Notes
The class is immutable (frozen dataclass) to ensure variable integrity
Operations between incompatible units raise TypeError
Supports comparison operators with automatic unit conversion
Can be converted to float/int for numerical operations
See also
ArrayFor handling arrays of values with the same unit
antupy.units.UnitThe underlying unit representation class
Methods
__init__([_value, _unit])compatible()Return a list of compatible units for the variable unit.
get_value([unit])Method to obtain the value of the variable in the requested unit.
gv([unit])Alias for self.get_value()
set_unit([unit])Set the primary unit of the variable.
su([unit])Alias of self.set_unit
Attributes
uProperty to obtain the label unit of the variable
vProperty to obtain the value of the variable in its label unit.
valueunit