I can apply format rules

This commit is contained in:
2026-01-26 23:29:51 +01:00
parent 9abb9dddfe
commit 778e5ac69d
5 changed files with 144 additions and 28 deletions

View File

@@ -1,3 +1,4 @@
from numbers import Number
from typing import Any
from myfasthtml.core.formatting.dataclasses import Condition
@@ -32,6 +33,10 @@ class ConditionEvaluator:
if condition.operator == "isnotempty":
result = not self._is_empty(cell_value)
return self._apply_negate(result, condition.negate)
if condition.operator == "isnan":
result = self._is_nan(cell_value)
return self._apply_negate(result, condition.negate)
# For all other operators, None cell_value returns False
if cell_value is None:
@@ -70,6 +75,10 @@ class ConditionEvaluator:
if isinstance(value, str) and value == "":
return True
return False
def _is_nan(self, value: Any) -> bool:
"""Check if a value is NaN."""
return isinstance(value, float) and value != value
def _apply_negate(self, result: bool, negate: bool) -> bool:
"""Apply negation if needed."""
@@ -124,7 +133,7 @@ class ConditionEvaluator:
"""Check if cell_value < compare_value."""
if type(cell_value) != type(compare_value):
# Allow int/float comparison
if isinstance(cell_value, (int, float)) and isinstance(compare_value, (int, float)):
if isinstance(cell_value, Number) and isinstance(compare_value, Number):
return cell_value < compare_value
raise TypeError("Type mismatch")
return cell_value < compare_value
@@ -132,7 +141,7 @@ class ConditionEvaluator:
def _less_than_or_equal(self, cell_value: Any, compare_value: Any) -> bool:
"""Check if cell_value <= compare_value."""
if type(cell_value) != type(compare_value):
if isinstance(cell_value, (int, float)) and isinstance(compare_value, (int, float)):
if isinstance(cell_value, Number) and isinstance(compare_value, Number):
return cell_value <= compare_value
raise TypeError("Type mismatch")
return cell_value <= compare_value
@@ -140,7 +149,7 @@ class ConditionEvaluator:
def _greater_than(self, cell_value: Any, compare_value: Any) -> bool:
"""Check if cell_value > compare_value."""
if type(cell_value) != type(compare_value):
if isinstance(cell_value, (int, float)) and isinstance(compare_value, (int, float)):
if isinstance(cell_value, Number) and isinstance(compare_value, Number):
return cell_value > compare_value
raise TypeError("Type mismatch")
return cell_value > compare_value
@@ -148,7 +157,7 @@ class ConditionEvaluator:
def _greater_than_or_equal(self, cell_value: Any, compare_value: Any) -> bool:
"""Check if cell_value >= compare_value."""
if type(cell_value) != type(compare_value):
if isinstance(cell_value, (int, float)) and isinstance(compare_value, (int, float)):
if isinstance(cell_value, Number) and isinstance(compare_value, Number):
return cell_value >= compare_value
raise TypeError("Type mismatch")
return cell_value >= compare_value

View File

@@ -121,6 +121,9 @@ class TextFormatter(Formatter):
max_length: int = None
ellipsis: str = "..."
@dataclass
class ConstantFormatter(Formatter):
value: str = None
@dataclass
class EnumFormatter(Formatter):

View File

@@ -9,6 +9,7 @@ from myfasthtml.core.formatting.dataclasses import (
BooleanFormatter,
TextFormatter,
EnumFormatter,
ConstantFormatter,
)
from myfasthtml.core.formatting.presets import DEFAULT_FORMATTER_PRESETS
@@ -258,6 +259,15 @@ class EnumFormatterResolver(BaseFormatterResolver):
)
class ConstantFormatterResolver(BaseFormatterResolver):
def resolve(self, formatter: ConstantFormatter, value: Any) -> str:
return formatter.value
def apply_preset(self, formatter: Formatter, presets: dict) -> Formatter:
return formatter
class FormatterResolver:
"""
Main resolver that dispatches to the appropriate formatter resolver.
@@ -281,6 +291,7 @@ class FormatterResolver:
BooleanFormatter: BooleanFormatterResolver(),
TextFormatter: TextFormatterResolver(),
EnumFormatter: EnumFormatterResolver(lookup_resolver),
ConstantFormatter: ConstantFormatterResolver()
}
def resolve(self, formatter: Formatter, value: Any) -> str: