Gestion des quantités
# `Quantity` Class: Detailed Method Documentation
This document provides a detailed explanation of each method of the `Quantity` class, designed to handle physical quantities with units.
## Introduction
The `Quantity` class is a wrapper around the Pint library, allowing manipulation of values associated with physical units. It facilitates unit conversion, display, serialization, and arithmetic operations.
## Constructor
### Description
The constructor initializes a new `Quantity` instance with a numeric value and a unit.
- **Parameters**:
- `value`: Numeric value of the quantity.
- `unit`: String representing the unit (e.g., "m/s", "Pa", "kg").
## Main Methods
### `set_quantity`
#### Description
Updates the value and unit of the quantity.
- **Parameters**:
- `value`: New numeric value.
- `unit`: New unit.
### `get_in`
#### Description
Converts the quantity to a target unit and returns its numeric value.
- **Parameters**:
- `target_unit`: Unit to convert to.
- **Returns**: Numeric value in the target unit.
- **Exceptions**: Raises an error if conversion is not possible.
### `to`
#### Description
Converts the quantity to a target unit and returns a new `Quantity` object.
- **Parameters**:
- `target_unit`: Unit to convert to.
- **Returns**: A new `Quantity` object with the converted value.
- **Exceptions**: Raises an error if conversion is not possible.
## Serialization
### `as_dict`
#### Description
Returns a dictionary representation of the quantity.
- **Returns**: A dictionary with keys `value` and `unit`.
- **Example Output**: `{"value": 10.0, "unit": "meter / second"}`
### `from_dict`
#### Description
Creates a `Quantity` object from a dictionary.
- **Parameters**:
- `data`: Dictionary containing `value` and `unit`.
- **Returns**: A new `Quantity` object.
### `from_pint`
#### Description
Creates a `Quantity` object from a Pint Quantity.
- **Parameters**:
- `pint_quantity`: Pint Quantity object.
- **Returns**: A new `Quantity` object.
## Representation
### `__repr__` and `__str__`
#### Description
- **`__repr__`**: Returns a formal representation of the object, useful for debugging.
- **`__str__`**: Returns a human-readable representation of the object.
## Arithmetic Operations
### Description
The class supports `+`, `-`, `*`, `/` operations with other `Quantity` objects or scalars.
- **Addition (`__add__`)**: Adds two quantities or a quantity and a scalar.
- **Subtraction (`__sub__`)**: Subtracts two quantities or a quantity and a scalar.
- **Multiplication (`__mul__`)**: Multiplies two quantities or a quantity and a scalar.
- **Division (`__truediv__`)**: Divides two quantities or a quantity and a scalar.
- **Returns**: A new `Quantity` object representing the result of the operation.
## Usage Examples
1. **Creating objects**:
- `speed = Quantity(10, "m/s")`
- `pressure = Quantity(101325, "Pa")`
2. **Conversion**:
- `speed.get_in("km/h")`
- `pressure.get_in("atm")`
3. **Arithmetic**:
- `d1 + d2`
- `force / area`
4. **Serialization**:
- `data = {"speed": speed.as_dict(), "pressure": pressure.as_dict()}`
- `json_str = json.dumps(data, indent=2)`
5. **Deserialization**:
- `speed_loaded = Quantity.from_dict(loaded_data["speed"])`
## Conclusion
The `Quantity` class simplifies handling physical quantities with units in Python, leveraging Pint for conversions and adding serialization and arithmetic capabilities.