kopia lustrzana https://github.com/corrscope/corrscope
Add unit suffix metadata to config classes
rodzic
73912f74fd
commit
3835421a09
|
@ -24,6 +24,8 @@ __all__ = [
|
|||
"copy_config",
|
||||
"DumpableAttrs",
|
||||
"KeywordAttrs",
|
||||
"with_units",
|
||||
"get_units",
|
||||
"Alias",
|
||||
"Ignored",
|
||||
"DumpEnumAsStr",
|
||||
|
@ -222,6 +224,18 @@ class KeywordAttrs(DumpableAttrs):
|
|||
super().__init_subclass__(kw_only=True, **kwargs)
|
||||
|
||||
|
||||
UNIT_SUFFIX = "suffix"
|
||||
|
||||
|
||||
def with_units(unit, **kwargs):
|
||||
metadata = {UNIT_SUFFIX: f" {unit}"}
|
||||
return attr.ib(metadata=metadata, **kwargs)
|
||||
|
||||
|
||||
def get_units(field: attr.Attribute) -> str:
|
||||
return field.metadata.get(UNIT_SUFFIX, "")
|
||||
|
||||
|
||||
@attr.dataclass
|
||||
class Alias:
|
||||
"""
|
||||
|
|
|
@ -12,7 +12,7 @@ import attr
|
|||
|
||||
from corrscope import outputs as outputs_
|
||||
from corrscope.channel import Channel, ChannelConfig
|
||||
from corrscope.config import KeywordAttrs, DumpEnumAsStr, CorrError
|
||||
from corrscope.config import KeywordAttrs, DumpEnumAsStr, CorrError, with_units
|
||||
from corrscope.layout import LayoutConfig
|
||||
from corrscope.renderer import MatplotlibRenderer, RendererConfig, Renderer
|
||||
from corrscope.triggers import (
|
||||
|
@ -51,13 +51,13 @@ class Config(
|
|||
""" Default values indicate optional attributes. """
|
||||
|
||||
master_audio: Optional[str]
|
||||
begin_time: float = 0
|
||||
begin_time: float = with_units("s", default=0)
|
||||
end_time: Optional[float] = None
|
||||
|
||||
fps: int
|
||||
|
||||
trigger_ms: int
|
||||
render_ms: int
|
||||
trigger_ms: int = with_units("ms")
|
||||
render_ms: int = with_units("ms")
|
||||
|
||||
# Performance
|
||||
trigger_subsampling: int = 1
|
||||
|
|
|
@ -6,7 +6,7 @@ import attr
|
|||
import matplotlib
|
||||
import numpy as np
|
||||
|
||||
from corrscope.config import DumpableAttrs
|
||||
from corrscope.config import DumpableAttrs, with_units
|
||||
from corrscope.layout import RendererLayout, LayoutConfig, EdgeFinder
|
||||
from corrscope.outputs import RGB_DEPTH, ByteBuffer
|
||||
from corrscope.util import coalesce
|
||||
|
@ -54,7 +54,7 @@ def default_color() -> str:
|
|||
class RendererConfig(DumpableAttrs, always_dump="*"):
|
||||
width: int
|
||||
height: int
|
||||
line_width: float = 1.5
|
||||
line_width: float = with_units("px", default=1.5)
|
||||
|
||||
bg_color: str = "#000000"
|
||||
init_line_color: str = default_color()
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import attr
|
||||
import pytest
|
||||
from ruamel.yaml import yaml_object
|
||||
|
||||
|
@ -9,12 +10,13 @@ from corrscope.config import (
|
|||
Ignored,
|
||||
CorrError,
|
||||
CorrWarning,
|
||||
with_units,
|
||||
get_units,
|
||||
)
|
||||
|
||||
# YAML Idiosyncrasies: https://docs.saltstack.com/en/develop/topics/troubleshooting/yaml_idiosyncrasies.html
|
||||
|
||||
# Load/dump infrastructure testing
|
||||
import attr
|
||||
|
||||
|
||||
def test_dumpable_attrs():
|
||||
|
@ -52,6 +54,26 @@ def test_yaml_object():
|
|||
assert s == "!Bar {}\n"
|
||||
|
||||
|
||||
# Test per-field unit suffixes (used by GUI)
|
||||
|
||||
|
||||
def test_unit_suffix():
|
||||
class Foo(DumpableAttrs):
|
||||
xs: int = with_units("xs")
|
||||
ys: int = with_units("ys", default=2)
|
||||
no_unit: int = 3
|
||||
|
||||
# Assert class constructor works.
|
||||
foo = Foo(1, 2, 3)
|
||||
foo_default = Foo(1)
|
||||
|
||||
# Assert units work.
|
||||
foo_fields = attr.fields(Foo)
|
||||
assert get_units(foo_fields.xs) == " xs"
|
||||
assert get_units(foo_fields.ys) == " ys"
|
||||
assert get_units(foo_fields.no_unit) == ""
|
||||
|
||||
|
||||
# Dataclass dump testing
|
||||
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue