Use dataclass for class definitions

This commit is contained in:
Candifloss 2025-03-03 14:08:29 +05:30
parent e00b1a128c
commit 3731a68d3c

View File

@ -1,30 +1,21 @@
from datetime import datetime
import re
from typing import List, Optional, Tuple
from dataclasses import dataclass, field
ALLOWED_INPUT_TYPES = {"text", "number", "date", "select"}
@dataclass
class Attribute:
"""Base class for all attribute types."""
def __init__(
self,
attrib_name: str,
display_name: str,
html_input_type: str,
required: bool = False,
unique: bool = False,
primary: bool = False,
default_val: Optional[str] = None,
compareto: Optional[List[Tuple[str, str]]] = None,
):
self.attrib_name = attrib_name
self.display_name = display_name
self.html_input_type = html_input_type
self.required = required
self.unique = unique
self.primary = primary
self.default_val = default_val
self.compareto = compareto
attrib_name: str
display_name: str
html_input_type: str
required: bool = False
unique: bool = False
primary: bool = False
default_val: Optional[str] = None
compareto: Optional[List[Tuple[str, str]]] = None
def validate(self) -> Optional[str]:
"""Validate common attributes. Returns an error message if invalid, otherwise None."""
@ -38,24 +29,17 @@ class Attribute:
return f"Invalid input type '{self.html_input_type}' for attribute '{self.attrib_name}'."
return None
@dataclass
class textAttribute(Attribute):
"""Class for text attributes."""
def __init__(
self,
attrib_name: str,
display_name: str,
regex: Optional[str] = None,
min_length: Optional[int] = None,
max_length: Optional[int] = 50,
allowed_chars: Optional[str] = None,
**kwargs
):
super().__init__(attrib_name, display_name, html_input_type="text", **kwargs)
self.regex = regex
self.allowed_chars = allowed_chars
self.min_length = min_length
self.max_length = max_length
regex: Optional[str] = None
min_length: Optional[int] = None
max_length: Optional[int] = 50
allowed_chars: Optional[str] = None
def __post_init__(self):
"""Post-initialization to set the HTML input type."""
self.html_input_type = "text"
def validate(self) -> Optional[str]:
"""Validate text-specific attributes."""
@ -84,22 +68,16 @@ class textAttribute(Attribute):
return f"Invalid default value for '{self.attrib_name}'. The maximum length is: {self.max_length}"
return None
@dataclass
class intAttribute(Attribute):
"""Class for integer attributes."""
def __init__(
self,
attrib_name: str,
display_name: str,
min_val: Optional[int] = None,
max_val: Optional[int] = None,
step: int = 1,
**kwargs
):
super().__init__(attrib_name, display_name, html_input_type="number", **kwargs)
self.min_val = min_val
self.max_val = max_val
self.step = step
min_val: Optional[int] = None
max_val: Optional[int] = None
step: int = 1
def __post_init__(self):
"""Post-initialization to set the HTML input type."""
self.html_input_type = "number"
def validate(self) -> Optional[str]:
"""Validate integer-specific attributes."""
@ -130,22 +108,16 @@ class intAttribute(Attribute):
return f"default_val cannot be greater than max_val for attribute '{self.attrib_name}'."
return None
@dataclass
class floatAttribute(Attribute):
"""Class for float attributes."""
def __init__(
self,
attrib_name: str,
display_name: str,
min_val: Optional[float] = None,
max_val: Optional[float] = None,
step: float = 0.1,
**kwargs
):
super().__init__(attrib_name, display_name, html_input_type="number", **kwargs)
self.min_val = min_val
self.max_val = max_val
self.step = step
min_val: Optional[float] = None
max_val: Optional[float] = None
step: float = 0.1
def __post_init__(self):
"""Post-initialization to set the HTML input type."""
self.html_input_type = "number"
def validate(self) -> Optional[str]:
"""Validate float-specific attributes."""
@ -159,7 +131,7 @@ class floatAttribute(Attribute):
if self.min_val is not None and self.max_val is not None and self.min_val > self.max_val:
return f"min_val cannot be greater than max_val for attribute '{self.attrib_name}'."
if self.step is not None:
if not isinstance(self.step, float):
if not isinstance(self.step, (int, float)):
return f"Step must be an float for attribute '{self.attrib_name}'."
if self.step <= 0:
return f"Step must be a positive float for attribute '{self.attrib_name}'."
@ -176,20 +148,15 @@ class floatAttribute(Attribute):
return f"default_val cannot be greater than max_val for attribute '{self.attrib_name}'."
return None
@dataclass
class dateAttribute(Attribute):
"""Class for date attributes."""
def __init__(
self,
attrib_name: str,
display_name: str,
min_val: Optional[str] = None,
max_val: Optional[str] = None,
**kwargs
):
super().__init__(attrib_name, display_name, html_input_type="date", **kwargs)
self.min_val = min_val
self.max_val = max_val
min_val: Optional[str] = None
max_val: Optional[str] = None
def __post_init__(self):
"""Post-initialization to set the HTML input type."""
self.html_input_type = "date"
def _is_date(self, value: str) -> bool:
"""Check if a value is a valid date in YYYY-MM-DD format."""
@ -227,18 +194,14 @@ class dateAttribute(Attribute):
return f"default_val cannot be later than max_val for attribute '{self.attrib_name}'."
return None
@dataclass
class selectAttribute(Attribute):
"""Class for select attributes."""
def __init__(
self,
attrib_name: str,
display_name: str,
options: List[str],
**kwargs
):
super().__init__(attrib_name, display_name, html_input_type="select", **kwargs)
self.options = options
options: List[str]
def __post_init__(self):
"""Post-initialization to set the HTML input type."""
self.html_input_type = "select"
def validate(self) -> Optional[str]:
"""Validate select-specific attributes."""