Skip to content

Commit

Permalink
finish DEMO-3
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanLukes committed Aug 12, 2024
1 parent aa2c0a9 commit 7dfdd82
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 18 deletions.
34 changes: 19 additions & 15 deletions src/renkon/core/model/type/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,6 @@
#
# SPDX-License-Identifier: BSD-3-Clause

from renkon.core.model.type.base import (
Bool,
Bottom,
Float,
Int,
String,
Type,
Union,
rk_bool,
rk_bottom,
rk_float,
rk_int,
rk_str,
rk_union,
)

__all__ = [
"Type",
Expand All @@ -32,4 +17,23 @@
"rk_bool",
"rk_union",
"rk_bottom",
"renkon_type_to_polars_type",
"polars_type_to_renkon_type",
]

from renkon.core.model.type.base import (
Bool,
Bottom,
Float,
Int,
String,
Type,
Union,
rk_bool,
rk_bottom,
rk_float,
rk_int,
rk_str,
rk_union
)
from renkon.core.model.type.convert import renkon_type_to_polars_type, polars_type_to_renkon_type
3 changes: 1 addition & 2 deletions src/renkon/core/model/type/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Type(BaseModel, ABC, Hashable):
class Config:
frozen = True

_parser: ClassVar[Lark] = Lark(grammar, parser="lalr")
_parser: ClassVar[Lark] = Lark(grammar, lexer="standard", parser="lalr")

@abstractmethod
def is_equal(self, other: Type) -> bool:
Expand Down Expand Up @@ -361,5 +361,4 @@ def union(self, types: list[Type]) -> Union:
def paren(self, type_: list[Type]) -> Type:
return type_[0]


# endregion
44 changes: 44 additions & 0 deletions src/renkon/core/model/type/convert.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
# SPDX-FileCopyrightText: 2024-present Dylan Lukes <[email protected]>
#
# SPDX-License-Identifier: BSD-3-Clause

import polars as pl

from renkon.core.model.type import rk_int
from renkon.core.model.type.base import Type as RenkonType, rk_float, rk_str, rk_bool


def polars_type_to_renkon_type(rk_ty: pl.PolarsDataType) -> RenkonType:
"""
Convert a Polars data type to a Renkon data type.
"""
if rk_ty.is_integer():
return rk_int

if rk_ty.is_float():
return rk_float

if rk_ty.is_(pl.String):
return rk_str

if rk_ty.is_(pl.Boolean):
return rk_bool

raise ValueError(f"Unsupported Polars data type: {rk_ty}")


def renkon_type_to_polars_type(rk_ty: RenkonType) -> pl.PolarsDataType:
"""
Convert a Renkon data type to a Polars data type.
"""

if rk_ty.is_equal(rk_int):
return pl.Int64

if rk_ty.is_equal(rk_float) or rk_ty.is_equal(rk_int | rk_float):
return pl.Float64

if rk_ty.is_equal(rk_str):
return pl.Utf8

if rk_ty.is_equal(rk_bool):
return pl.Boolean

raise ValueError(f"Unsupported Renkon data type: {rk_ty}")
28 changes: 28 additions & 0 deletions tests/renkon/core/model/test_convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# SPDX-FileCopyrightText: 2024-present Dylan Lukes <[email protected]>
#
# SPDX-License-Identifier: BSD-3-Clause

import polars as pl
import pytest

import renkon.core.model.type as rkty
from renkon.core.model.type import polars_type_to_renkon_type, renkon_type_to_polars_type


def test_polars_type_to_renkon_type():
assert polars_type_to_renkon_type(pl.Int64) == rkty.rk_int
assert polars_type_to_renkon_type(pl.Float64) == rkty.rk_float
assert polars_type_to_renkon_type(pl.Utf8) == rkty.rk_str
assert polars_type_to_renkon_type(pl.Boolean) == rkty.rk_bool

with pytest.raises(ValueError):
polars_type_to_renkon_type(pl.Date)
polars_type_to_renkon_type(pl.Time)
polars_type_to_renkon_type(pl.Datetime)


def test_renkon_type_to_polars_type():
assert renkon_type_to_polars_type(rkty.rk_int) == pl.Int64
assert renkon_type_to_polars_type(rkty.rk_float) == pl.Float64
assert renkon_type_to_polars_type(rkty.rk_str) == pl.Utf8
assert renkon_type_to_polars_type(rkty.rk_bool) == pl.Boolean
2 changes: 1 addition & 1 deletion tests/renkon/core/model/test_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def test_comparable():


def test_type_parser():
parser = Lark(grammar, parser='lalr', transformer=TreeToType())
parser = Lark(grammar, parser='lalr', lexer="standard", transformer=TreeToType())

assert parser.parse("int") == rk_int
assert parser.parse("float") == rk_float
Expand Down

0 comments on commit 7dfdd82

Please sign in to comment.