1
0

fix: fix human readable value display

This commit is contained in:
2026-06-17 20:25:02 +08:00
parent d42885f1ab
commit 181c483923
3 changed files with 67 additions and 21 deletions

View File

@@ -143,8 +143,8 @@ class App:
tolerance = self.__accept_device_value_tolerance(target_value) tolerance = self.__accept_device_value_tolerance(target_value)
print("How to sort result?") print("How to sort result?")
print("l: less component")
print("a: more accuracy") print("a: more accuracy")
print("l: less component")
response_priority = self.__accept_command( response_priority = self.__accept_command(
App.QuerySortPriority App.QuerySortPriority
).to_response_priority() ).to_response_priority()

View File

@@ -1,3 +1,4 @@
import enum
from typing import Iterable from typing import Iterable
from pathlib import Path from pathlib import Path
from .common import LcrConnException from .common import LcrConnException
@@ -157,6 +158,47 @@ def from_human_readable_value(strl: str) -> float:
return float(strl) return float(strl)
class UnitScale(enum.IntEnum):
"""
The unit scale for human readable value
"""
NANO_LOWER = enum.auto()
NANO = enum.auto()
MICRO = enum.auto()
MILLI = enum.auto()
NONE = enum.auto()
KILO = enum.auto()
MEGA = enum.auto()
GIGA = enum.auto()
GIGA_HIGHER = enum.auto()
def get_human_readable_value_scale(v: float) -> UnitScale:
"""
Get the unit scale of human readable value
:param v: The value
:return: The unit scale
"""
v = abs(v)
if v < 1e-12:
return UnitScale.NANO_LOWER
if v < 1e-9:
return UnitScale.NANO
if v < 1e-6:
return UnitScale.MICRO
if v < 1e-3:
return UnitScale.MILLI
if v < 1e3:
return UnitScale.NONE
if v < 1e6:
return UnitScale.KILO
if v < 1e9:
return UnitScale.MEGA
return UnitScale.GIGA_HIGHER
def to_human_readable_value(v: float) -> str: def to_human_readable_value(v: float) -> str:
""" """
Convert float value to human readable value Convert float value to human readable value
@@ -164,21 +206,23 @@ def to_human_readable_value(v: float) -> str:
:param value: The float value :param value: The float value
:return: The human readable value :return: The human readable value
""" """
if v / 1e-12 < 1e3: scale = get_human_readable_value_scale(v)
return "{:e} n".format(v / 1e-12) match scale:
if v / 1e-9 < 1e3: case UnitScale.NANO_LOWER:
return "{:.4f} p".format(v / 1e-9) return "{:+.4e} n".format(v / 1e-12)
if v / 1e-6 < 1e3: case UnitScale.NANO:
return "{:.4f} u".format(v / 1e-6) return "{:+.4f} p".format(v / 1e-9)
if v / 1e-3 < 1e3: case UnitScale.MICRO:
return "{:.4f} m".format(v / 1e-3) return "{:+.4f} u".format(v / 1e-6)
if v < 1e3: case UnitScale.MILLI:
return "{:.4f}".format(v) return "{:+.4f} m".format(v / 1e-3)
if v / 1e3 < 1e3: case UnitScale.NONE:
return "{:.4f} k".format(v / 1e3) return "{:+.4f}".format(v)
if v / 1e6 < 1e3: case UnitScale.KILO:
return "{:.4f} M".format(v / 1e6) return "{:+.4f} k".format(v / 1e3)
if v / 1e9 < 1e3: case UnitScale.MEGA:
return "{:.4f} G".format(v / 1e9) return "{:+.4f} M".format(v / 1e6)
case UnitScale.GIGA:
return "{:e}".format(v) return "{:+.4f} G".format(v / 1e9)
case UnitScale.GIGA_HIGHER:
return "{:+.4e} G".format(v / 1e9)

View File

@@ -234,9 +234,11 @@ class Response:
# Sort by different strategy # Sort by different strategy
match request.response_priority: match request.response_priority:
case ResponsePriority.LESS_DEVICES: case ResponsePriority.LESS_DEVICES:
self.__sorted_items.sort(key=lambda x: (x.device_count, x.difference)) self.__sorted_items.sort(
key=lambda x: (x.device_count, x.unsigned_difference)
)
case ResponsePriority.MORE_ACCURACY: case ResponsePriority.MORE_ACCURACY:
self.__sorted_items.sort(key=lambda x: x.difference) self.__sorted_items.sort(key=lambda x: x.unsigned_difference)
# Cut item by limit # Cut item by limit
self.__sorted_items = self.__sorted_items[: request.count_limit] self.__sorted_items = self.__sorted_items[: request.count_limit]