Make validation run by default when initializing an AuDaGeFrame.
This commit is contained in:
@@ -8,11 +8,14 @@ date: 2026-09-14
|
|||||||
desc: Extensions to DataFrame for AuDaGe purposes.
|
desc: Extensions to DataFrame for AuDaGe purposes.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from collections.abc import Sequence
|
||||||
from enum import StrEnum, auto
|
from enum import StrEnum, auto
|
||||||
|
|
||||||
from pandas import DataFrame
|
from pandas import DataFrame
|
||||||
|
|
||||||
type Frame = list[dict[str, any]]
|
type DataFrameLike = DataFrame | Sequence[Sequence]
|
||||||
|
|
||||||
|
type Frame = dict[str, list[any]]
|
||||||
"""A mutable data structure for importing from and exporting to DataFrames."""
|
"""A mutable data structure for importing from and exporting to DataFrames."""
|
||||||
|
|
||||||
|
|
||||||
@@ -28,11 +31,25 @@ class MandatoryColumns(StrEnum):
|
|||||||
class AuDaGeFrame(DataFrame):
|
class AuDaGeFrame(DataFrame):
|
||||||
"""DataFrame wrapped for AuDaGe purposes."""
|
"""DataFrame wrapped for AuDaGe purposes."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self, data: DataFrameLike, validate: bool = True, name: str = ""
|
||||||
|
):
|
||||||
|
super().__init__(data)
|
||||||
|
self._name = name
|
||||||
|
missing_columns = self.missing_columns()
|
||||||
|
if validate and missing_columns != []:
|
||||||
|
raise ValueError(
|
||||||
|
f"The provided data is missing the following columns "
|
||||||
|
f"required by AuDaGe:\n{'\n'.join(missing_columns)}"
|
||||||
|
)
|
||||||
|
|
||||||
def missing_columns(self) -> list[MandatoryColumns]:
|
def missing_columns(self) -> list[MandatoryColumns]:
|
||||||
"""Return a list of mandatory columns that are missing from this
|
"""Return a list of mandatory columns that are missing from this
|
||||||
DataFrame.
|
DataFrame.
|
||||||
"""
|
"""
|
||||||
return [label for label in MandatoryColumns if not label in self.columns]
|
return [
|
||||||
|
label for label in MandatoryColumns if not label in self.columns
|
||||||
|
]
|
||||||
|
|
||||||
def is_valid(self) -> bool:
|
def is_valid(self) -> bool:
|
||||||
"""Check whether the DataFrame contains all mandatory columns."""
|
"""Check whether the DataFrame contains all mandatory columns."""
|
||||||
|
|||||||
@@ -5,24 +5,20 @@ from AuDaGe.audage_frame import AuDaGeFrame
|
|||||||
|
|
||||||
class TestAuDaGeFrame(unittest.TestCase):
|
class TestAuDaGeFrame(unittest.TestCase):
|
||||||
def test_init(self):
|
def test_init(self):
|
||||||
valid_adf = AuDaGeFrame(
|
valid_adf = {
|
||||||
{
|
"name": ["a", "b", "c"],
|
||||||
"name": ["a", "b", "c"],
|
"type": ["d", "e", "f"],
|
||||||
"type": ["d", "e", "f"],
|
"address": [1, 2, 3],
|
||||||
"address": [1, 2, 3],
|
"comment": ["g", "h", "i"],
|
||||||
"comment": ["g", "h", "i"],
|
}
|
||||||
}
|
self.assertTrue(AuDaGeFrame(valid_adf).is_valid())
|
||||||
)
|
invalid_adf = {
|
||||||
self.assertTrue(valid_adf.is_valid())
|
"name": ["a", "b", "c"],
|
||||||
invalid_adf = AuDaGeFrame(
|
# "type": ["d", "e", "f"],
|
||||||
{
|
"address": [1, 2, 3],
|
||||||
"name": ["a", "b", "c"],
|
"comment": ["g", "h", "i"],
|
||||||
# "type": ["d", "e", "f"],
|
}
|
||||||
"address": [1, 2, 3],
|
self.assertRaises(ValueError, AuDaGeFrame, invalid_adf)
|
||||||
"comment": ["g", "h", "i"],
|
|
||||||
}
|
|
||||||
)
|
|
||||||
self.assertFalse(invalid_adf.is_valid())
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user