Skip to content

Commit

Permalink
add custom written 'classproperty' decorator
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Chigarev <[email protected]>
  • Loading branch information
dchigarev committed Dec 7, 2023
1 parent 1a24eb1 commit fb18942
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
22 changes: 5 additions & 17 deletions modin/pandas/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
SupportsPublicToNumPy,
_inherit_docstrings,
expanduser_path_arg,
classproperty,
)

# below logic is to handle circular imports without errors
Expand All @@ -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):
Expand Down
27 changes: 27 additions & 0 deletions modin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit fb18942

Please sign in to comment.