-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f570f7
commit 26c9a8f
Showing
14 changed files
with
176 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,17 @@ | ||
# SPDX-FileCopyrightText: 2024-present Dylan Lukes <[email protected]> | ||
# | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
__all__ = [ | ||
"int_", | ||
"float_", | ||
"str_", | ||
"bool_", | ||
"any_", | ||
"none", | ||
"numeric", | ||
"equatable", | ||
"comparable" | ||
] | ||
|
||
from renkon.core.model.type import int_, float_, str_, bool_, any_, none, numeric, equatable, comparable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,43 @@ | ||
from collections.abc import Hashable, Iterator, Mapping, Sequence | ||
from typing import Self, overload | ||
import sys | ||
from collections.abc import Sequence | ||
from typing import Self | ||
|
||
from polars.type_aliases import SchemaDict | ||
from pydantic import ConfigDict, RootModel | ||
from polars.type_aliases import SchemaDict as PolarsSchemaDict | ||
from pydantic import GetCoreSchemaHandler | ||
from pydantic_core import core_schema as cs | ||
|
||
from renkon.core.model.type import RenkonType, tyconv_pl_to_rk | ||
from renkon.core.model.type_aliases import ColumnName, ColumnNames | ||
from renkon.core.model.type import RenkonType, tyconv_pl_to_rk, tyconv_rk_to_pl | ||
|
||
type ColumnName = str | ||
type ColumnNames = tuple[ColumnName, ...] | ||
|
||
class Schema(RootModel[dict[ColumnName, RenkonType]], Mapping[ColumnName, RenkonType], Hashable): | ||
""" | ||
Represents a schema for some or all of the columns a data frame. | ||
type ColumnType = RenkonType | ||
type ColumnTypes = tuple[ColumnType, ...] | ||
type ColumnTypeSet = frozenset[ColumnType] | ||
|
||
Explicitly preserves order of its entries, provides a .index method for lookup of | ||
the index of a column name, and convenience accessors for column names and types. | ||
if sys.version_info <= (3, 6): | ||
raise RuntimeError("Dictionaries are not guaranteed to preserve order before Python 3.6.") | ||
|
||
Note that Python dict preserves insertion order since Pyt@hon 3.7. | ||
""" | ||
|
||
model_config = ConfigDict(frozen=True) | ||
root: dict[ColumnName, RenkonType] | ||
|
||
def __hash__(self) -> int: | ||
return hash(tuple(self.root.items())) | ||
|
||
@overload | ||
def __getitem__(self, key: ColumnName) -> RenkonType: ... | ||
|
||
@overload | ||
def __getitem__(self, key: ColumnNames) -> Self: ... | ||
|
||
def __getitem__(self, key: ColumnName | ColumnNames) -> RenkonType | Self: | ||
match key: | ||
case str(): | ||
return self.root[key] | ||
case tuple(): | ||
return self.subschema(key) | ||
|
||
def __iter__(self) -> Iterator[ColumnName]: # type: ignore | ||
yield from iter(self.root) | ||
|
||
def __len__(self) -> int: | ||
return len(self.root) | ||
|
||
def __lt__(self, other: Self) -> bool: | ||
"""Compares two schemas by their column names in lexicographic order.""" | ||
return self.columns < other.columns | ||
|
||
class Schema(dict[str, RenkonType]): | ||
@property | ||
def columns(self) -> ColumnNames: | ||
return tuple(self.root.keys()) | ||
def columns(self): | ||
return list(self.keys()) | ||
|
||
@property | ||
def dtypes(self) -> tuple[RenkonType, ...]: | ||
return tuple(self.root.values()) | ||
def types(self): | ||
return list(self.values()) | ||
|
||
def subschema(self, columns: Sequence[str]) -> Self: | ||
return self.__class__({col: self[col] for col in columns}) | ||
|
||
@classmethod | ||
def from_polars(cls, schema_dict: SchemaDict) -> Self: | ||
return cls(root={col_name: tyconv_pl_to_rk(pl_ty) for col_name, pl_ty in schema_dict.items()}) | ||
def from_polars(cls, schema: PolarsSchemaDict): | ||
return cls({col: tyconv_pl_to_rk(pl_ty) for col, pl_ty in schema.items()}) | ||
|
||
def subschema(self, columns: Sequence[str]) -> Self: | ||
return self.__class__(root={col: self.root[col] for col in columns}) | ||
def to_polars(self) -> PolarsSchemaDict: | ||
return {col: tyconv_rk_to_pl(rk_ty) for col, rk_ty in self.items()} | ||
|
||
@classmethod | ||
def __get_pydantic_core_schema__(cls, source_type: type, handler: GetCoreSchemaHandler, /): | ||
return cs.chain_schema([handler(dict), cs.no_info_plain_validator_function(cls.__call__)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,18 @@ | ||
# SPDX-FileCopyrightText: 2024-present Dylan Lukes <[email protected]> | ||
# | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
__all__ = ["Equal", "Less", "LessOrEqual", "Greater", "GreaterOrEqual"] | ||
__all__ = [ | ||
"Equal", | ||
"Less", | ||
"LessOrEqual", | ||
"Greater", | ||
"GreaterOrEqual", | ||
"NonNull", | ||
"NonZero", | ||
"NonNegative", | ||
"Linear2" | ||
] | ||
|
||
from renkon.core.trait.compare import Equal, Greater, GreaterOrEqual, Less, LessOrEqual | ||
from renkon.core.trait.refinement import NonNull, NonZero, NonNegative | ||
from renkon.core.trait.linear import Linear2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# SPDX-FileCopyrightText: 2024-present Dylan Lukes <[email protected]> | ||
# | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
from renkon.core.model.schema import Schema | ||
|
||
|
||
def test_schema(): | ||
schema = Schema({}) | ||
print(schema) | ||
|
||
def test_schema_as_model_field(): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# SPDX-FileCopyrightText: 2024-present Dylan Lukes <[email protected]> | ||
# | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
import pytest | ||
|
||
import renkon.api as rk | ||
from renkon.core.model import TraitSketch, Schema | ||
from renkon.core.trait import Linear2, Equal | ||
|
||
|
||
def test_sketch_bindings_missing(): | ||
schema = Schema({ | ||
"x": rk.int_(), | ||
"y": rk.int_() | ||
}) | ||
with pytest.raises(ValueError, match="missing in bindings"): | ||
TraitSketch( | ||
spec=Equal.spec, | ||
schema=schema, | ||
bindings={ | ||
"A": "x" | ||
} | ||
) | ||
|
||
|
||
def test_sketch_bindings_extra(): | ||
schema = Schema({ | ||
"x": rk.int_(), | ||
"y": rk.int_() | ||
}) | ||
with pytest.raises(ValueError, match="do not occur in pattern"): | ||
TraitSketch( | ||
spec=Equal.spec, | ||
schema=schema, | ||
bindings={ | ||
"A": "x", | ||
"B": "y", | ||
"C": "z" | ||
} | ||
) | ||
|
||
|
||
def test_sketch_linear2(): | ||
schema = Schema({ | ||
"time": rk.float_(), | ||
"open tabs": rk.float_() | ||
}) | ||
TraitSketch( | ||
spec=Linear2.spec, | ||
schema=schema, | ||
bindings={ | ||
"X": "time", | ||
"Y": "open tabs" | ||
} | ||
) |