pythonpython-collections

How to use proper container in python


For the following code I would like to use a structure in order to not have to give 2 values for each element min and max and also I would like to have a container if exists to can give one value and the other one to remain None. For instance power to have only min element. So for power to have a container with 2 elements (for min and max) and same for temperature. How is that possible in python ? please help, thanks!

def result_final(
    power_min,
    power_max,
    temperature_min,
    temperature_max
) -> str:
    def _result_min(value) -> str:
        return "<min>" "<value>" + str(value) + "</value>" + "</min>"

    def _result_max(value) -> str:
        return "<max>" "<value>" + str(value) + "</value>" +" </max>"

    def _measure_result(unit_id, min_value, max_value) -> str:
        return (
            "<measure_result>"
            "<unit-id>" + str(unit_id) + "</unit-id>"
            "" + _result_min(min_value) + ""
            "" + _result_max(max_value) + ""
            "</measure_result>"
        )

    def _stats(object, min_value, max_value) -> str:
        return (
            "<stats>"
            "<object>" + object + "</object>"
            "" + _measure_result(0, min_value, max_value) + ""
            "" + _measure_result(1, min_value, max_value) + ""
            "</stats>"
        )

    content = (
        '<result-stats>'
        "" + _stats("POWER", power_min, power_max) + ""
         "" + _stats("TEMPERATURE", temperature_min, temperature_max) + ""
        "</result-stats>"
    )
    return content

x = result_final(power_min = 12, power_max = 125, temperature_min = 12, temperature_max = 12)
print(x)

Solution

  • I'd suggest just using tuples for each min/max pair:

    from typing import Optional, Tuple
    
    Stats = Tuple[Optional[int], Optional[int]]  # min, max
    
    
    def result_final(power: Stats, temperature: Stats) -> str:
        def _result_min(value: Optional[int]) -> str:
            return "" if value is None else f"<min><value>{value}</value></min>"
    
        def _result_max(value: Optional[int]) -> str:
            return "" if value is None else f"<max><value>{value}</value></max>"
    
        def _measure_result(unit_id: int, value: Stats) -> str:
            min_value, max_value = value
            return (
                "<measure_result>"
                f"<unit-id>{unit_id}</unit-id>"
                f"{_result_min(min_value)}"
                f"{_result_max(max_value)}"
                "</measure_result>"
            )
    
        def _stats(obj: str, value: Stats) -> str:
            return (
                "<stats>"
                f"<object>{object}</object>"
                f"{_measure_result(0, value)}"
                f"{_measure_result(1, value)}"
                "</stats>"
            )
    
        return (
            "<result-stats>"
            f"{_stats('POWER', power)}"
            f"{_stats('TEMPERATURE', temperature)}"
            "</result-stats>"
        )
    
    
    print(result_final((12, 125), (12, 12)))