Skip to content

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

@Zeroto521

Description

@Zeroto521

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions