From fb1894225432d5128a68b765d012937a9d296b06 Mon Sep 17 00:00:00 2001 From: Dmitry Chigarev Date: Thu, 7 Dec 2023 12:12:38 +0100 Subject: [PATCH] add custom written 'classproperty' decorator Signed-off-by: Dmitry Chigarev --- modin/pandas/io.py | 22 +++++----------------- modin/utils.py | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/modin/pandas/io.py b/modin/pandas/io.py index 91c86faa9c9..37a4a532aea 100644 --- a/modin/pandas/io.py +++ b/modin/pandas/io.py @@ -74,6 +74,7 @@ SupportsPublicToNumPy, _inherit_docstrings, expanduser_path_arg, + classproperty, ) # below logic is to handle circular imports without errors @@ -87,27 +88,14 @@ class ModinObjects: _dataframe = None - @classmethod - def DataFrame(cls, *args, **kwargs): - """ - Build ``modin.pandas.DataFrame`` object with the passed arguments. - - Parameters - ---------- - *args : tuple - Positional arguments to pass to the ``modin.pandas.DataFrame`` constructor. - **kwargs : dict - Keyword arguments to pass to the ``modin.pandas.DataFrame`` constructor. - - Returns - ------- - modin.pandas.DataFrame - """ + @classproperty + def DataFrame(cls): + """Get ``modin.pandas.DataFrame`` class.""" if cls._dataframe is None: from .dataframe import DataFrame cls._dataframe = DataFrame - return cls._dataframe(*args, **kwargs) + return cls._dataframe def _read(**kwargs): diff --git a/modin/utils.py b/modin/utils.py index 27830233027..d56408dcb45 100644 --- a/modin/utils.py +++ b/modin/utils.py @@ -850,3 +850,30 @@ class ModinAssumptionError(Exception): """An exception that allows us defaults to pandas if any assumption fails.""" pass + + +class classproperty: + """ + Decorator that allows creating read-only class properties. + + Parameters + ---------- + func : method + + Examples + -------- + >>> class A: + ... field = 10 + ... @classproperty + ... def field_x2(cls): + ... return cls.field * 2 + ... + >>> print(A.field_x2) + 20 + """ + + def __init__(self, func: Any): + self.fget = func + + def __get__(self, instance: Any, owner: Any) -> Any: # noqa: GL08 + return self.fget(owner)