Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EHN: Cut duplicate codes via the factory design pattern #23

Open
Zeroto521 opened this issue May 28, 2022 · 3 comments
Open

EHN: Cut duplicate codes via the factory design pattern #23

Zeroto521 opened this issue May 28, 2022 · 3 comments

Comments

@Zeroto521
Copy link
Member

Zeroto521 commented May 28, 2022

I saw there have a lot of duplicate codes. And they both have the same structure.
Maybe we could extract the same codes via the factory design pattern?

Duplicate codes:

The prototype of this idea.
It could work for pandas-like object, such as pandas.DataFrame, pandas.Series, pandas.Index, geopandas.GeoDataFrame, and geopandas.GeoSeries.

def register_method_factory(register_accessor):
    @wraps(register_accessor)
    def decorator(method):
        def method_accessor(pd_obj):
            @wraps(method)
            def wrapper(*args, **kwargs):
                return method(pd_obj, *args, **kwargs)

            return wrapper

        # Register method as pandas object inner method.
        register_accessor(method.__name__)(method_accessor)

        # Must return method itself, otherwise would get None.
        return method

    return decorator


# or register_dataframe_method = register_method_factory(register_dataframe_accessor)
@register_method_factory
def register_dataframe_method(method):
    """Docstring"""

    return register_dataframe_accessor(method)


@register_method_factory
def register_dataarray_method(method):
    """Docstring"""

    return register_dataarray_accessor(method)
@ericmjl
Copy link
Member

ericmjl commented Jan 3, 2023

@Zeroto521 this feels like a great way to enable arbitrary dataframe extensions, perhaps even to other up-and-coming dataframe libraries. How would you like to move forward here?

@asmirnov69
Copy link
Contributor

asmirnov69 commented Jan 3, 2023

@ericmjl @Zeroto521 also, note that this is exactly the place where pyjviz dev require to insert some additional logic (wip on pyjviz-callbacks branch) ->

def __call__(self, *args, **orig_kwargs):

@asmirnov69 asmirnov69 pinned this issue Jan 3, 2023
@Zeroto521
Copy link
Member Author

Zeroto521 commented Jan 3, 2023

This feature already finished in https://github.com/Zeroto521/my-data-toolkit/blob/main/dtoolkit/accessor/register.py
And an example of geopandas, use this wrapper to decorate geopandas.GeoDataFrame (https://github.com/Zeroto521/my-data-toolkit/blob/main/dtoolkit/geoaccessor/register.py)

PS: I need to focus on my work recently so that any open-source activities will get late.

@asmirnov69 asmirnov69 unpinned this issue Jan 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants