Repository: Avaiga/taipy
File: taipy/core/data/_tabular_datanode_mixin.py (Python)

Code before:
# the License. You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.

from typing import Any, Callable, Dict, List, Union

import numpy as np
import pandas as pd

from ..exceptions.exceptions import InvalidExposedType


class _TabularDataNodeMixin(object):
    """Mixin class designed to handle tabular representable data nodes."""

    _HAS_HEADER_PROPERTY = "has_header"
    _EXPOSED_TYPE_PROPERTY = "exposed_type"
    _EXPOSED_TYPE_NUMPY = "numpy"
    _EXPOSED_TYPE_PANDAS = "pandas"
    _EXPOSED_TYPE_MODIN = "modin"  # Deprecated in favor of pandas since 3.1.0
    _VALID_STRING_EXPOSED_TYPES = [_EXPOSED_TYPE_PANDAS, _EXPOSED_TYPE_NUMPY]
    _VALID_OTHER_EXPOSED_TYPES = [pd.DataFrame, np.ndarray]
    _EXPOSED_TYPE_PANDAS_DATAFRAME = pd.DataFrame
    _EXPOSED_TYPE_NUMPY_NDARRAY = np.ndarray

    def __init__(self, **kwargs) -> None:
        self._decoder: Union[Callable, Any]
        self.custom_document = kwargs.get(self._EXPOSED_TYPE_PROPERTY)

        custom_decoder = getattr(self.custom_document, "decode", None)
        if callable(custom_decoder):
            self._decoder = custom_decoder
        elif kwargs.get(self._HAS_HEADER_PROPERTY, True):
            self._decoder = self._default_decoder_with_header
        else:
            self._decoder = self._default_decoder_without_header

        self._encoder = self._default_encoder
        custom_encoder = getattr(self.custom_document, "encode", None)
        if callable(custom_encoder):
            self._encoder = custom_encoder

    def _convert_data_to_dataframe(self, exposed_type: Any, data: Any) -> Union[pd.DataFrame, pd.Series]:
        if exposed_type in [self._EXPOSED_TYPE_PANDAS, self._EXPOSED_TYPE_PANDAS_DATAFRAME] and isinstance(
            data, (pd.DataFrame, pd.Series)
        ):
            return data

Review comment (suggestion):
The `_VALID_STRING_EXPOSED_TYPES` is used when serialize and deserialize the `exposed_type` property.

The `_VALID_OTHER_EXPOSED_TYPES` is defined here but not used anywhere.

Code after:
# the License. You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.

from typing import Any, Callable, Dict, List, Union

import numpy as np
import pandas as pd

from ..exceptions.exceptions import InvalidExposedType


class _TabularDataNodeMixin(object):
    """Mixin class designed to handle tabular representable data nodes."""

    _HAS_HEADER_PROPERTY = "has_header"
    _EXPOSED_TYPE_PROPERTY = "exposed_type"
    _EXPOSED_TYPE_NUMPY = "numpy"
    _EXPOSED_TYPE_PANDAS = "pandas"
    _EXPOSED_TYPE_MODIN = "modin"  # Deprecated in favor of pandas since 3.1.0
    _VALID_STRING_EXPOSED_TYPES = [_EXPOSED_TYPE_PANDAS, _EXPOSED_TYPE_NUMPY]
    _EXPOSED_TYPE_PANDAS_DATAFRAME = pd.DataFrame
    _EXPOSED_TYPE_NUMPY_NDARRAY = np.ndarray

    def __init__(self, **kwargs) -> None:
        self._decoder: Union[Callable, Any]
        self.custom_document = kwargs.get(self._EXPOSED_TYPE_PROPERTY)

        custom_decoder = getattr(self.custom_document, "decode", None)
        if callable(custom_decoder):
            self._decoder = custom_decoder
        elif kwargs.get(self._HAS_HEADER_PROPERTY, True):
            self._decoder = self._default_decoder_with_header
        else:
            self._decoder = self._default_decoder_without_header

        self._encoder = self._default_encoder
        custom_encoder = getattr(self.custom_document, "encode", None)
        if callable(custom_encoder):
            self._encoder = custom_encoder

    def _convert_data_to_dataframe(self, exposed_type: Any, data: Any) -> Union[pd.DataFrame, pd.Series]:
        if exposed_type in [self._EXPOSED_TYPE_PANDAS, self._EXPOSED_TYPE_PANDAS_DATAFRAME] and isinstance(
            data, (pd.DataFrame, pd.Series)
        ):
            return data
        elif exposed_type in [self._EXPOSED_TYPE_NUMPY, self._EXPOSED_TYPE_NUMPY_NDARRAY] and isinstance(