fix: fix human readable value display
This commit is contained in:
@@ -143,8 +143,8 @@ class App:
|
||||
tolerance = self.__accept_device_value_tolerance(target_value)
|
||||
|
||||
print("How to sort result?")
|
||||
print("l: less component")
|
||||
print("a: more accuracy")
|
||||
print("l: less component")
|
||||
response_priority = self.__accept_command(
|
||||
App.QuerySortPriority
|
||||
).to_response_priority()
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import enum
|
||||
from typing import Iterable
|
||||
from pathlib import Path
|
||||
from .common import LcrConnException
|
||||
@@ -157,6 +158,47 @@ def from_human_readable_value(strl: str) -> float:
|
||||
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:
|
||||
"""
|
||||
Convert float value to human readable value
|
||||
@@ -164,21 +206,23 @@ def to_human_readable_value(v: float) -> str:
|
||||
:param value: The float value
|
||||
:return: The human readable value
|
||||
"""
|
||||
if v / 1e-12 < 1e3:
|
||||
return "{:e} n".format(v / 1e-12)
|
||||
if v / 1e-9 < 1e3:
|
||||
return "{:.4f} p".format(v / 1e-9)
|
||||
if v / 1e-6 < 1e3:
|
||||
return "{:.4f} u".format(v / 1e-6)
|
||||
if v / 1e-3 < 1e3:
|
||||
return "{:.4f} m".format(v / 1e-3)
|
||||
if v < 1e3:
|
||||
return "{:.4f}".format(v)
|
||||
if v / 1e3 < 1e3:
|
||||
return "{:.4f} k".format(v / 1e3)
|
||||
if v / 1e6 < 1e3:
|
||||
return "{:.4f} M".format(v / 1e6)
|
||||
if v / 1e9 < 1e3:
|
||||
return "{:.4f} G".format(v / 1e9)
|
||||
|
||||
return "{:e}".format(v)
|
||||
scale = get_human_readable_value_scale(v)
|
||||
match scale:
|
||||
case UnitScale.NANO_LOWER:
|
||||
return "{:+.4e} n".format(v / 1e-12)
|
||||
case UnitScale.NANO:
|
||||
return "{:+.4f} p".format(v / 1e-9)
|
||||
case UnitScale.MICRO:
|
||||
return "{:+.4f} u".format(v / 1e-6)
|
||||
case UnitScale.MILLI:
|
||||
return "{:+.4f} m".format(v / 1e-3)
|
||||
case UnitScale.NONE:
|
||||
return "{:+.4f}".format(v)
|
||||
case UnitScale.KILO:
|
||||
return "{:+.4f} k".format(v / 1e3)
|
||||
case UnitScale.MEGA:
|
||||
return "{:+.4f} M".format(v / 1e6)
|
||||
case UnitScale.GIGA:
|
||||
return "{:+.4f} G".format(v / 1e9)
|
||||
case UnitScale.GIGA_HIGHER:
|
||||
return "{:+.4e} G".format(v / 1e9)
|
||||
|
||||
@@ -234,9 +234,11 @@ class Response:
|
||||
# Sort by different strategy
|
||||
match request.response_priority:
|
||||
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:
|
||||
self.__sorted_items.sort(key=lambda x: x.difference)
|
||||
self.__sorted_items.sort(key=lambda x: x.unsigned_difference)
|
||||
|
||||
# Cut item by limit
|
||||
self.__sorted_items = self.__sorted_items[: request.count_limit]
|
||||
|
||||
Reference in New Issue
Block a user