import subprocess
from collections import namedtuple

GPUUsageInfo = namedtuple('GPUUsageInfo', ['total_mem', 'avail_mem', 'used_mem',
                                           'temp', 'percent_fan',
                                           'usage_gpu', 'usage_mem'])


def query_nvidia_smi(gpu_number) -> GPUUsageInfo:
    """
    :return:
        all memory fields are in megabytes,
        temperature in degrees celsius,
        fan speed is integer percent from 0 to 100 inclusive,
        usage_gpu and usage_mem are integer percents from 0 to 100 inclusive
        (usage_mem != used_mem, usage_mem is about read/write access load)
        read more in 'nvidia-smi --help-query-gpu'.

        Any field can be None if such information is not supported by nvidia-smi for current GPU

        Returns None if call failed (no nvidia-smi or query format was changed)

        Raises exception with readable comment
    """
    params = ["memory.total", "memory.free", "memory.used",
              "temperature.gpu", "fan.speed",
              "utilization.gpu", "utilization.memory"]
    try:
        output = subprocess.check_output(["nvidia-smi",
                                          "--query-gpu={}".format(','.join(params)),
                                          "--format=csv,noheader,nounits"])
    except FileNotFoundError:
        raise Exception("No nvidia-smi")
    except subprocess.CalledProcessError:
        raise Exception("nvidia-smi call failed")

    output = output.decode('utf-8').split("\n")[gpu_number].strip()
    values = output.split(", ")

    # If value contains 'not' - it is not supported for this GPU (in fact, for now nvidia-smi returns '[Not Supported]')
    values = [None if ("not" in value.lower()) else int(value) for value in values]

    return GPUUsageInfo(*values)

@@ -45,16 +45,44 @@
 medtuple
+%0Afrom typing import Optional
 %0A%0AGPUUsa
@@ -294,16 +294,354 @@
 em'%5D)%0A%0A%0A
+def _convert_nvidia_smi_value(value) -%3E Optional%5Bint%5D:%0A    value = value.lower()%0A    # If value contains 'not' or 'N/A' - it is not supported for this GPU%0A    # (in fact, for now nvidia-smi returns '%5BNot Supported%5D' or '%5BN/A%5D' depending of its version)%0A    if %22not%22 in value or %22n/a%22 in value:%0A        return None%0A    return int(value)%0A%0A%0A
 def quer
@@ -1948,180 +1948,43 @@
     
-# If value contains 'not' - it is not supported for this GPU (in fact, for now 
+values = %5B_convert_
 nvidia
--smi returns '%5BNot Supported%5D')%0A    values = %5BNone if (%22not%22 in value.lower()) else int
+_smi_value
 (val

fix nvidia-smi parsing for new nvidia-smi that uses (N/A) when unsupported (#776)